How to add an audio file (aac or mp3) to a mp4 video file?
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.