Skip to content

How to Map Streams in FFMPEG

George Stoyanov edited this page Oct 25, 2017 · 3 revisions

Streams Mapping

The default behavior of ffmpeg is to choose the best video and audio stream in a file and drop the rest. All other input streams are in essence discarded. So in case you have a MPTS ts file you will need either to define which particular streams you want to be processed to the output or which programs. But first you need to check how many channels there are in the input file. This could be done using the following command:

ffmpeg -i input.ts

The output of this command will show you all the programs and streams available in the MPTS. In case of some unsupported streams you would need to use the -ignore_unknown or -copy_unknown to either drop out or copy the unknown streams

Specific Streams Mapping

ffmpeg -i input.ts -map 0:1 -map 0:2 -map 0:3 -map 0:4 -c copyoutput.ts

This command will map only the streams with ID 0:1, 0:2, 0:3 and 0:4 to the output ts and will drop everything else

Mapping of whole programs

ffmpeg -i input.ts -map 0:p:1 -map 0:p:2 -map 0:p:3 -map 0:p:4 -c copy output.ts

This command will map to the output only programs with ID 1, 2, 3 and 4 to the output ts.

Mapping of everything

ffmpeg -i input.ts -map 0 -c copy output.ts

This command will copy everything on the input and insert it in the output without re-encoding it. In some particular cases you should also . Please note that ffmpeg won't execute this command in case you have a stream which is scrambled. In that case you should use some of the above mentioned methods to exclude the scrambled streams or -copyts.

Multiplexing of three independent sources

ffmpeg -analyzeduration 100M -probesize 100M -i video.mp4 -i left.wav -i right.wav -map 0 -map 1 -map 2 -c copy video.mxf

This command will multiplex the video file video.mp4 and the two audio files into a new file: video.mxf

Audio Streams Mapping

The default behavior of ffmpeg is to choose the best video and audio stream in a file and drop the rest. All other input streams are in essence discarded. In case you want to control which streams are included, or include more than one stream in the output, then you will need to specify the "-map" command manually, and change these parameters.

MAP syntax

Input file:

In all following examples, we will use an example input file like this one:

# 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

Examples:

Now, let's say we want 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:

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:

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.

If we want to extract only audio streams, from the same input file, then we can do it like this: ffmpeg -i input.mkv -map 0:1 -map 0:2 -c copy output.mkv

References:

  1. Streams Mapping
  2. Map Audio Channels