« ffmpeg stream selection

1ffmpeg -v error -y -i multitrack.mp4 -to 1 multitrack-ls.mp4
  • We want to produce a one second cut from the sample file multitrack.mp4 . It has one video stream and two audio streams.
  • You can see that we are using the -to option to specify that we want to stop after outputting just one second.
  • Now, if we ffprobe the outputs.
1ffprobe -v error multitrack-ls.mp4 -show_format -show_streams
  • We see that it has a video stream and an audio stream.
  • But the input had two audio screens, but the output does not have all the audio streams from the input. This is because by default, if you do not specify streams with the -map option, it will automatically choose only one video and one audio stream for the output based on some criteria like video resolution number of audio channels, etc..
1ffmpeg -v error -y -i multitrack.mp4 -to 1 -map 0 multitrack-ls.mp4
  • If we want all the streams you can just specify -map 0 in the previous command. Running the same shows that we have both the audio streams in the output now.
1ffmpeg -v error -y -i multitrack.mp4 -to 1 -map 0:v multitrack-ls.mp4
  • Say, we want to take only the video from this sample file to produce a different video only mp4. We can do this by just adding a v stream type to the selector.
1ffprobe -v error multitrack-ls.mp4 -show_format -show_streams
  • Running the ffprobe on the output confirms that the output indeed has just a single video stream and no audio streams.
1ffmpeg -v error -y -i multitrack.mp4 -to 1 -map 0:0 multitrack-ls.mp4
  • We could achieve the same output if we just specify 0 as the stream index instead of the stream type.
1ffmpeg -v error -y -i multitrack.mp4 -to 1 -map 0:a multitrack-ls.mp4
  • Similarly, we can choose to produce an audio only file containing all the audio streams by adding the stream type of a.
1ffprobe -v error multitrack-ls.mp4 -show _format -show_streams
  • We can see that the output contains two audio streams.
1ffmpeg -v error -y -i multitrack.mp4 -to 1 -map 0:1 multitrack-ls.mp4
  • We can choose a single audio stream by specifying 0:1 to -map.
1ffmpeg -v error -y -i multitrack.mp4 -to 1 -map 0:a:0 multitrack-ls.mp4
  • We can choose first audio stream by specifying 0:a:0 to -map.
1ffmpeg -v error -y -i multitrack.mp4 -i cow_4k.mp4 -to 4 -map 1:v:0 -map 0:a:1 multitrack-ls.mp4
  • We can use two or more input files. Here I am taking video from the second file and audio from the first.