日本語

Replace Audio in a Video with FFmpeg (-map, -shortest, apad)

​​FFmpeg​AAC,audio,ffprobe,video editing

To replace the audio of a video with another audio file in FFmpeg, pass two input files and select "the video from the first input" and "the audio from the second input" with -map 0:v -map 1:a. With -c copy, the streams are not re-encoded. In my test, replacing the audio of a 20-second video finished in 0.090 seconds. This is useful when you want to swap the audio of a recorded video for a narration or BGM recorded later (dubbing).

Note

The test environment was Ubuntu 24.04.5 with FFmpeg 6.1.1 (Ubuntu package 6.1.1-3ubuntu5). The video was a 20-second MP4 (640×360, H.264) generated from FFmpeg's test pattern (testsrc2). The original audio was a 440 Hz sine wave, and the replacement audio was an 880 Hz sine wave (both at 48 kHz). I did not test footage recorded with a camera or microphone, containers other than MP4 (such as MKV or MOV), FFmpeg 7 or later, or playback in video players.

Replace the audio by selecting streams with -map

Let the original video be video.mp4 and the replacement audio be new.m4a (AAC).

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

Inputs are numbered 0, 1, and so on, in the order given with -i. 0:v means "the video of input 0", and 1:a means "the audio of input 1". Only the streams selected with the -map option go into the output, so the original video's audio (0:a) is not included.

Diagram showing the video from video.mp4 and the audio from new.m4a selected with -map and written into replaced.mp4
-map 0:v and -map 1:a select which streams go into the output

The commands in this article that create output files were actually run with -hide_banner -loglevel error -y added. When I inspected the output file with ffprobe, it contained one video stream and one audio stream, both 20 seconds long.

text
$ ffprobe -v error -show_entries stream=index,codec_type,codec_name,channels,duration:stream_tags=language:format=duration -of compact=p=0 replaced.mp4
index=0|codec_name=h264|codec_type=video|duration=20.000000|tag:language=und
index=1|codec_name=aac|codec_type=audio|channels=2|duration=20.000000|tag:language=und
duration=20.000000

To confirm the replacement really happened, I computed the MD5 of each stream's data using FFmpeg's streamhash output format. The video matches the original video, and the audio matches new.m4a. Because -c copy was used, both contain the original data unchanged.

Stream MD5 of the source file MD5 in replaced.mp4
Video 0d81ce9b… (video.mp4) 0d81ce9b…
Audio dd679ac5… (new.m4a) dd679ac5…

Without -map, the original audio remains

When I gave two input files without -map, the file was created with no error. However, the audio MD5 matched the original video's audio (09dceb12…), so the audio had not been replaced.

text
$ ffmpeg -i video.mp4 -i new.m4a -c copy nomap.mp4
$ ffmpeg -i nomap.mp4 -map 0:a -c copy -f streamhash -hash md5 -
0,a,MD5=09dceb1261f4497956e62d13a1a104c2

According to the official documentation on automatic stream selection, when -map is not given, FFmpeg picks one stream of each type from all inputs. For audio, it picks the stream with the most channels, and if they are equal, the one with the lower index. Both test files were stereo, so the audio from input 0 (the original video) was chosen.

Even when I made the replacement audio mono, the stereo original audio was still selected. As the documented selection rules say, which audio gets chosen depends on the input order and channel count, so when replacing audio, it is safest to always write -map.

Replace with MP3 or WAV audio

MP3 audio went into the MP4 as-is with -c copy. The audio length was 20.016 seconds, slightly longer than the video.

text
$ ffmpeg -i video.mp4 -i new.mp3 -map 0:v -map 1:a -c copy replaced-mp3.mp4; echo exit=$?
exit=0
$ ffprobe -v error -show_entries stream=index,codec_type,codec_name,channels,duration:stream_tags=language:format=duration -of compact=p=0 replaced-mp3.mp4
index=0|codec_name=h264|codec_type=video|duration=20.000000|tag:language=und
index=1|codec_name=mp3|codec_type=audio|channels=2|duration=20.016000|tag:language=und
duration=20.016000

WAV (uncompressed pcm_s16le) also went into the MP4 with -c copy without an error. However, because it stays uncompressed, the file becomes large. To encode the audio to AAC while copying only the video, split the options as -c:v copy -c:a aac.

bash
ffmpeg -i video.mp4 -i new.wav -map 0:v -map 1:a -c:v copy -c:a aac -b:a 192k replaced-wav-aac.mp4

Because of the audio encoding, this took 0.595 seconds. The video MD5 matched the original video.

