Ubuntu – How to add an audio file (aac or mp3) to a mp4 video file

multimedia

I have an mp4 video file created from images and would like to add mp3 sound to it. I am not familiar with video editing at all, but read somewhwere that aac sound is preferred, so I got sound in aac form as well.

The preferred form of the result is also mp4 or flv. My Operating System is Ubuntu (Saucy Salamander).

I have Googled a lot for avconv and mencoder trying to transform the found examples to my case without any success.

Will you be kind enough any of you, experts, to drop me a command line example how can I do it?

Best Answer

It depends a bit how your streams are mapped within the mp4 file. Something like this should work:

ffmpeg -i video.mp4 -i audio.aac -map 0:0 -map 1:0 -vcodec copy -acodec copy newvideo.mp4

Usually the videostream is mapped as first stream in the container.
If it isn't mapped as first stream, change the mapping:

ffmpeg -i video.mp4 -i audio.aac -map 0:1 -map 1:0 -vcodec copy -acodec copy newvideo.mp4

To find out how the streams in your video file actually were mapped in the first place you can use mediainfo which is in the standard Ubuntu repositories nowadays.

If we break down the commandline here it works like this:

-i video.mp4  -> first media file
-i audio.aac  -> second media file
-map 0:1      -> use the second stream from the first mediafile
-map 1:0      -> use the first stream from the second mediafile
-vcodec copy  -> leave the video as is
-acodec copy  -> leave the audio as is
newvideo      -> resulting videofile

Make sure that the audiofile and the videofile have the same duration. Not every player is accepting tracks with huge duration differences.

Please be aware that avconv and ffmpeg are almost the same thing. In fact, this command using avconv also works like the same command using ffmpeg:

avconv -i video.mp4 -i audio.aac -map 0:0 -map 1:0 -vcodec copy -acodec copy newvideo.mp4  

avconv program belongs to the Libav project, a fork from the FFmpeg project. You also can safely ignore the following error message:

*** THIS PROGRAM IS DEPRECATED ***
This program is only provided for compatibility and will be removed in a future release. Please use avconv instead.

An alternative for muxing files in MP4 format is MP4Box from the gpac package.