How to Use FFmpeg astats Filter for Audio Analysis
The astats filter in FFmpeg is a powerful tool designed
to analyze audio channels and output various statistical properties,
such as volume levels, peak amplitudes, and dynamic range. This article
provides a quick, practical guide on how to use the astats
filter, explains its key output parameters, and shows real-world
command-line examples to help you measure and understand your audio
files.
To use the astats filter, you apply it as an audio
filter (-af) within your FFmpeg command. Because you
typically only want to read the statistics rather than write a new audio
file, it is common to discard the output audio by directing it to the
null muxer.
Basic Usage Command
Run the following command to analyze an audio file and print the global statistics directly to your terminal:
ffmpeg -i input.wav -af astats -f null -FFmpeg will process the entire file and output a detailed block of statistics for each audio channel, as well as cumulative stats for all channels combined.
Key Statistical Metrics Explained
When the analysis finishes, the astats filter displays
several crucial metrics: * DC offset: Shows the
displacement of the audio signal from the zero line. A high DC offset
can reduce headroom and cause clicks. * Min/Max level:
The lowest and highest peak amplitude values detected in the audio file,
represented in decibels (dB). * RMS (Root Mean Square)
level: The average power of the audio signal. This value
closely correlates with how loud humans perceive the audio to be. *
Crest factor: The ratio of the peak amplitude to the
RMS value, which indicates the dynamic range of the track. *
Flat factor: Indicates how often the signal hits its
maximum peak level, which helps identify digital clipping or heavy
limiting.
Useful Filter Options
You can customize the behavior of the astats filter by
passing optional parameters:
length: Sets the window length in seconds for calculating local measurements. The default is 0.05 seconds.ffmpeg -i input.wav -af astats=length=0.1 -f null -metadata: When set to1(ortrue), this option injects the calculated statistics into the audio frames as metadata. This is useful if you want to pass these values to downstream filters or extract them usingffprobe.ffmpeg -i input.wav -af astats=metadata=1 -f null -reset: Resets the accumulated statistics after a specified number of frames.
Using the astats filter is an efficient way to quickly
check if your audio is clipping, measure its perceived loudness, or
diagnose channel imbalances without needing to open a graphical audio
editor.