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 -acodec copy -vcodec copy output.avi -map 0.0 -map 1.0
# krrrrrz from the comments says that this is the new syntax
# (-map before other parameters)
$ ffmpeg -i audio.mp3 -i video-with-audio.avi -map 0.0 -map 1.0 -acodec copy -vcodec copy 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)”.
You can see a list of stream numbers by
$ ffmpeg -i audio.mp3 -i video-with-audio.avi
You can add additional streams to the output file with the
-newvideo parameters. Two audio streams make sense when you’re making a DVD rip with two sound tracks. I didn’t try it myself but it’s nice to know