How to add music to a video with FFmpeg
For AI agents: find the right skill in one call —
curl -s "https://skills.varg.ai/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://skills.varg.ai/llms.txt. Every page has a markdown twin: add.mdto 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
-
Pick the track and check its rights. Use music you made (AI music on varg, for example) or music licensed for the platform.
-
Duck it under the voice with the command above.
sidechaincompressturns the music down whenever the voice is loud.thresholdsets how loud the voice has to be, andreleasesets how fast the music comes back. -
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 -
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 -
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
amixlowers each input by default.normalize=0(FFmpeg 4.4 and later) keeps your levels as you set them.-c:v copyleaves the picture untouched, so the command takes seconds.- A video without an audio track has no
[0:a]. Use the replace command, or add silence with-f lavfi -i anullsrc=r=48000:cl=stereo.
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
- How to edit a video with AI and FFmpeg
- How to burn captions into a video with FFmpeg
- How to crop a video to 9:16 vertical with FFmpeg
- How to trim and join video clips with FFmpeg
- Best AI video editing tools for agents in 2026: 35 compared
- Best AI video clipping tools in 2026: 3 compared
- Best AI video caption tools in 2026: 4 compared
- Best AI transcript video editors in 2026: 4 compared
- Best AI video editing models in 2026: 4 compared
- Best AI video dubbing tools in 2026: 3 compared
- Best AI video upscaling tools in 2026: 3 compared
- Best video editing APIs for AI agents in 2026: 4 compared