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

replace audio
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

  1. New audio shorter than the video

    apad adds silence after the audio ends, and -shortest then 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.mp4
  2. Add 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

The flags in these commands
FlagWhat it doesUsed here
-map 0:vTakes every video stream from input 0All three
-map 1:aTakes every audio stream from input 1All three
-c:v copyCopies the video packets, no re-encodeAll three
-c:a aac -b:a 192kEncodes the new audio as AAC at 192 kbit/sAll three
-shortestEnds the output when the shortest stream endsAll three
-af apadPads the end of the audio with silenceShort audio
-metadata:s:a:0Sets a tag on the first audio stream of the outputTwo 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.

terminalTry it
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"]
  }'
The clip's picture with the music track instead of the voice, cut to length by -shortest.Input: 10.7-second clipmusic.mp3 (147.6 s)

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.

Ready to build?

Ship video from your app today

Your first $0.50 is free, and one key covers every endpoint.

Moonlit lake with a lit cabin under the mountains