ffmpegaudioapi

How to Replace Audio in Video with FFmpeg (Track Swap API Guide)

·Javid Jamae·8 min read
How to Replace Audio in Video with FFmpeg (Track Swap API Guide)

Swapping the audio track in a video sounds like a two-file merge. It isn't. When you drop a voiceover or a licensed music bed onto existing footage, you're not adding a track, you're replacing one, and FFmpeg will happily keep the original audio too unless you tell it exactly which streams to pull.

Quick answer: To replace audio in video with FFmpeg, map the video stream from the first input and the audio stream from the second, then drop the original audio: ffmpeg -i video.mp4 -i newaudio.mp3 -map 0:v:0 -map 1:a:0 -c:v copy -c:a aac -shortest output.mp4. The -map flags are what make it a swap instead of a mix.

The FFmpeg command to swap an audio track

Replacing audio is a stream-selection job, not a filter job. You tell FFmpeg to take video from input 0 and audio from input 1, and everything else gets left behind.

ffmpeg -i video.mp4 -i newaudio.mp3 \
  -map 0:v:0 \
  -map 1:a:0 \
  -c:v copy \
  -c:a aac -b:a 192k \
  -shortest \
  output.mp4

Each part does one thing:

  • -map 0:v:0 selects the first video stream from the first input (video.mp4).
  • -map 1:a:0 selects the first audio stream from the second input (newaudio.mp3).
  • -c:v copy passes the video through untouched, so there's no re-encode and no quality loss.
  • -c:a aac -b:a 192k re-encodes the new audio to AAC, which every MP4 player understands.
  • -shortest ends the output when the shorter of the two streams runs out.

The moment you leave out the -map flags, FFmpeg falls back to its default stream picker and keeps the video's original audio. That single omission is the top reason "replace" turns into "the old audio is still there." If you want a deeper tour of stream selection, the FFmpeg -map flag guide breaks down the 0:v:0 syntax.

Replace vs mix: why they're different commands

Replacing removes the original audio entirely. Mixing keeps both and blends them. People conflate the two because both take a video and an audio file as input, but the FFmpeg commands diverge completely.

A swap uses -map to hand-pick streams. A mix uses the amix filter to combine them:

# Mix (keeps both, blends the levels)
ffmpeg -i video.mp4 -i music.mp3 \
  -filter_complex "[0:a][1:a]amix=inputs=2:duration=shortest[a]" \
  -map 0:v:0 -map "[a]" -c:v copy output.mp4

If your goal is a clean voiceover over silent B-roll, or a licensed track over footage whose original audio you don't want, you want the swap. Reach for a mix only when the original ambient sound should stay under the new track.

Handle the length mismatch

Your new audio and your video will almost never be the same length, and how you resolve that gap changes the output. This is where templated production breaks if you ignore it.

Three common cases:

  • Audio shorter than video. With -shortest, the output ends when the audio does and you lose the tail of the footage. Drop -shortest and the video plays to the end over silence.
  • Audio longer than video. -shortest cuts the audio at the last video frame. Without it, the file keeps the trailing audio with no picture.
  • You need an exact target length. Pad or trim the audio with apad or -t before the swap so the durations line up on purpose, not by accident.

For a 45-second clip that must always end on the last frame, -shortest plus audio you've pre-trimmed to 45 seconds is the reliable combo. Guessing here is how one faceless-channel builder ended up with 200 clips that each faded to black three seconds early.

Do the same swap with one API call

The CLI works great on your laptop. It stops being fun when the swap has to run 500 times a night inside an n8n flow, or behind a SaaS feature, or from an AI agent, and now you own FFmpeg installs, codec versions, and a queue of long-running jobs. FFmpeg Micro exposes the same track swap as a job on a hosted FFmpeg API: you submit the two source URLs and the mapping, then poll or catch a webhook for the finished file.

curl -X POST https://api.ffmpeg-micro.com/v1/jobs \
  -H "Authorization: Bearer $FFMPEG_MICRO_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "inputs": [
      { "url": "https://example.com/video.mp4" },
      { "url": "https://example.com/newaudio.mp3" }
    ],
    "map": ["0:v:0", "1:a:0"],
    "video_codec": "copy",
    "audio_codec": "aac",
    "shortest": true
  }'

