ffmpeg -i in.mp4 -i music.mp3 -map 0:v -map 1:a -c:v copy -shortest out.mp4 swaps the soundtrack and copies the picture. Send the same args to ReelSDK and the swap costs under a fifth of a cent, at $0.02 per compute minute.
Replace the audio in a video with ffmpeg
Take the video from input 0 and the audio from input 1, then stop at whichever ends first; ReelSDK's API did that swap in under 5 seconds. Without -shortest, an 11-second clip with a long song keeps showing its last frame for over two minutes.
GuidesUpdated
ffmpeg -i in.mp4 -i music.mp3 -map 0:v -map 1:a \
-c:v copy -c:a aac -b:a 192k -shortest out.mp4-map 0:v takes the video from the first input and -map 1:a the audio from the second. The video is copied, the MP3 is encoded to AAC, and -shortest ends the file with the video, at the length of the original clip.
Variations
The other commands you will need
New audio shorter than the video
apadadds silence after the audio ends, and-shortestthen cuts at the end of the video. With a 5-second MP3 the output kept the full length of the video.pad short audio ffmpeg -i in.mp4 -i short.mp3 -map 0:v -map 1:a -c:v copy \ -af apad -c:a aac -b:a 192k -shortest padded.mp4Add a track instead of replacing one
Map both audio streams and the player gets a choice. Track 0 is the music, track 1 the original voice, each with a title.
two audio tracks ffmpeg -i in.mp4 -i music.mp3 -map 0:v -map 1:a -map 0:a \ -c:v copy -c:a aac -b:a 192k -shortest \ -metadata:s:a:0 title=Music -metadata:s:a:1 title=Original two.mp4
The flags
The flags in these commands
| Flag | What it does | Used here |
|---|---|---|
-map 0:v | Takes every video stream from input 0 | All three |
-map 1:a | Takes every audio stream from input 1 | All three |
-c:v copy | Copies the video packets, no re-encode | All three |
-c:a aac -b:a 192k | Encodes the new audio as AAC at 192 kbit/s | All three |
-shortest | Ends the output when the shortest stream ends | All three |
-af apad | Pads the end of the audio with silence | Short audio |
-metadata:s:a:0 | Sets a tag on the first audio stream of the output | Two tracks |
Sources: ffmpeg documentation, apad filter, checked November 2026.
Without -map you keep the old audio
With two inputs and no -map, ffmpeg picks one stream of each type by itself. For audio it takes the stream with the most channels, and on a tie the one from the first input. Our clip and the song are both stereo, so the clip's own voice won and the music was dropped: auto.mp4 came out with the 48 kHz narration and no music.
Once you add any -map, the automatic choice is off and the output holds exactly the streams you listed. That is why the command maps 0:v as well as 1:a.
-shortest decides the length
The song runs about 2.5 minutes and the clip 11 seconds. Without -shortest the output kept all of the music, and a player shows the last frame for over two minutes.
With -shortest, ffmpeg stops when the video ends. The ffmpeg docs note that it may buffer frames to do that, up to -shortest_buf_duration, which defaults to 10 seconds.
That buffer shows when you copy both streams. With -c copy and the MP3 left as is, the audio ran about 5 seconds past the end of the video. Encoding the audio, as in the main command, ended both streams with the video.
When the new audio is too short
-shortest cuts at the shortest stream, whichever it is. With a 5-second MP3 and no padding, the output stopped after 5 seconds, less than half the clip. Adding -af apad makes the audio endless silence after the song, so the video becomes the shorter stream again and the file keeps its full length.
If you want the song to repeat instead of going quiet, loop the input with -stream_loop -1 before -i music.mp3 and keep -shortest.
Loudness after the swap
Our narration is about 2 LU louder than the start of the song, so the swapped file is a little quieter than the original. If the new track should match a target, run it through loudnorm in the same command. To keep the voice and lay music under it, mix the two instead of replacing: see mixing audio and ducking.
Without installing ffmpeg
The same command as one API call
Send the same args to POST /v1/ffmpeg with both URLs in inputs. Each job runs ffmpeg 8.1 on a fresh machine at $0.02 per compute minute, billed per second; this swap finished in under 5 seconds and cost under a fifth of a cent.
curl "https://api.reelsdk.com/v1/ffmpeg?wait=60" \
-H "Authorization: Bearer $REELSDK_KEY" \
--json '{
"inputs": {
"in.mp4": "https://api.reelsdk.com/f/lib/demo/clip.mp4",
"music.mp3": "https://api.reelsdk.com/f/lib/music/chill/calm-currents.mp3"
},
"args": [
"-i", "in.mp4", "-i", "music.mp3", "-map", "0:v", "-map", "1:a",
"-c:v", "copy", "-c:a", "aac", "-b:a", "192k", "-shortest", "out.mp4"
],
"outputs": ["out.mp4"]
}'FAQ
Questions, answered
How do I replace the audio of a video with ffmpeg?
Run ffmpeg -i in.mp4 -i new.mp3 -map 0:v -map 1:a -c:v copy -c:a aac -shortest out.mp4. The video is copied without re-encoding and the file ends when the video does.
Why does my output still have the old audio?
Because without -map ffmpeg chooses the audio stream itself: the one with the most channels, and on a tie the one from the first input. Map the video and the new audio explicitly with -map 0:v -map 1:a.
Why is my file as long as the song?
Add -shortest. Without it ffmpeg writes every stream to its end; our 11-second clip with a 2.5-minute song came out as long as the song.
Can I do this without installing ffmpeg?
Yes. POST the same args to https://api.reelsdk.com/v1/ffmpeg with the video and audio URLs in inputs. Each job runs ffmpeg 8.1 for $0.02 per compute minute, billed per second, and failed jobs are free.
- Add music to videoMix a looped music track under a video's own audio with ducking and a fade-out in one ffmpeg call, for a fraction of a cent.
- Mix two audio tracks with ffmpegLay music under a voice or combine any two tracks with amix, set their levels with weights, and avoid the three defaults that bite.
- Duck music under a voice with ffmpegLower background music only while someone speaks, with sidechaincompress keyed by the voice and amix to put them together.
- Remove audio from a video with ffmpegStrip the sound from a video with -an while the picture is copied bit for bit, or drop one track, mute a range or add silence.
- Extract audio from a video with ffmpegCopy the AAC track out of an MP4 without re-encoding, or convert it to MP3 or WAV, with the file size each command produced.
- Normalize audio loudness with ffmpeg loudnormBring any track to a loudness target such as -14 LUFS with loudnorm: one pass, or a measured second pass that keeps the dynamics.
- Loop a video with ffmpegRepeat a clip a set number of times or to a length with -stream_loop, without re-encoding, or with the loop filter inside a graph.
- FFmpeg onlineRun your own ffmpeg command on your files in the browser, free and without installing or uploading anything.
- ReelSDK vs self-hosted FFmpegTranscoding a minute of 1080p to 720p takes about 24 seconds of compute: about 0.8 cents on ReelSDK, about a tenth of a cent on a busy 4 vCPU box. Below about 11,000 minutes a month, the API costs less than an always-on box.
Sources, checked November 2026
Ready to build?
Ship video from your app today
Your first $0.50 is free, and one key covers every endpoint.
