ffmpeg - replace audio in video

53

16

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

Posted 2016-10-21T22:02:39.650

Reputation: 3 736

Answers

78

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.

qubodup

Posted 2016-10-21T22:02:39.650

Reputation: 3 736

2or directly -c copy to just re-mux audio and video without re-encoding any of the streams ^^ – Francesco Yoshi Gobbo – 2018-09-03T06:19:17.830

1Do 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 – 2019-06-15T19:34:07.230

Works like charm!! – Tessaracter – 2019-08-26T11:32:19.283

@Kostanos not automatically but I would like to. Please ask another question. You can reference this one to clarify that yours is new. – qubodup – 2019-09-04T09:08:40.773