Configure FFmpeg aselect Filter Volume Threshold
This article explains how to configure the volume threshold in FFmpeg
using the aselect filter. You will learn how to pair
aselect with the astats filter to measure
audio levels in real time and discard any audio frames that fall below a
specified decibel (dB) threshold, effectively filtering out silence or
quiet background noise.
To filter audio by volume using aselect, you must chain
it with the astats filter. Because aselect
cannot measure audio volume on its own, astats is used
first to inject volume metadata into each audio frame. The
aselect filter then reads this metadata and determines
whether to keep or discard the frame based on your target threshold.
The Basic Command Syntax
The standard FFmpeg filtergraph for volume thresholding looks like this:
ffmpeg -i input.mp3 -af "astats=metadata=1,aselect='gt(metadata:lavfi.astats.Overall.RMS_level,-40)'" -acodec libmp3lame output.mp3How It Works
astats=metadata=1: This activates the audio statistics filter and instructs it to inject measurement metadata (such as RMS and peak levels) into each audio frame.aselect='...': This filters the audio frames. Only frames that evaluate to true (non-zero) are passed to the output.metadata:lavfi.astats.Overall.RMS_level: This extracts the overall Root Mean Square (RMS) volume level of the current frame in decibels (dB).gt(..., -40): This is the “greater than” function. In this example, it checks if the frame’s volume is louder than-40dB. Any frame quieter than-40dB (for example,-50dB) evaluates to false and is discarded.
Adjusting the Threshold and Metrics
You can customize the filter behavior by changing the volume metric or the decibel threshold:
Change the decibel limit: To make the filter more sensitive (allowing quieter sounds through), lower the threshold to
-50or-60. To make it less sensitive (only keeping very loud sounds), raise it to-30or-20.Use Peak Level instead of RMS: If you want to filter based on transient peak volume rather than average RMS volume, replace
Overall.RMS_levelwithOverall.Peak_level:aselect='gt(metadata:lavfi.astats.Overall.Peak_level,-35)'Filter by specific channel: If you only want to measure the volume of the first channel (usually the left channel), replace
Overallwith the channel number:aselect='gt(metadata:lavfi.astats.1.RMS_level,-40)'