How to merge audio and video file in ffmpeg

178

98

I want to merge an audio file (.wav or .au format) with a video file (.mp4 format).

Please suggest me how to achieve this. I want to merge these file to new .mp4 video file. An ffmpeg command would be very welcome.

Sandy

Posted 2011-05-01T07:45:34.087

Reputation: 2 309

There's a decent article here that might be helpful too... http://cfc.kizzx2.com/index.php/muxing-audio-and-video-with-ffmpeg/

– Simon East – 2014-02-08T02:57:17.433

Answers

286

Merging video and audio, with audio re-encoding

See this example, taken from this blog entry but updated for newer syntax. It should be something to the effect of:

ffmpeg -i video.mp4 -i audio.wav \
-c:v copy -c:a aac -strict experimental output.mp4

Here, we assume that the video file does not contain any audio stream yet, and that you want to have the same output format (here, MP4) as the input format.

The above command transcodes the audio, since MP4s cannot carry PCM audio streams. You can use any other desired audio codec if you want. See the AAC Encoding Guide for more info.

If your audio or video stream is longer, you can add the -shortest option so that ffmpeg will stop encoding once one file ends.

Copying the audio without re-encoding

If your output container can handle (almost) any codec – like MKV – then you can simply copy both audio and video streams:

ffmpeg -i video.mp4 -i audio.wav -c copy output.mkv

Replacing audio stream

If your input video already contains audio, and you want to replace it, you need to tell ffmpeg which audio stream to take:

ffmpeg -i video.mp4 -i audio.wav \
-c:v copy -c:a aac -strict experimental \
-map 0:v:0 -map 1:a:0 output.mp4

The map option makes ffmpeg only use the first video stream from the first input and the first audio stream from the second input for the output file.

Patrick Georgi

Posted 2011-05-01T07:45:34.087

Reputation: 3 436

Very sweet command lines... – Chandra Nakka – 2014-09-03T04:37:38.380

Great, I used ffmpeg -i video.mp4 -i audio.wav
-c:v copy -c:a aac -strict experimental output.mp4 to merge MP3 with Wav file...=> mp4 5 minutes, rendered 30 seconds..Thanks
– sonida – 2015-05-24T18:55:45.640

About the "Replacing audio stream", will it work even if there is no audio in the video file? And how can I make the audio loop in case it's too short compared to the input video? I've also noticed that if the audio is longer than the video, the output video will be as long as the audio file. Is there a way to overcome it? – android developer – 2019-02-24T12:35:44.607

Many thanks for the -strict experimental, because all the articles and answers I've found before just told me to use -c:v copy and it always just gave me an error that copy decoder doesn't exist. – coladict – 2019-10-28T13:13:02.177

I couldn't get -map 1:a:0 working to replace the audio stream, despite ensuring that the inputs were in the same order. So, I just removed the audio ffmpeg -i input.mp4 -c copy -an output_an.mp4, then I executed the first option with reencoding and it worked. – Yamaneko – 2020-01-28T17:42:09.450

I tried it but ffmpeg is freezing while merging – Sandy – 2011-05-01T11:41:13.410

4i don't think mp4 containers can have wav audio streams. Try ffmpeg -i audio.wav -i video.mp4 -acodec copy -vcodec copy -f mkv output.mkv – Luke – 2013-03-12T23:29:28.377

1I am using this and it is replacing the origional audio from the input audio. What if I want to merge input audio with the audio of input video? – Iqbal Malik – 2014-01-31T07:00:07.707

16

Since I am not allowed to write comments to the first answer with my reputation, an addendum here, because I had this problem when encoding webms.
If your audio stream is for example longer than the video stream, you have to cut it or otherwise you will have the last video frame as a still image and audio running.

To cut either stream, you can use -ss [hh:mm:ss] -t [ss] before each of the -i "file.ext".
-ss [...] will define the starting point to cut
-t [...] will define the length of the segment in seconds

Example:

ffmpeg.exe -ss 00:00:10  -t 5 -i "video.mp4" -ss 0:00:01 -t 5 -i "music.m4a" -map 0:v:0 -map 1:a:0 -y out.mp4

user136036

Posted 2011-05-01T07:45:34.087

Reputation: 286

9You can also just use -shortest to cut the end. – nyuszika7h – 2016-03-25T15:22:55.527

8

-open command promt (windows+r ->cmd->enter)

-then go to inside the folder where you have audio and video file

apply this cmd

ffmpeg -i "videoFile.mp4" -i "audioFile.mp3" -shortest outPutFile.mp4

Enter....you will get a new file named outPutFime.mp4 (a merged file of audio and video)

UniCoder

Posted 2011-05-01T07:45:34.087

Reputation: 189

3It's really not necessary to describe how to use the command line here. It's pretty much implied that users will be able to figure that out. Especially since the asker specified FFmpeg. All the same, the -shortest tag is very nifty. Much simpler than in @user136036's answer. – Kat – 2015-05-14T17:08:56.847

3This will re-encode everything, which is probably not what is wanted. – mivk – 2015-11-02T22:00:13.323

http://adaptivesamples.com/how-to-install-ffmpeg-on-windows/ – UniCoder – 2016-12-08T12:24:32.953

'ffmpeg' is not recognized as an internal or external command, – UniCoder – 2016-12-08T12:24:45.763

You have to add ffmpeg to PATH http://www.wikihow.com/Install-FFmpeg-on-Windows

– Arete – 2017-03-27T16:14:04.983

if you are inside the same folder where your file lies , need not to add path – UniCoder – 2017-05-11T12:34:12.613

This was the only solution that worked for me on my mac. I'm trying to combine an M2V file with a WAV file. – Scott Biggs – 2018-07-16T05:06:01.800

The -shortest tag was very useful thanks. – himanshuxd – 2020-01-17T23:36:47.473

5

This worked for me:

ffmpeg.exe -i AudioT.m4a -i VideoT.mp4 -acodec copy -vcodec copy muxed.mp4

Ronald

Posted 2011-05-01T07:45:34.087

Reputation: 241

1its partially working for me, not working for all inputs, some resulting input videos remains same in the output – Narendra Singh – 2016-09-24T11:13:35.333

Detailed: https://superuser.com/questions/590201/add-audio-to-video-using-ffmpeg

– Vadzim – 2018-01-03T21:16:21.257

1

Try using mencoder (Yes it is ffmpeg based, but you never know). I use the -audiofile argument. I generally use ffmpeg, though, so take this advice with a pinch of salt.

And, if you use Windows, Mediacoder (not open source anymore sadly) works... its basically a frontend for a lot of gnu encoders and a few non-free ones.

Wyatt8740

Posted 2011-05-01T07:45:34.087

Reputation: 943