Mix the video's audio with an external audio file (e.g., background music)
Adding a music track or voiceover under the existing audio is a common post-production task. The amix filter mixes two audio streams together, with options for the number of inputs and the duration handling.
The command
ffmpeg -i input.mp4 -i music.mp3 -filter_complex "[0:a][1:a]amix=inputs=2:duration=first[a]" -map 0:v -map "[a]" -c:v copy output.mp4What it does
FFmpeg takes the audio from input 0 (the video's existing audio) and the audio from input 1 (the music), mixes them with amix, and labels the output [a]. duration=first means the output is as long as the first input. The video stream from input 0 is mapped through with -c:v copy; the mixed audio replaces the original.
When to use it
For adding background music to a voiceover, layering a sound effect on top of ambient audio, or any time you need two audio sources to play together. The mix is at equal volumes by default; add :weights=1 0.5 to make the second input quieter (0.5 = -6 dB).
Video and audio requirements
Any decodable video and audio. The two audio streams do not need to share codec or sample rate; amix handles resampling. They should have the same sample rate to avoid resampling artifacts; if not, add the aresample filter before amix.
Things to avoid
- Forgetting to set weights when one input should be louder.
amixdefaults to equal weights, so the music and the voice are at the same volume, which makes the voice hard to hear. Use:weights=1 0.3or similar to balance. - Mixing two stereo sources into a mono output by accident.
amixpreserves the channel layout of the first input; if the first is stereo and the second is mono, the result is stereo with the mono duplicated. - Forgetting that
amixsums the signals, not averages them. Two full-scale sources at equal weight clip to 0 dBFS and distort. Apply a limiter (alimiter) after the mix to prevent clipping. - Using this to add music to a video whose audio you want to keep at its original level. The mix is additive; the voice and the music together are louder than either alone. Normalize the voice track first if it is close to 0 dBFS.