Replacement audio Method Output audio File size
new.m4a (AAC) -c copy AAC 2,189,768 bytes
new.mp3 (MP3) -c copy MP3 1,992,249 bytes
new.wav (WAV) -c copy PCM (uncompressed) 5,705,413 bytes
new.wav (WAV) -c:v copy -c:a aac -b:a 192k AAC 2,349,920 bytes

If the audio is longer, use -shortest to match the video

When I replaced the audio of a 20-second video with 30-second audio, the output was 30 seconds long. The video ended at 20 seconds, and the remaining 10 seconds had only audio.

text
$ ffprobe -v error -show_entries stream=index,codec_type,codec_name,channels,duration:stream_tags=language:format=duration -of compact=p=0 long-out.mp4
index=0|codec_name=h264|codec_type=video|duration=20.000000|tag:language=und
index=1|codec_name=aac|codec_type=audio|channels=2|duration=30.000000|tag:language=und
duration=30.000000

With -shortest, the output stops when the shortest stream ends.

bash
ffmpeg -i video.mp4 -i long.m4a -map 0:v -map 1:a -c copy -shortest long-shortest.mp4

The output was 20 seconds, and the audio was 19.989 seconds. When I re-encoded the audio to AAC, the audio was also 19.989 seconds. When I set the length with -t 20 instead of -shortest, the audio was 20.010 seconds. Neither gave exactly 20 seconds.

Option Video Audio Whole file
None 20.000 s 30.000 s 30.000 s
-shortest 20.000 s 19.989 s 20.000 s
-t 20 20.000 s 20.010 s 20.010 s

If the audio is shorter, use apad to add silence

When I replaced the audio with 12-second audio, the whole file stayed 20 seconds long, and the audio ended at 12 seconds. After 12 seconds, there was only video with no audio data.

text
$ ffprobe -v error -show_entries stream=index,codec_type,codec_name,channels,duration:stream_tags=language:format=duration -of compact=p=0 short-out.mp4
index=0|codec_name=h264|codec_type=video|duration=20.000000|tag:language=und
index=1|codec_name=aac|codec_type=audio|channels=2|duration=12.000000|tag:language=und
duration=20.000000

To keep the audio going until the end of the video, add silence after the audio with the apad filter. The official documentation also describes using it together with -shortest to make the audio the same length as the video. Without arguments, apad adds silence endlessly, so -shortest stops the output at the end of the video. Because the audio goes through a filter, it must be re-encoded (filters cannot be used with stream copy).

bash
ffmpeg -i video.mp4 -i short.m4a -map 0:v -map 1:a -c:v copy -c:a aac -b:a 128k -af apad -shortest short-apad.mp4

The audio length was 19.989 seconds. When I measured the volume of each section with the volumedetect filter, the 2–6 second section had sound with a mean of -24.1 dB, and the 14–18 second section was -91.0 dB, meaning it contained silence.

text
$ ffmpeg -hide_banner -ss 14 -t 4 -i short-apad.mp4 -map 0:a -af volumedetect -f null - 2>&1 | grep -E 'mean_volume|max_volume'
[Parsed_volumedetect_0 @ 0x55d8190bfc40] mean_volume: -91.0 dB
[Parsed_volumedetect_0 @ 0x55d8190bfc40] max_volume: -91.0 dB

Keep the original audio as a second track

You can also keep the original audio along with the new audio instead of replacing it. Write three -map options and put the new audio first.

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

In this case, both audio tracks in the output were marked as the default track (default). According to the -disposition description, when -disposition is not given, the dispositions of the input streams are copied. Both test inputs had their audio marked default, so both stayed marked.

text
$ ffprobe -v error -show_entries stream=index,codec_type:stream_disposition=default -of compact=p=0 two-audio-nodisp.mp4
index=0|codec_type=video|disposition:default=1
index=1|codec_type=audio|disposition:default=1
index=2|codec_type=audio|disposition:default=1

To make only the new audio the default track, set default on the first audio track and 0 on the second.

bash
ffmpeg -i video.mp4 -i new.m4a -map 0:v -map 1:a -map 0:a -c copy -disposition:a:0 default -disposition:a:1 0 two-audio.mp4
text
$ ffprobe -v error -show_entries stream=index,codec_type:stream_disposition=default -of compact=p=0 two-audio.mp4
index=0|codec_type=video|disposition:default=1
index=1|codec_type=audio|disposition:default=1
index=2|codec_type=audio|disposition:default=0

When I checked the audio MD5s, the first track matched new.m4a and the second matched the original video's audio. If you want to mix the original and new audio into a single track, don't use -map; use a filter such as amix, which combines multiple audio inputs into one.

​Posted by 86