Thursday, October 23, 2025

Using FFmpeg to add audio to your video

I have decided to add some background music to my smartphone gameplay casts, and it turns out that it's easiest to do this with ffmpeg. It's just a two-step process:

  1. Find out the video length.

    ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 input.mp4

  2. Add background music with a fade-out effect (starting two seconds before the video ends, D-2 should be the video length returned by the previous command, minus two seconds).

    ffmpeg -i input.mp4 -i input.mp3 -map 0:v -map 1:a -c:v copy -c:a aac -af "afade=t=out:st=D-2:d=2" -shortest output.mp4