Code for Concinnity

beautiful and elegant solutions


Muxing audio and video with ffmpeg

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

1
2
3
4
5
6
# 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

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 0.0 and 1.0 refers to the first input file and the second input file respectively.

The -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

1
$ ffmpeg -i audio.mp3 -i video-with-audio.avi

You can add additional streams to the output file with the -newaudio and -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 :P

Cheers :)

Published by kizzx2, on April 8th, 2010 at 6:10 pm. Filled under: Useful tips Tags: , , 4 Comments