Code for Concinnity


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 -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 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)”.

-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

1
$ 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:

1
2
3
# Untested
# 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

Cheers :)

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

12 Responses to “Muxing audio and video with ffmpeg”

  1. 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



  2. @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



  3. 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



  4. 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



  5. 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



  6. 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



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



  8. 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



  9. Thank you very much! It helps me a lot

    Comment by Tomas on December 26, 2012 at 11:48 pm



  10. 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



  11. I am trying to add additional audio tracks to the video like many languages:

    ffmpeg -i “F:\Video.mp4″ -i “F:\Audio.MP3″ -vcodec copy -acodec copy -shortest -f flv -map 0:0 -map 1:0 rtmp://IP/input.stream

    But at the output I get the video without original audio with only mp3 audio, I cannot choose the audio track (original or additional mp3). Where could be the problem?

    Comment by Martins on August 29, 2013 at 8:54 pm



  12. @Martins

    1. The last parameter should be the output file name, which I think should not be

      rtmp://IP/input.stream
      . I don’t know if ffmpeg can stream directly from rtmp://. I guess you should download the files locally using a tool such as RTMPDump

    2. If your MP3 files has multiple tracks, you can do it like -map 0:0 -map 1:0 -map 1:1 -map 1:2, etc. We can see this from man ffmpeg:

    1
    2
    3
    -map
    [-]input_file_id[:stream_specifier][,sync_file_id[:stream_specifier]] |
    [linklabel] (output)

    Comment by kizzx2 on August 29, 2013 at 10:46 pm



Leave a Reply