171

How to replace the audio in a video file using an audio file using ffmpeg?

I imagine the command looks like:

ffmpeg -i v.mp4 -i a.wav -MAGIC video-new.mp4

This is very similar to How to replace an audio stream in a video file with multiple audio streams? but that question handles multiple audio tracks, which complicates it very much, making it unclear which part of the solution is enough for a simple audio swap.

qubodup
  • 8,804
  • 7
  • 36
  • 49

2 Answers2

263

You will want to copy the video stream without re-encoding to save a lot of time but re-encoding the audio might help to prevent incompatibilities:

ffmpeg -i v.mp4 -i a.wav -c:v copy -map 0:v:0 -map 1:a:0 new.mp4

-map 0:v:0 maps the first (index 0) video stream from the input to the first (index 0) video stream in the output.

-map 1:a:0 maps the second (index 1) audio stream from the input to the first (index 0) audio stream in the output.

If the audio is longer than the video, you will want to add -shortest before the output file name.

Not specifying an audio codec, will automatically select a working one. You can specify one by for example adding -c:a libvorbis after -c:v copy. You can also use -c copy to avoid re-encoding the audio, but this has lead to compatibility and synchronization problems in my past.

qubodup
  • 8,804
  • 7
  • 36
  • 49
  • 21
    or directly -c copy to just re-mux audio and video without re-encoding any of the streams ^^ – Francesco Yoshi Gobbo Sep 03 '18 at 06:19
  • 1
    Do you know how to convert the new audio on the same format (with the same parameters) as it is in original video? I mean, I need the new audios to be encoded in the same way as it was the old audio. – Kostanos Jun 15 '19 at 19:34
  • 2
    Works like charm!! – Trect Aug 26 '19 at 11:32
  • @Kostanos not automatically but I would like to. Please ask another question. You can reference this one to clarify that yours is new. – qubodup Sep 04 '19 at 09:08
  • @FrancescoYoshiGobbo - could you expand on that? How much of the rest of the command remains after -c copy? Obviously the new filename - what else? If you could provide a complete revised command, that would be helpful. – almcnicoll Apr 01 '20 at 16:06
  • 18
    @almcnicoll - if the audio files we are going to substitute have the same or compatible codec, the re-encoding is not necessary and a ffmpeg -i v.mp4 -i a.m4a -c copy -map 0:v:0 -map 1:a:0 new.mp4 would suffice. Or alternatively if the 2 streams are going to be muxed into an .mkv container, so on the code above we can substitute .m4a with the original .wav of the question and the new.mp4 with new.mkv. – Francesco Yoshi Gobbo Apr 01 '20 at 19:16
  • 4
    I agree with Francesco to use -c copy instead of -c:v copy. -c:v copy means to copy the video only, which means re-encoding the audio. -c copy will copy both the video and audio, no re-encoding. – wisbucky May 30 '21 at 08:36
  • worked on ffmpeg v4.3 win10. Thanks! – Rinaldo Jonathan Sep 18 '21 at 18:53
  • Interestingly when trying to put wav into my h265 file I get: Could not find tag for codec pcm_s16le in stream #1, codec not currently supported in container Could not write header for output file #0 (incorrect codec parameters – Jamie Hutber Mar 31 '22 at 13:51
  • Needed to pull out the audio from a video, de-noise it, and re-insert it. This was perfect. – Aaron R. Apr 22 '22 at 22:35
  • I tried but the video is not visible. what is the reason? – Harshil Kulkarni Jun 23 '22 at 04:48
  • -shortest didn't work for me. It still kept the video as long as the audio. -fflags shortest worked instead. – Iulian Onofrei Aug 21 '23 at 06:37
  • For some reason it have length from audio file. bur after video stream ends -- picture is on pause. – Bogdan Mart Feb 16 '24 at 20:49
1

If your video file contains subtitle tracks or multiple audio tracks, the above solution by @qubodup will discard the subtitle tracks but keep all the non-first audio tracks in the video. In that case, you should use the following command line as a more general solution:

ffmpeg -y -i v.mp4 -i a.m4a -c copy -map 0 -map -0:a -map 1:a video-new.mp4

This will replace all audio tracks in the video file v.mp4 by all audio tracks in the audio file a.m4a, keeping all other tracks including video tracks and subtitle tracks intact.

For better understanding, -map 0 selects all tracks (including all audio/video/subtitle tracks) from the first input file (index starts from 0); -map -0:a discard all audio tracks from the first input file; -map 1:a select all audio tracks from the second input file. In particular, 1:a:0 means the first (the last '0') audio track (the middle 'a') of the second input file (the first '1'), discarding the 3rd number (i.e., becomes 1:a) means all that type of tracks (audio tracks), discarding the 2nd number (i.e., becomes 1) mean all types of tracks. The -ve sign means subtraction.

For detailed explanation on FFMPEG's -map option, you can refer to FFMPEG Advanced Options

xuancong84
  • 141
  • 3