The response is a job you poll or wire to a webhook. The exact field names and options live in the docs, and the same -map semantics from the CLI carry straight over, so nothing new to learn. No binaries, no servers to run, and the free tier covers you while you template the workflow. If you're building the voiceover side of this, adding an AI voiceover with ElevenLabs pairs the generated track with this exact swap step.

The honest comparison:

FFmpeg CLIFFmpeg Micro API
SetupInstall FFmpeg, manage versionsOne API key
Runs whereYour machine or a server you maintainHosted, no servers to run
Batch of 500 swapsYour queue, your scalingOne call per job, they scale
Fits into n8n/Make/ZapierShell node, fragileFirst-class HTTP step
Best forOne-off local editsTemplated, repeated production

Common pitfalls when replacing audio

Most failed swaps come down to four mistakes, and all of them are quick to fix.

  • Forgetting -map entirely. FFmpeg keeps the original audio. Always specify both -map 0:v:0 and -map 1:a:0.
  • Copying an incompatible audio codec into MP4. -c:a copy with an Opus or raw PCM source can produce a file that won't play everywhere. Re-encode to AAC to be safe.
  • Ignoring extra audio tracks. A source with multiple language tracks means 1:a:0 picks only the first. Check the streams with ffprobe before you assume.
  • A/V sync drift on variable-frame-rate video. Screen recordings and phone footage sometimes use VFR, which can slide the new audio out of sync. Re-encoding video with -c:v libx264 -vsync cfr instead of -c:v copy fixes it at the cost of an encode.

When not to swap this way

Track replacement is the wrong tool in a few cases. If you want the original sound to stay under a music bed, mix with amix instead of mapping a single audio stream. If you only need to strip audio and add nothing, removing the audio track with -an is simpler. And if you're syncing dialogue to lip movement frame by frame, that's an editor's job, not a stream swap.

FAQ

How do I replace audio in a video with FFmpeg without re-encoding the video?

Use -c:v copy so the video stream passes through untouched: ffmpeg -i video.mp4 -i newaudio.mp3 -map 0:v:0 -map 1:a:0 -c:v copy -c:a aac -shortest output.mp4. Only the audio gets re-encoded, so the swap is fast and lossless on the video side.

Why does FFmpeg keep the original audio when I add a new track?

Because without -map flags, FFmpeg uses its default stream selection and grabs the original audio along with the video. Explicitly map the new audio with -map 1:a:0 and only the video with -map 0:v:0 to replace instead of keep.

How do I swap an audio track in a video with an API?

Submit a job to a hosted FFmpeg API like FFmpeg Micro with both source URLs, a map of ["0:v:0", "1:a:0"], and shortest: true, then poll or catch a webhook for the output. It runs the same stream selection as the CLI without you installing FFmpeg or running a server.

What happens if the new audio is shorter than the video?

With -shortest, the output ends when the audio ends and the remaining video is dropped. Without it, the video plays to its full length with silence after the audio finishes, so pad or trim the audio first when you need an exact runtime.

Can I replace audio in a video inside n8n or Make?

Yes. Call the FFmpeg Micro API from an HTTP request node with the video URL, the new audio URL, and the map parameters, then use polling or a webhook to fetch the result. It drops into a video webhook workflow the same way any other job does.

Ready to run the same swap without owning an FFmpeg install? Grab a key and try it on the free tier: Sign up free.

About Javid Jamae

Founder & CEO at FFmpeg Micro

Javid is a software engineer, author, and entrepreneur with over 25 years of professional software development experience across enterprise, startup, and consulting environments. He founded FFmpeg Micro to make video processing accessible to developers through a simple, automation-first REST API.

Software EngineeringVideo ProcessingFFmpegCloud ArchitectureAPI DesignAutomation

Ready to process videos at scale?

Start using FFmpeg Micro's simple API today. No infrastructure required.

Get Started Free