Forward from White Wolf Stack: See the original article

From the operation of the stream (a) video to audio caused by the bloody case, we know that the stream selection, actually there are two ways, one is FFMPEG automatic selection, one is to set parameters manual selection.

For automatic selection, FFMPEG default selection rules are as follows:

  1. Video Streaming: The highest resolution stream is selected by default
  2. Audio Streams: The stream with the most channels is selected by default
  3. Captioning stream: By default select the captioning stream supported by the first captioning encoder

For video streams and audio streams, if the resolution is equal or the channel is equal, the first shall prevail. Data streams and accessory streams do not support automatic selection. Manual selection is required.

In the case of automatic selection, FFMPEG will only select one path of each type, for example

ffmpeg -i r3.mp4 -hide_banner Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'r3.mp4': ... Duration: 00:00:58.54, start: 0.000000, bitrate: 1998KB /s Stream #0:0(und): Video: Duration: 00:00:58.54, start: 0.000000, bitrate: 1998KB /s Stream #0:0(und): Video: H264 (HIGH) (AVC1/0x31637661), YUV420P, 544x960, 1732 KB /s, 29.83 FPS, 29.83 TBR, 11456 TBN, 59.67 TBC (default) Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 129 kb/s (default) Stream #0:2(und): Audio: mp3 (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s (default)

Note: STREAM #0:0, where the first 0 represents the first input file and the second 0 represents the first Stream from the first input file

If we transcode r3.mp4 directly, you will find that the output video only retains one video and one audio.

1, FFMPEG-I TMP-R3. MP4 2, FFMPEG-I TMP-R3. MP4 Stream #0:0(und): Video: H264 (HIGH) (AVC1/0x31637661), YUV420P, 544x960, 1684 KB /s, 29.83 FPS, 29.83 TBR, 11456 TBN, 59.67 TBC (Default) Stream #0:1(und): Audio: AAC (LC) (MP4A / 0x6134706D), 44100 Hz, STEREO, FLTP, 129 KB /s (Default)

This is the result of FFMPEG’s automatic selection.

If you want to enter Stream #0:2 of the video, you have to specify it manually.

Manual mode of the stream, using the -map parameter, which is very important and we’ll use it again and again. It indicates which streams we need to select from the input files to the output files.

Grammar rules:

-map [-]input_file_index[:stream_type_specifier][:stream_index]
  1. The brackets [] indicate optional, and the – before input_file_index indicates negative selection, i.e. a path can be excluded.
  2. Input_file_index refers to an input file, we can use subscript 0 for the first input file, 1 for the second input file, and so on;
  3. Stream_type_specifier (optional) refers to the type of stream that specifies the input file. We use A, V, S, D, T for Audio, Video, Subtitle, Data, and Accessory streams.
  4. Stream_index (optional) refers to a specific type of stream.

We will continue to use the video footage from Case 1 as an example. (If you haven’t downloaded it, you can click here.

If we take r1ori. Mp4 as the input and want to get the result of r3.mp4, that is, there is one more audio stream and it is in mp3 format, what do we do?

ffmpeg -i r1ori.mp4 -map 0:v -map 0:a:0 -map 0:a:0 -c:v copy -c:a:0 copy -c:a:1 libmp3lame -y r3.mp4

Note the Stream mapping section of the output code segment

Stream mapping: 
Stream #0:0 -> #0:0 (copy) 
Stream #0:1 -> #0:1 (copy) 
Stream #0:1 -> #0:2 (aac (native) -> mp3 (libmp3lame))

So a little bit of analysis

  1. Since there is only one input file, the first argument to -map is all 0
  2. -map 0:v = select all streams from the input file to the output, -c:v = copy all streams from the input file, -c:v = copy
  3. -map 00 = -map 00 = -map 00 = -map 00 = -map 00 = -map 00 = -map 00 = -map 00 = -map 00 = -map 00 = -map 00 = -map 00 = -map 00 = -map 00 = -map 00 = -map 00 = -map 00
  4. -map 0: v-map 00 -map 00 -map 00 -map 00 -map 00
  5. Stream #0:1 -> #0:1 (copy); -C1 libmp3lame Recencode the libmp3lame encoder for the second audio Stream, i.e., Stream #0:1 -> #0:2 (AAC (Native) -> mp3 (libmp3lame) during output)

We’ll be using the -map command a lot later, so it’s important to understand this analysis.

The above command is also equivalent to

ffmpeg -i r1ori.mp4 -map 0:0 -map 0:1 -map 0:1 -c:v copy -c:a:0 copy -c:a:1 libmp3lame -y r3.mp4

-map 0:v is the same as -map 0:0, -map 00 is the same as -map 0:1, because in r1ori.mp4 #0:0 is the video stream and #0:1 is the audio stream

ffmpeg -i r1ori.mp4 
... 
Stream #0:0(und): Video: h264 
Stream #0:1(und): Audio 
...

Note: -Map parameters are for input streams, because we want to select streams from the input stream to the output; The -c option is no longer an input stream, but the -c option is an output stream selected by -map.

In addition, let’s look at a few simple examples that you can try out before you see the results

Copy all streams from the input file to the output. The output can be more than just two streams

ffmpeg -i r3.mp4 -map 0 -c copy output.mp4

2. Disassemble the three streams of the input file and output three files

ffmpeg -y -i r3.mp4 -map 0:v -c:v copy output-silent.mp4 \
-map 0:a:0 -c:a copy output-audio.aac \
-map 0:a:1 -c:a copy output-audio.mp3

We can also set some options, such as setting R3. MP4 two audio streams have different bit rates.

ffmpeg -i r3.mp4 -b:a:0 32k -b:a:1 64k -map 0 -y r4.mp4

-map 0 is required, otherwise the output will not output two audio streams.

There are many options for audio and video streaming Settings, you can refer to the official documents to understand the specific, you can also refer to more information in our usual practice, lay the foundation.

Finally, we have one of the most common operations we can do with a filter.

For example, scale the original video R3. MP4 and so on to double

ffmpeg -i r3.mp4 -vf scale=272:480 -y filter.mp4

We can also manually select stream processing

ffmpeg -i r3.mp4 -filter_complex "[0]scale=272:480[out]" -map 0:a -map "[out]" -y filter.mp4

The above two commands may be difficult to understand, about the filter, we first have an impression, the following article we will make a detailed introduction.

The operation of the stream we introduced here, we encountered all kinds of inexplicable problems in the practice, although the message.