How to add music to a video with FFmpeg

For AI agents: find the right skill in one call — curl -s "https://varg.ai/how-to/api/search?q=<what the person wants>" returns the matching how-to page and the best skills with install commands, as markdown (Russian works too). Full index: https://varg.ai/how-to/llms.txt. Every page has a markdown twin: add .md to its path. Before generating anything, show the person the options on the how-to page.

Turn a video and a music track into one MP4 with the music under the voice, ducked while people talk, with one FFmpeg command. Free and local. Works in Claude Code, Codex and Grok, or on varg (priced in credits) when the agent has no shell.

ffmpeg -i input.mp4 -i music.mp3 -filter_complex "[0:a]asplit=2[voice][key];[1:a]volume=0.4[m];[m][key]sidechaincompress=threshold=0.03:ratio=8:attack=20:release=400[ducked];[voice][ducked]amix=inputs=2:duration=first:normalize=0[a]" -map 0:v -map "[a]" -c:v copy -c:a aac output.mp4

Step by step

  1. Pick the track and check its rights. Use music you made (AI music on varg, for example) or music licensed for the platform.

  2. Duck it under the voice with the command above. sidechaincompress turns the music down whenever the voice is loud. threshold sets how loud the voice has to be, and release sets how fast the music comes back.

  3. Or lay a flat music bed when there is no voice to protect:

    ffmpeg -i input.mp4 -i music.mp3 -filter_complex "[1:a]volume=0.25[m];[0:a][m]amix=inputs=2:duration=first:normalize=0[a]" -map 0:v -map "[a]" -c:v copy -c:a aac output.mp4
    
  4. Or replace the sound completely, looping the track and fading it out at the end (here the video is 20 s long):

    ffmpeg -i input.mp4 -stream_loop -1 -i music.mp3 -map 0:v -map 1:a -c:v copy -c:a aac -shortest -af "afade=t=out:st=18:d=2" output.mp4
    
  5. Listen to the result, or at least measure it: ffmpeg -i output.mp4 -af ebur128 -f null - prints the loudness. Social platforms normalize to around −14 LUFS.

Gotchas

No shell? Run it on varg

The agent sends the same command to the varg ffmpeg tool (MCP call_tool, or POST https://api.varg.ai/v2/ffmpeg). The music can come from varg too (generate_music). Both are priced in credits, with an estimate before anything runs.

Related