186

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
  • 9,394

3 Answers3

289

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 (or possibly -fflags 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
  • 9,394
6

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

0

[Sorry, but my reputation is insufficient for adding comments, so I try another answer, that actually refers to the previous answer by @xuancong84.]

Thanks for the hint, and especially the great explanation! - I would extend the answer to generalize it for more audio files. My command looks like

ffmpeg -i Input.mp4  -i Audio_1.mp4 -i Audio_2.mp4 -map 0 -map -0:a -map 1:a -map 2:a -c:v copy -shortest Output.mp4

If necessary, this can help to understand mapping for ever more input files; eventually combined with streams (tracks) within files, like already mentioned in said answer.

udippel
  • 141