Adding a new soundtrack to a video — background music, a voiceover, or a cleaned-up audio mix — is a classic FFmpeg job. The trick is telling FFmpeg to take the picture from one file and the sound from another, then combine them into a single output while keeping the video lossless. This guide shows the core two-input command, how to match lengths with -shortest, and a simple remove-then-add workflow. Estimated time: 8 minutes.

Tested with: FFmpeg 8.1


What You’ll Learn

  1. The core command: map video from one input, audio from another
  2. Matching durations with -shortest
  3. Removing the old audio first (a clean single-input step)
  4. Re-encoding audio vs copying it
  5. Adding an audio delay or offset
  6. Troubleshooting and FAQ

1. The Core Command — Two Inputs, Mapped Together

To replace a video’s audio with a separate audio file, give FFmpeg two inputs and use -map to pick the video from the first and the audio from the second:

ffmpeg -i input.mp4 -i input.mp3 -map 0:v:0 -map 1:a:0 -c:v copy -c:a aac -shortest output.mp4
  • -i input.mp4 … input 0 (the video)
  • -i input.mp3 … input 1 (the new audio)
  • -map 0:v:0 … take the video stream from input 0
  • -map 1:a:0 … take the audio stream from input 1
  • -c:v copy … copy the video losslessly (no re-encode)
  • -c:a aac … encode the new audio as AAC (MP4-friendly)
  • -shortest … stop at the shorter of the two streams
TokenReads as
0:v:0input 0, first video stream
1:a:0input 1, first audio stream
-c:v copykeep video bit-for-bit
-c:a aacre-encode audio to AAC

This command has two -i inputs, which is why it is shown as plain text rather than a runnable block — it needs both a video and an audio file present. It is the correct command for the job. The Map wiki explains the -map syntax in full.


2. Matching Durations with -shortest

The video and the new audio are rarely the same length. -shortest tells FFmpeg to end the output when the shorter stream runs out, so you never get a trailing silent video or audio that plays over a frozen last frame.

  • If the audio is longer than the video, the extra audio is cut off.
  • If the video is longer than the audio, the video is cut to the audio’s length.

If you want to keep the full video and simply let the music end early, omit -shortest — but then the tail of the video will have no sound.


3. Remove the Old Audio First (Single-Input Step)

A clean two-step approach is to first strip the original audio, then add the new one. Removing audio is a single-input operation, so it runs as a normal command:

ffmpeg -i input.mp4 -c:v copy -an output.mp4
  • -c:v copy … copy the video unchanged
  • -an … drop all audio

Now you have a silent video. Muxing in the new soundtrack is the two-input step from section 1. For more on stripping audio, see the remove audio guide.


4. Re-encode Audio vs Copy It

In section 1 the audio is re-encoded to AAC because MP4 plays best with AAC. If your source audio is already AAC (for example an .m4a file) you can copy it instead and skip the encode entirely:

ffmpeg -i input.mp4 -i input.m4a -map 0:v:0 -map 1:a:0 -c copy -shortest output.mp4
  • -c copy … copy both video and audio with no re-encoding
Source audioRecommendedWhy
WAV / FLAC-c:a aacNot MP4-native; re-encode to AAC
MP3-c:a aac (or -c copy)MP3 can be copied into MP4, but AAC gives the widest compatibility
AAC / M4A-c copyAlready compatible; copy losslessly

5. Add an Audio Delay or Offset

If the new audio should start a little later than the video, delay the audio input with -itsoffset before its -i:

ffmpeg -i input.mp4 -itsoffset 1.5 -i input.mp3 -map 0:v:0 -map 1:a:0 -c:v copy -c:a aac -shortest output.mp4
  • -itsoffset 1.5 … shift the next input (the audio) 1.5 seconds later

Adjust the number until the audio lines up. A negative value pulls the audio earlier.


6. Troubleshooting

Issue 1: The output keeps the original audio, not the new one

Cause: No -map was given, so FFmpeg auto-selected the video file’s own audio. Fix: Add -map 0:v:0 -map 1:a:0 to choose streams explicitly (section 1).

Issue 2: Audio and video drift out of sync

Cause: The two streams have different frame rates or start offsets. Fix: Use -itsoffset to nudge timing (section 5), or re-encode video so timestamps regenerate cleanly.

Issue 3: “Could not find tag for codec” when copying audio

Cause: The source audio codec isn’t valid in MP4. Fix: Re-encode with -c:a aac instead of -c copy (section 1).


FAQ

Q1. Why is the main command shown as plain text instead of a runnable block? A. It uses two inputs (a video and an audio file), which a generic single-file test can’t run. The command itself is correct — supply your own video and audio files.

Q2. Do I have to remove the old audio first? A. No. The two-input command in section 1 replaces it in one pass. The remove-first approach (section 3) is just a clear alternative.

Q3. Does replacing audio reduce video quality? A. No. -c:v copy copies the video unchanged; only the audio is written fresh.

Q4. My music is longer than the video — how do I cut it? A. Keep -shortest; it ends the output when the video ends.

Q5. Can I keep the original audio AND add the new one? A. Yes — map both audio streams instead of replacing. That makes a multi-track file; see working with multiple audio tracks.



Tested with FFmpeg 8.1 — verified with our command-check script Primary sources: ffmpeg.org/ffmpeg.html / trac.ffmpeg.org/wiki/Map