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-mapflags 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:0selects the first video stream from the first input (video.mp4).-map 1:a:0selects the first audio stream from the second input (newaudio.mp3).-c:v copypasses the video through untouched, so there's no re-encode and no quality loss.-c:a aac -b:a 192kre-encodes the new audio to AAC, which every MP4 player understands.-shortestends 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-shortestand the video plays to the end over silence. - Audio longer than video.
-shortestcuts 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
apador-tbefore 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 CLI | FFmpeg Micro API | |
|---|---|---|
| Setup | Install FFmpeg, manage versions | One API key |
| Runs where | Your machine or a server you maintain | Hosted, no servers to run |
| Batch of 500 swaps | Your queue, your scaling | One call per job, they scale |
| Fits into n8n/Make/Zapier | Shell node, fragile | First-class HTTP step |
| Best for | One-off local edits | Templated, repeated production |
Common pitfalls when replacing audio
Most failed swaps come down to four mistakes, and all of them are quick to fix.
- Forgetting
-mapentirely. FFmpeg keeps the original audio. Always specify both-map 0:v:0and-map 1:a:0. - Copying an incompatible audio codec into MP4.
-c:a copywith 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:0picks 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 cfrinstead of-c:v copyfixes 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.
You might also like

FFmpeg Scene Detection: Auto-Split a Long Video at Scene Changes
FFmpeg scene detection with select='gt(scene,0.4)',showinfo finds cut points automatically, then split a long video into clips by scene, no hand-marked timestamps.

Turn long-form video into Shorts/Reels/TikToks automatically
Turn long form video into shorts automatically by splitting moment-picking from clip-making, then calling a video API to cut, crop to 9:16, and caption.

Compress video for the web: bitrate, codecs, and one-call presets
Compress video for the web with the right bitrate, codec, and CRF preset. See the exact FFmpeg command plus a one-call video compression API you can try free.
Ready to process videos at scale?
Start using FFmpeg Micro's simple API today. No infrastructure required.
Get Started Free