Replace audio in a video

audio

// what it does

Swaps the video's soundtrack for an external audio file. Two inputs are opened, then -map 0:v:0 takes the first video stream from the video and -map 1:a:0 takes the first audio stream from the MP3; -c:v copy keeps the video untouched while -c:a aac re-encodes the new audio to an MP4-friendly codec. This is the standard 'dub over' or 'replace the music' operation.

// shell

$ ffmpeg -i input.mp4 -i audio.mp3 -c:v copy -c:a aac -map 0:v:0 -map 1:a:0 output.mp4

// gotcha

Without -shortest the output runs to the longer of the two streams: if the audio is shorter the tail of the video plays silent, if the audio is longer it keeps going after the picture ends — nothing is truncated, it just gets padded out. Add -shortest to cut output to whichever stream ends first.