Insert an audio clip to a video file with FFMPEG

October 26, 2017

The goal

For a hobby project of mine, I wanted to be able to merge an audio clip into the existing audio track of a video file.

The command

After struggling for an hour or two, I finally came up with the correct incantation of the command ffmpeg to overlay an audio clip to a video file:

ffmpeg \
  -i movie.mkv \
  -i new-audio-clip.mp3 \
  -filter_complex "[1:0] adelay=2728000|2728000 [delayed];[0:1][delayed] amix=inputs=2" \
  -map 0:0 \
  -c:a aac -strict -2 \
  -c:v copy \
  output.mp4

The explanation

Line by line:

JS version

And for any of you interested in doing this in JavaScript, here's the same command translated to node-fluent-ffmpeg:

ffmpeg()
  .input('movie.mkv')
  .input('new-audio-clip.mp3')
  .complexFilter([
    '[1:0] adelay=2728000|2728000 [delayed]',
    '[0:1][delayed] amix=inputs=2',
  ])
  .outputOption('-map 0:0')
  .audioCodec('aac')
  .videoCodec('copy')
  .save('output.mp4')

Realization

After writing all of this, I now realize that I actually need to silence the video's audio track for the duration of the new audio clip. Not merge them.

Back to work.

← Back to posts