Changes between Version 18 and Version 19 of Map


Ignore:
Timestamp:
May 14, 2020, 7:46:09 PM (6 years ago)
Author:
llogan
Comment:

complete rewrite and simplification

Legend:

Unmodified
Added
Removed
Modified
  • Map

    v18 v19  
    1 = Introduction =
    2 [[PageOutline(1-3, Contents)]]
     1[[PageOutline]]
    32
    4 The best way to understand [http://ffmpeg.org/ffmpeg.html#Advanced-options -map option] is to think of it like a way to tell FFmpeg which streams do you want to select/copy from input to an output.
     3The [https://ffmpeg.org/ffmpeg.html#Advanced-options -map] option is used to choose which streams from the input(s) should be included in the output(s). The `-map` option can also be used to exclude specific streams with ''negative mapping''.
    54
    6 '''The order of -map options, specified on cmd line, will create the same order of streams in the output file.'''
     5= Default behavior =
    76
    8 Here are several examples.
     7If you do not use the `-map` option then the default [https://ffmpeg.org/ffmpeg.html#Stream-selection stream selection] behavior will automatically choose streams.
    98
    10 == Default ==
     9* Default stream selection will '''not''' automatically choose all of the streams.
     10* Only one stream per type will be selected. For example, if the input has 3 video streams it will only choose 1.
     11* The default stream selection will choose streams based upon [https://ffmpeg.org/ffmpeg.html#Automatic-stream-selection specific criteria].
    1112
    12  The default behavior (when map is not specified), ex:
     13Using the `-map` option disables the default stream selection behavior and allows you to manually choose streams.
     14
     15= Syntax =
     16
     17The syntax is:
     18
    1319{{{
    14   ffmpeg -i INPUT OUTPUT
     20-map input_file_index:stream_type_specifier:stream_index
    1521}}}
    1622
    17 is to, in essence, discover the "highest quality" (single) video input stream and "highest quality" (single) audio input stream from all input(s), and "send" that to the OUTPUT.  All other input streams are in essence discarded.
    18 
    19 The "map" command basically means "include this stream into the immediately following OUTPUT file."
    20 
    21 If we wanted to "show" the same behavior using the map command, it would look like this:
     23Example:
    2224
    2325{{{
    24   ffmpeg -i INPUT -map single_highest_quality_video_stream_from_all_inputs -map single_highest_quality_audio_stream_from_all_inputs OUTPUT
     26-map 2:v:5
    2527}}}
    26 and OUTPUT will end up with 2 streams, one audio, one video.
    2728
    28 When you want to control which streams are included, or include more than one stream in the output, then you will need/want to specify the "-map" command manually, and change these parameters.
     29Translation:
    2930
    30 == Input file ==
     31* `2` is input file !#3.
     32* `v` is a [https://ffmpeg.org/ffmpeg.html#Stream-specifiers-1 stream specifier] to choose the type of stream. Other stream specifiers include `a`udio, `s`ubtitles, `d`ata, a`t`tachments.
     33* `5` is video stream !#6.
    3134
    32 In all following examples, we will use an example input file like this one:
     35So `-map 2:v:5` will choose from input !#3, audio stream !#6. Note that counting starts from 0.
     36
     37The stream specifier is optional and can be omitted. Example:
    3338
    3439{{{
    35 # fmpeg -i input.mkv
    36 
    37 ffmpeg version ... Copyright (c) 2000-2012 the FFmpeg developers
    38 ...
    39 Input #0, matroska,webm, from 'input.mkv':
    40   Duration: 01:39:44.02, start: 0.000000, bitrate: 5793 kb/s
    41     Stream #0:0(eng): Video: h264 (High), yuv420p, 1920x800, 23.98 fps, 23.98 tbr, 1k tbn, 47.95 tbc (default)
    42     Stream #0:1(ger): Audio: dts (DTS), 48000 Hz, 5.1(side), s16, 1536 kb/s (default)
    43     Stream #0:2(eng): Audio: dts (DTS), 48000 Hz, 5.1(side), s16, 1536 kb/s
    44     Stream #0:3(ger): Subtitle: text (default)
    45 At least one output file must be specified
    46 
    47 #
     40-map 1:2
    4841}}}
    4942
    50 == Example 1 ==
     43From input !#2 choose stream !#3.
    5144
    52 Now, let's say we want to:
    53 - copy video stream
    54 - encode german audio stream to mp3 (128kbps) and aac (96kbps) (creating 2 audio streams in the output)
    55 - drop english audio stream
    56 - copy subtitle stream
     45= Examples =
    5746
    58 This can be done using the following FFmpeg command line:
     47== Choose all streams ==
     48
     49Single input:
    5950
    6051{{{
    61 ffmpeg -i input.mkv \
    62     -map 0:0 -map 0:1 -map 0:1 -map 0:3 \
    63     -c:v copy \
    64     -c:a:0 libmp3lame -b:a:0 128k \
    65     -c:a:1 libfaac -b:a:1 96k \
    66     -c:s copy \
    67     output.mkv
     52ffmpeg -i input.avi -map 0 output.mkv
    6853}}}
    6954
    70 ''Note there is no "-map 0:2" and that "-map 0:1" has been specified twice.''
    71 
    72 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.
    73 
    74 So, our output will now have the following streams:
     55Multiple inputs:
    7556
    7657{{{
    77 Output #0, matroska, to 'output.mkv':
    78     Stream #0:0(eng): Video ...
    79     Stream #0:1(ger): Audio ...
    80     Stream #0:2(ger): Audio ...
    81     Stream #0:3(ger): Subtitle ...
     58ffmpeg -i input0.mkv -i input1.mp4 -i input2.wav -map 0 -map 1 -map 2 output.mkv
    8259}}}
    8360
    84 After we selected which streams we would like in our output, using "-map" option, we specified codecs for each stream in our output.
    85 
    86 Video and subtitle stream have just been copied and german audio stream has been encoded to 2 new audio streams, mp3 and aac.
    87 
    88 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.
    89 
    90 ''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 (which is also mapped to the input stream `#`0:1), etc.''
    91 
    92 The result will be:
     61Using [https://ffmpeg.org/ffmpeg.html#Stream-copy stream copy] (re-mux) to avoid re-encoding:
    9362
    9463{{{
    95 Output #0, matroska, to 'output.mkv':
    96     Stream #0:0(eng): Video ...
    97     Stream #0:1(ger): Audio ...
    98     Stream #0:2(ger): Audio ...
    99     Stream #0:3(ger): Subtitle ...
    100 Stream mapping:
    101   Stream #0:0 -> #0:0 (copy)
    102   Stream #0:1 -> #0:1 (dca -> libmp3lame)
    103   Stream #0:2 -> #0:2 (dca -> libfaac)
    104   Stream #0:3 -> #0:3 (copy)
     64ffmpeg -i input.avi -map 0 -c copy output.mkv
    10565}}}
    10666
    107 == Example 2 ==
    108 
    109 Let's say that we want to reorder input streams backwards, so that we have output like this:
     67== Video streams only ==
    11068
    11169{{{
    112     Stream #0:0(ger): Subtitle: text (default)
    113     Stream #0:1(eng): Audio: dts (DTS), 48000 Hz, 5.1(side), s16, 1536 kb/s
    114     Stream #0:2(ger): Audio: dts (DTS), 48000 Hz, 5.1(side), s16, 1536 kb/s (default)
    115     Stream #0:3(eng): Video: h264 (High), yuv420p, 1920x800, 23.98 fps, 23.98 tbr, 1k tbn, 47.95 tbc (default)
     70ffmpeg -i input.mp4 -map 0:v output.mp4
    11671}}}
    11772
    118 This can simply be done using the following command line:
     73== Audio streams only ==
    11974
    12075{{{
    121 ffmpeg -i input.mkv -map 0:3 -map 0:2 -map 0:1 -map 0:0 -c copy output.mkv
     76ffmpeg -i input.mp4 -map 0:a output.mp4
    12277}}}
    12378
    124 ''Note that we specified all the input streams, but in the reverse order, which causes that order to be respected in the output.''
     79== A specific video stream only ==
    12580
    126 The option "-c copy" tells FFmpeg to use "copy" on all streams.
    127 
    128 == Example 3 ==
    129 
    130 If we want to extract only audio streams, from the same input file, then we can do it like this:
     81Choose video stream !#5:
    13182
    13283{{{
    133 ffmpeg -i input.mkv -map 0:1 -map 0:2 -c copy output.mkv
     84ffmpeg -i input.mkv -map 0:v:4 output.mp4
    13485}}}
    13586
    136 == Example 4 ==
     87Note that counting starts from 0.
    13788
    138 If we want to re-encode just the video streams, but copy all the other streams (like audio, subtitles, attachments, etc), we might use something like this:
     89== Video and audio from different files ==
     90
    13991{{{
    140 ffmpeg -i input.mkv -map 0 -c copy -c:v mpeg2video output.mkv
    141 }}}
    142 It will tell ffmpeg to:
    143 - read the input file 'input.mkv'
    144 - select all the input streams (first input = 0) to be processed (using "-map 0")
    145 - mark all the streams to be just copied to the output (using "-c copy")
    146 - mark just the video streams to be re-encoded (using "-c:v mpeg2video")
    147 - write the output file 'output.mkv'
    148 
    149 == Example 5 ==
    150 You can use the "-map" command in creating multiple output files, ex:
    151 {{{
    152 ffmpeg -i input.mkv -map 0:1 -map 0:2 audios_only.mkv -map 0:0 video_only.mkv
     92ffmpeg -i video.mp4 -i audio.m4a -map 0:v -map 1:a output.mp4
    15393}}}
    15494
    155 The default is to map the "highest quality video" and "highest quality audio" to each output file (basically reuses it for each output), see also [[Creating multiple outputs]].
     95== Everything except audio ==
    15696
    157 == Example 6 ==
    158 You can use the named output of a filtergraph as a mapping parameter, ex:
     97Using negative mapping:
     98
    15999{{{
    160 ffmpeg -i INPUT -filter_complex "[0] scale=100x100[smaller_sized]"  -map "[smaller_sized]" out.mp4
    161 }}}
    162 which (in our example) is the same as specifying the stream more precisely, and equivalent to this
    163 {{{
    164 ffmpeg -i INPUT -filter_complex "[0:0] scale=100x100[smaller_sized]"  -map "[smaller_sized]" out.mp4
     100ffmpeg -i input.mp4 -map 0 -map -0:a output.mp4
    165101}}}
    166102
    167 == Example 7 ==
     103== Specific language ==
    168104
    169 There are stream selection shortcuts you can also use like "0:v"
     105Include all video and all streams with Spanish language metadata:
     106
    170107{{{
    171 ffmpeg -i input -map 0:v -map 0:a output.mkv # chooses video and audio from input 0
    172 }}}
    173 See [http://ffmpeg.org/ffmpeg.html#Stream-specifiers stream specifiers] for details.
    174 
    175 == Example 8 ==
    176 MPEG stream selection:
    177 
    178 The tricky part with selecting from MPEG TS is it may contain multiple streams/channels, and if you are receiving "live data" just specifying an index might not work because it can change from run to run, so
    179 {{{
    180 ffmpeg -i INPUT -map 0:6 OUTPUT # might not work the same every run, DO NOT USE THIS WAY!
     108ffmpeg -i input.mkv -map 0:v -map 0:m:language:spa output.mp4
    181109}}}
    182110
    183 Assuming your file is MPEG, you can run "ffmpeg -i INPUT" (not specify an output) to see what program id's and stream id's it contains, like this example (probesize and analyzeduration specified to help "make sure" it picks up all the streams in it, may not be always needed)
    184 {{{
    185 $ ffmpeg -probesize 50M -analyzeduration 50M -i INPUT
    186 ...
    187 Input #0, mpegts, from 'INPUT':
    188   Duration: N/A, start: 22159.226833, bitrate: N/A
    189   Program 1344
    190     Metadata:
    191       service_name    : 7 Digital
    192       service_provider: Seven Network
    193     Stream #0:0[0x401]: Video: mpeg2video (Main) ([2][0][0][0] / 0x0002), yuv420p(tv), 720x576 [SAR 64:45 DAR 16:9], max. 14950 kb/s, 25 fps, 25 tbr, 90k tbn, 50 tbc
    194     Stream #0:1[0x402](eng): Audio: mp2 ([3][0][0][0] / 0x0003), 48000 Hz, stereo, s16p, 256 kb/s
    195   Program 1346
    196     Metadata:
    197       service_name    : 7TWO
    198       service_provider: Seven Network
    199     Stream #0:3[0x406]: Unknown: none ([5][0][0][0] / 0x0005)
    200     Stream #0:6[0x421]: Video: mpeg2video (Main) ([2][0][0][0] / 0x0002), yuv420p(tv), 720x576 [SAR 64:45 DAR 16:9], max. 14950 kb/s, 25 fps, 25 tbr, 90k tbn, 50 tbc
    201     Stream #0:7[0x422](eng): Audio: mp2 ([3][0][0][0] / 0x0003), 48000 Hz, stereo, s16p, 192 kb/s
    202     Stream #0:8[0x424](eng): Subtitle: dvb_teletext ([6][0][0][0] / 0x0006), 492x250
    203     Stream #0:4[0x499]: Unknown: none ([11][0][0][0] / 0x000B)
     111See [https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes List of ISO 639-1 codes] for the 3 letter language names.
     112
     113== Choose outputs from filters ==
     114
     115In this example the filtered video is named `[v]` and the filtered audio is named `[a]`:
     116
     117{{{
     118ffmpeg -i input.mp4 -i logo.png -i audio.mp3 -filter_complex "[0]scale=1280:-1[bg];[bg][1]overlay[v];[2:a]volume=-3dB[a]" -map "[v]" -map "[a]" output.mp4
    204119}}}
    205 you could specify desired streams by program id:
     120
     121You can use almost any arbitrary names to label the filter outputs.
     122
     123== Different outputs ==
     124
     125Video to `video.mp4`, audio !#2 to `audio_2.mp3`, audio !#3 to `audio_3.wav`:
     126
    206127{{{
    207 ffmpeg -i INPUT -map 0:p:1344 OUTPUT # pulls in both inputs from program 1344, channel "7 Digital" in this case
     128ffmpeg -i input.mkv -map 0:v video.mp4 -map 0:a:1 audio_2.mp3 -map 0:a:2 audio_3.wav
    208129}}}
    209 or specify a child stream:
     130
     131Note that counting starts from 0.
     132
     133== Conditional mapping ==
     134
     135Adding a trailing question mark (`?`) to `-map` will ignore the mapping if the stream does not exist.
     136
     137To map the video and audio streams and ignore the audio mapping if no audio streams exist:
     138
    210139{{{
    211 ffmpeg -i INPUT -map i:0x401 OUTPUT # pulls in the single input stream with PID (MPEG Packet ID [stream identifier]) 0x401 from wherever it is found, in this case its a video stream in "7 Digital"
     140ffmpeg -i input.webm -map 0:v -map 0:a? output.mkv
    212141}}}
    213 or similar, see other specifier examples.   Note if you have "unknown" streams in there you may need to add the -ignore_unknown flag as well.
    214142
    215 Also note that if your input stream contains multiple program id's, you can record simultaneously from various of them, using the same FFmpeg instance and the map commands described here.
     143If `-map 0:a` is used instead, and there are no audio streams, then `ffmpeg` will fail with `Stream map '0:a' matches no streams. To ignore this, add a trailing '?' to the map.`.
    216144
    217 == Example 9 ==
    218 Include "all" inputs to the output.  The default behavior is to copy only one audio and one video channel.  If you want to copy 'all' channels, use "-map"
     145== Re-order streams ==
     146
     147The order of your `-map` options determines the order of the streams in the output. In this example the input file has audio as stream !#0 and video as stream !#1 (which is possible but unusual). Example to re-position video followed by audio:
     148
    219149{{{
    220 ffmpeg -i input -map 0 output.mp4 # re encodes all video and audio channels from input one
    221 ffmpeg -i input -map 0 -c copy output.mp4 # copies all video and audio channels from input one to output, not just one video
     150ffmpeg -i input.mp4 -map 0:v -map 0:a -c copy output.mp4
    222151}}}
     152
     153This example [https://ffmpeg.org/ffmpeg.html#Stream-copy stream copies] (re-mux) with `-c copy` to avoid re-encoding.
     154
     155= See also =
     156
     157* [https://ffmpeg.org/ffmpeg.html#Advanced-options -map option documentation]