9

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?

LiveWireBT
  • 28,763
user2469202
  • 213
  • 1
  • 2
  • 3

1 Answers1

16

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.

karel
  • 114,770
thom
  • 7,542
  • Thank you for the fast reply. However none of the commands resulted audible sound with totem. – user2469202 Nov 02 '13 at 16:11
  • I tested the commands before I committed my answer to make sure that it would work. Therefore I suspect the problem must be something else. Could you please check your inputfiles and outputfile in totem and in mediainfo ? Also if you did got any error messages would you be so kind to post them ? – thom Nov 02 '13 at 16:19
  • I am sure the error is at me and I am very graceful for helping me. I have tried copy here the outputs, but tthose are too large so i put there: http://evoran.hu/tmp/mediainfo - And thanks agaig for your help. – user2469202 Nov 02 '13 at 16:29
  • Yes, I see you have issued both ffmpeg commands. The first command worked and gave you the valid file. The second command had the mapping wrong and gave you a file without sound and, of course, did overwrite the first file. So please just invoke the first command and forget about the second at the moment. – thom Nov 02 '13 at 17:13
  • Unfortunately it was not overwritten. After the 1st attempt I have started totem to check the result. What is strange, the newvideo.mp4 is reported by mediainfo as an mp4 containing aac audio. However totem plays no sound. Here is the resulted mp4: evoran.hu/tmp/newvideo.mp4 (Rather large, 39 MB) – user2469202 Nov 02 '13 at 17:49
  • Really nice !! i like your video :-) it plays perfectly in VLC (which shows that it worked) but not in Totem. Now to get to the bottom of this, I compared it to my testfile and the audioparameters are the same so you encoded it well. The reason that Totem chokes on it is because your video is about 4:24 min and your audio is 30 seconds. After correcting this difference, Totem played it with sound and all :-) – thom Nov 02 '13 at 18:21
  • Thank you for your pathience and for the help. I am glad you have liked the promotion - I am a one-man company so I had to do everything and I am not familiar with video editing. Unfortunately the version I uploaded still contains a lot of grammaticac errors, but those seem to be negigable fro point of understanding. Thank you very-very much for yor kind help! - Steve – user2469202 Nov 02 '13 at 19:10
  • @LiveWireBT: Thanks for the valuable additions :-) – thom Nov 24 '13 at 03:47
  • Is there a way to add a short audio in the middle (at a particular time) of a much longer video? – AlwaysLearning Aug 01 '16 at 12:12