125

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.

178

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.

9
  • 15
    or directly -c copy to just re-mux audio and video without re-encoding any of the streams ^^ Sep 3 '18 at 6: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
  • Works like charm!!
    – Trect
    Aug 26 '19 at 11:32
  • 12
    @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. Apr 1 '20 at 19:16
  • 1
    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 at 8:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.