Home/News | About | Download | Documentation | Forum | Bug Reports | Contact | Donations | Consulting | Projects | Legal | Security | FATE


How to use -map option

A collection of useful tutorials for some common tasks.

How to use -map option

Postby burek » Fri Jun 22, 2012 9:31 am

The best way to understand the -map option is to think of it like a way to tell FFmpeg which streams do you want to select/copy from input to output.
The order of -map options, specified on cmd line, will create the same order of streams in the output file.
Here are several examples.

Input file

In all following examples, we will use an example input file like this one:
Code: Select all
# fmpeg -i input.mkv

ffmpeg version ... Copyright (c) 2000-2012 the FFmpeg developers
...
Input #0, matroska,webm, from 'input.mkv':
  Duration: 01:39:44.02, start: 0.000000, bitrate: 5793 kb/s
    Stream #0:0(eng): Video: h264 (High), yuv420p, 1920x800, 23.98 fps, 23.98 tbr, 1k tbn, 47.95 tbc (default)
    Stream #0:1(ger): Audio: dts (DTS), 48000 Hz, 5.1(side), s16, 1536 kb/s (default)
    Stream #0:2(eng): Audio: dts (DTS), 48000 Hz, 5.1(side), s16, 1536 kb/s
    Stream #0:3(ger): Subtitle: text (default)
At least one output file must be specified

#


Example 1

Now, let's say we wan't to:
- copy video stream
- encode german audio stream to mp3 (128kbps) and aac (96kbps) (creating 2 audio streams in the output)
- drop english audio stream
- copy subtitle stream

This can be done using the following FFmpeg command line:
Code: Select all
ffmpeg -i input.mkv \
    -map 0:0 -map 0:1 -map 0:1 -map 0:3 \
    -c:v copy \
    -c:a:0 libmp3lame -b:a:0 128k \
    -c:a:1 libfaac -b:a:1 96k \
    -c:s copy \
    output.mkv

Note there is no "-map 0:2" and that "-map 0:1" has been specified twice.

Using "-map 0:0 -map 0:1 -map 0:1 -map 0:3" we told FFmpeg to select/map specified input streams to output in that order.
So, our output will now have the following streams:
Code: Select all
Output #0, matroska, to 'output.mkv':
    Stream #0:0(eng): Video ...
    Stream #0:1(ger): Audio ...
    Stream #0:2(ger): Audio ...
    Stream #0:3(ger): Subtitle ...


After we selected which streams we would like in our output, using "-map" option, we specified codecs for each stream in our output.
Video and subtitle stream have just been copied and german audio stream has been encoded to 2 new audio streams, mp3 and aac.
We used "-c:a:0" to specify codec for the output's first AUDIO stream and "-c:a:1" to specify codec for the output's second AUDIO stream.
Note that "a:0" refers to the output's first AUDIO stream (#0:1 in our case), "a:1" refers to the output's 2nd AUDIO stream (#0:2 in our case), etc.

The result will be:
Code: Select all
Output #0, matroska, to 'output.mkv':
    Stream #0:0(eng): Video ...
    Stream #0:1(ger): Audio ...
    Stream #0:2(ger): Audio ...
    Stream #0:3(ger): Subtitle ...
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
  Stream #0:1 -> #0:1 (dca -> libmp3lame)
  Stream #0:2 -> #0:2 (dca -> libfaac)
  Stream #0:3 -> #0:3 (copy)


Example 2

Let's say that we want to reorder input streams backwards, so that we have output like this:
Code: Select all
    Stream #0:0(ger): Subtitle: text (default)
    Stream #0:1(eng): Audio: dts (DTS), 48000 Hz, 5.1(side), s16, 1536 kb/s
    Stream #0:2(ger): Audio: dts (DTS), 48000 Hz, 5.1(side), s16, 1536 kb/s (default)
    Stream #0:3(eng): Video: h264 (High), yuv420p, 1920x800, 23.98 fps, 23.98 tbr, 1k tbn, 47.95 tbc (default)


This can simply be done using the following command line:
Code: Select all
ffmpeg -i input.mkv -map 0:3 -map 0:2 -map 0:1 -map 0:0 -c copy output.mkv

Note that we specified all the input streams, but in the reverse order, which causes that order to be respected in the output.
The option "-c copy" tells FFmpeg to use "copy" on all streams.

Example 3

If we want to extract only audio streams, from input file, then we can do it like this:
Code: Select all
ffmpeg -i input.mkv -map 0:1 -map 0:2 -c copy output.mkv
burek
 
Posts: 864
Joined: Mon May 09, 2011 10:16 pm
Location: Serbia

Return to Tutorials

Who is online

Users browsing this forum: No registered users and 2 guests