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 7 8 9 10 | # 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 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
Cheers
Hi, If I want to mux this 2 files c:\test\testaudio.mp3 and c:\test\mainvideo.mp4 to get newvideo.mp4 using this codec -s 480×360 -vcodec libx264 -acodec libfaac -ac 1 -ar 32000 -r 25 -ab 32000 can you say me exactly how and where I have to set in winff? I tried but I made mistakes and it ask me to import at least one file.
Thank you. Angelo
Comment by Angelo on July 5, 2011 at 5:57 am
@Angelo,
I think you can follow my post using the command line ffmpeg instead of the GUI (Grahpical User Interface). There is probably the ffmpeg program somewhere in WinFF. For Windows, you should be looking at C:\Program Files\WinFF or something.
To get a command prompt going, type cmd in Win+R. Command line-fu will come with practice
Comment by kizzx2 on July 6, 2011 at 11:33 pm
If I’m understanding this correctly, the -map parameter can take an external audio track and replace an existing audio track in the video. That can be useful. I went through my video archives and encoded better quality tracks for my MKVs, but putting all that back together through a GUI was painful.
Comment by Sean on August 19, 2011 at 11:17 am
Just what I needed. I used ffmpeg to strip audio which was barely audible and then Audacity to adjust level. Thanks for letting me replacing audio without having to go through hoops.
to strip audio: ffmpeg -i SANY0001.MP4 -vn -ab 128000 audio.mp3
Comment by Raymond on September 6, 2011 at 9:49 am
Syntax has now changed in FFMpeg. To select streams to map it is now a “:” instead of a “.” (colon instead of full stop). So:
ffmpeg -i audio.mp3 -i video-with-audio.avi -acodec copy -vcodec copy output.avi -map 0:0 -map 1:0
Comment by Mark on February 22, 2012 at 3:34 am
Hi,
I did try this with the new syntax of using “:”, I got this error:
ffmpeg -i aud.mp3 -i vid_with_aud.mp4 -acodec copy -vcodec copy -map 0:0 -map 1:0 muxfile.mp4
Application provided invalid, non monotonically increasing dts to muxer in stream 0: 13420544 >= 13420416 av_interleaved_write_frame(): Invalid argument
Please advice.
Thanks & Regards, -Rajesh TN
Comment by Rajesh on May 2, 2012 at 2:08 pm
This is how to do with the new avconv:
avconv -i /tmp/ptichki_up.wav -i /media/CANON_DC/DCIM/147CANON/MVI_1849.AVI -acodec copy -vcodec copy -map 0 -map 1:v /tmp/pchela.avi
Read about incompatible changes compared to ffmpeg here: http://www.libav.org/changelog.html (version 0.8_beta1)
Comment by avalon on June 7, 2012 at 11:18 pm
Hi all. I have written a blog post with the new invocation on my technical blog. The command I have given there is:
ffmpeg -i in_audio.wav -i in_video.ogv -map 0:0 -map 1:1 -shortest \ -c:a libvorbis -q:a 7 -c:v copy output.ogv
Comment by Shlomi Fish on December 11, 2012 at 5:42 pm
Thank you very much! It helps me a lot
Comment by Tomas on December 26, 2012 at 11:48 pm
the syntax has changed. the -map option has to be BEFORE the “out.avi” . for example : ffmpeg -i input.avi -i input.mp3 -map 0:0 -map 1:0 -vcodec copy -acodec copy out.avi . in this example i used a video with existing audiostream and an audio only file. merged it successfully.
Comment by krrrrrz on February 22, 2013 at 3:34 am