407

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.

bummi
  • 1,703
  • 4
  • 16
  • 28
Sandy
  • 4,629
  • 9
  • 27
  • 27
  • 1
    There's a decent article here that might be helpful too... http://cfc.kizzx2.com/index.php/muxing-audio-and-video-with-ffmpeg/ – Simon E. Feb 08 '14 at 02:57
  • Simon East's comment's link from web archive - http://web.archive.org/web/20150103030913/https://cfc.kizzx2.com/index.php/muxing-audio-and-video-with-ffmpeg/ – dav Jan 21 '22 at 10:57
  • Same question on SO: [FFMPEG mux video and audio (from another video) - mapping issue - Stack Overflow](https://stackoverflow.com/questions/12938581/ffmpeg-mux-video-and-audio-from-another-video-mapping-issue) – user202729 May 15 '22 at 14:58

7 Answers7

652

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 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 FFmpeg Wiki: 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 -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.

llogan
  • 57,139
  • 15
  • 118
  • 145
Patrick Georgi
  • 7,116
  • 1
  • 16
  • 6
  • I tried it but ffmpeg is freezing while merging – Sandy May 01 '11 at 11:41
  • 7
    i 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 Mar 12 '13 at 23:29
  • 1
    I 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 Jan 31 '14 at 07:00
  • 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 May 24 '15 at 18:55
  • 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 Feb 24 '19 at 12:35
  • 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 Oct 28 '19 at 13:13
  • 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 Jan 28 '20 at 17:42
  • If I know the audio is longer and all I want is 3 seconds of Fade In and Fade Out. Can I do it? – Royi Mar 26 '20 at 01:48
  • For what it's worth, the command line used by `youtube-dl` to ask `ffmpeg` to merge a pair of video and audio files is `ffmpeg -i video.mp4 -i audio.wav -c copy -map 0:v:0 -map 1:a:0 output.mkv`, according to [its source code](https://github.com/ytdl-org/youtube-dl/blob/2020.06.16.1/youtube_dl/postprocessor/ffmpeg.py#L521). The presence or absence of those `-map 0:v:0 -map 1:a:0` parameters do not seem to make a difference based on my recent attempt, though. – RayLuo Jul 05 '20 at 07:07
  • using -an before -i will ignore the audio stream and -vn before -i will ignore the video stream and I gues it will make it faster telling ffmpeg not to decode unnecessary streams. – Alireza Rinan Dec 17 '20 at 06:11
  • Why would you use `-c:a aac` instead of `-c:a copy`? – Iulian Onofrei Apr 17 '21 at 19:48
  • How to actually merge with the existing audio? – dawid Jan 13 '22 at 21:48
  • My copy of ffmpeg doesn't like that "output" keyword. Removing it still worked fine. – CarlF May 15 '23 at 22:25
32

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"

Example with shortest:

ffmpeg -i "video.mp4" -i "music.m4a" -c:v copy -map 0:v:0 -map 1:a:0 -shortest "out.mp4"
user136036
  • 454
  • 4
  • 6
13

Open command promt (windows+R -> Cmd+Enter). Then go to inside the folder where you have audio and video file. Apply this command:

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

You will get a new file named outPutFile.mp4 (a merged file of audio and video)

mattbasta
  • 132
  • 7
UniCoder
  • 239
  • 2
  • 2
  • 3
    It'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 May 14 '15 at 17:08
  • 6
    This will re-encode everything, which is probably not what is wanted. – mivk Nov 02 '15 at 22:00
  • http://adaptivesamples.com/how-to-install-ffmpeg-on-windows/ – UniCoder Dec 08 '16 at 12:24
  • 'ffmpeg' is not recognized as an internal or external command, – UniCoder Dec 08 '16 at 12:24
  • You have to add ffmpeg to PATH http://www.wikihow.com/Install-FFmpeg-on-Windows – Arete Mar 27 '17 at 16:14
  • if you are inside the same folder where your file lies , need not to add path – UniCoder May 11 '17 at 12:34
  • This was the only solution that worked for me on my mac. I'm trying to combine an M2V file with a WAV file. – SMBiggs Jul 16 '18 at 05:06
8

This worked for me:

ffmpeg.exe -i AudioT.m4a -i VideoT.mp4 -acodec copy -vcodec copy muxed.mp4
Jeankowkow
  • 109
  • 6
falconR
  • 280
  • 2
  • 7
1

In case one would like to merge audio and video with different length and also to apply Fade In and Fade Out the following worked for me:

ffmpeg -i Video001.mp4 -i Audio001.mp3 -af afade=t=in:st=0:d=3,afade=t=out:st=47:d=4 -c:v copy -c:a aac -shortest Output.mp4

In my case above the video was of length 51 so I chose Fade In of length 3 [Sec] and Fade Out* of ~4 [Sec]. Since fading is applied by a filter it required transcoding of the audio. In the case above I chose aac encoding.

The answer is heavily based on the answer of @PatrickGeorgi.

Royi
  • 519
  • 1
  • 11
  • 24
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.

Wyatt Ward
  • 1,159
  • 10
  • 13
0

I've made a Python script to do this, you can try it if you want. Get it on github: https://github.com/mmakarov/replicator

Or look at video here: https://www.youtube.com/watch?v=4Uu1hS3-eQM

Replicator is a Python script to merge a few video files, transparent overlay, text areas with mp3 audio track. Final video will be exact same length as your audio track. Lenght of your video will be calculated automatically.

I've make this script in a try to merge any video sequence files with PNG transparent overlay, adding text areas on video, with mp3 audio track. Resulting video file are ready to upload on Youtube or any other video hosting services.

You can change it on your taste.

How to use it: 0. Open terminal and enter: git clone https://github.com/mmakarov/replicato...

  1. Put your video files to project directory as 'source1.mp4', 'source2.mp4', etc
  2. Put your PNG transparent image to project directory as 'overlay.png'
  3. Put your MP3 file to project directory as 'voice.mp3'
  4. in terminal run: python3 source-to-medium.py (here will be created 'medium.mp4')
  5. enter your text areas contents if needed ...it takes some time to convert source*.mp4 files to one medium.mp4
  6. in terminal run: python3 medium-to-fin.py ...it will caltulate final video length dividing voice.mp3.length() / medium.mp4.length() = silent_fin.mp4.length() ...also it will add voice.mp3 to silent_fin.mp4 as audio track
  7. Finally you've got youtube_ready.mp4 files

Warning: russian comments in sources!