Automatic stream selection (default): If you don’t specify any options for stream mapping, FFMpeg will try to match input streams with output streams based on their type and index. For example, if your first input file has a video stream at index 0 and an audio stream at index 1, and your first output file doesn’t have any streams yet, FFMpeg will map the video stream from input 0:v:0 to output 0:v:0 (the first video stream in output 0) and the audio stream from input 0:a:0 to output 0:a:0 (the first audio stream in output 0).
ffmpeg -i input1.mpg -i input2.avi -i input3.wav -map 0:v:0 -map 1:a:0 -map 2:a:0 -c copy output1.mkv output2.mov output3.mp4
In this example, we’re using the “-c copy” option to preserve the original format of each input stream in its corresponding output file. If you want to convert or transcode your streams instead, use other options like “-c:a libfdk_aac” for audio encoding or “-c:v libx264” for video encoding.
3. Stream selection with filters: FFMpeg also supports selecting and filtering specific input streams using various filters like “select”, “asetpts”, “setdar”, etc. For example, to select only the first 10 seconds of a video stream (index 0) from input file 2 (index 1), and output it to a new file named output.mp4:
ffmpeg -i input1.mpg -i input2.avi -filter_complex “select=’not(modulo(n,10))’ [v]” -map “[v]” output.mp4
In this example, we’re using the “-filter_complex” option to apply a filter graph that selects only every 10th frame (using the modulo operator) and outputs it to a new video stream named “v”. The resulting video stream is then mapped to output file using the “-map” option.
4. Stream selection based on metadata: FFMpeg can also select input streams based on their metadata, such as their duration or bitrate. For example, to select only the first 5 minutes of a video stream (index 0) with a bitrate greater than 1 Mbps from input file 3 (index 2):
ffmpeg -i input1.mpg -i input2.avi -i input3.mkv -filter_complex “select='(PTS-first_pts)>5*60 && bit_rate>1000000′ [v]” -map “[v]” output.mp4
In this example, we’re using the “-filter_complex” option to apply a filter graph that selects only video frames with PTS values greater than 5 seconds after the first frame (using the “first_pts” metadata) and bitrates greater than 1 Mbps. The resulting video stream is then mapped to output file using the “-map” option.
These are just a few examples of how FFMpeg’s stream selection methods can be used to manipulate input streams based on their properties or metadata. For more information, see the official documentation at https://ffmpeg.org/documentation.html#Stream_selection.