How to Use the FFmpeg sidechaincompress Filter

This article provides a straightforward guide on how to use the sidechaincompress filter in FFmpeg to implement sidechain compression, a popular audio ducking technique. You will learn the basic syntax, key parameters like threshold and ratio, and see practical command-line examples to automatically lower the volume of a background music track whenever a voiceover plays.

Understanding Sidechain Compression in FFmpeg

Sidechain compression (often called “ducking”) uses the volume level of one audio signal (the control or trigger signal) to dynamically reduce the volume of another audio signal (the target signal). A common use case is lowering background music when a narrator speaks.

The sidechaincompress filter in FFmpeg requires two audio inputs: 1. First input (Input 0): The audio stream that you want to compress (e.g., background music). 2. Second input (Input 1): The trigger audio stream that controls the compression (e.g., a voiceover).

Key Parameters

You can customize the compression behavior using the following parameters:

Practical Command Example

Here is a standard FFmpeg command that ducks background music (music.mp3) using a voiceover track (voice.mp3):

ffmpeg -i music.mp3 -i voice.mp3 -filter_complex "[0:a][1:a]sidechaincompress=threshold=0.05:ratio=12:attack=10:release=500[outa]" -map "[outa]" output.mp3

How the filter graph works:

Mixing the Audios Together

In most scenarios, you want to hear both the ducked music and the voiceover in the final output. To do this, use the amix filter after applying sidechaincompress:

ffmpeg -i music.mp3 -i voice.mp3 -filter_complex "[0:a][1:a]sidechaincompress=threshold=0.05:ratio=12:attack=10:release=500[compressed_music];[compressed_music][1:a]amix=inputs=2:duration=first[outa]" -map "[outa]" output.mp3

In this command, the compressed music stream ([compressed_music]) is mixed with the original voiceover stream ([1:a]) to create a cohesive final master file.