ffmpeg is a great codec converter, but it’s wide array of options is really daunting. I just figured out it can also be used to mux video and audio together into a file, here’s how to do it
# Simple example: mux an audio with a video file without audio track
$ ffmpeg -i audio.mp3 -i video.avi -acodec copy -vcodec copy output.avi
# Daily usage example: mux an audio with a video file _with_ an existing audio track.
# This will replace the AVI file's audio track with the MP3
$ ffmpeg -i audio.mp3 -i video-with-audio.avi -map 0:0 -map 1:0 -acodec copy -vcodec copy -shortest output.avi
The key to the second command is the
-map parameter. Typically, the output file would contain two streams: one audio and one video. The numbers
1:0 refers to the first input file and the second input file respectively.
-map parameter is used to spell out the streams. What the above said was “use input stream 0:0 for your first output stream (which is an audio stream) and input stream 1:0 for your second output stream (which is the video stream)”.
-shortest keep the output file to the length of the shortest stream, not strictly required but usually what you want.
You can see a list of stream numbers by
$ ffmpeg -i audio.mp3 -i video-with-audio.avi
Untested: You should be able to add additional streams to the output file with additional
-map. Two audio streams make sense when you’re making a DVD rip with two sound tracks. For example:
# Suppose audio.mp3 has 3 tracks
$ ffmpeg -i audio.mp3 -i video-with-audio.avi -map 0:0 -map 0:1 -map 0:2 -map 1:0 -acodec copy -vcodec copy -shortest output.avi