Detect DC Offset in Audio Using FFmpeg astats
A DC offset occurs when an audio signal's waveform is not centered on
the zero-amplitude axis, potentially reducing dynamic headroom and
causing audible clicks or distortion during playback and editing. This
guide provides a straightforward method to analyze audio files and
detect the presence and magnitude of a DC offset using FFmpeg's built-in
astats (audio statistics) filter directly from the command
line.
Running the Detection Command
To analyze the audio without creating an unnecessary output file, use
FFmpeg with the astats audio filter and direct the output
to the null muxer:
ffmpeg -i input.wav -af "astats" -f null -Replace input.wav with the path to your target audio
file (formats like MP3, FLAC, and AAC are also supported).
Filtering for the DC Offset Metric
The default astats filter generates an extensive list of
audio properties for every channel, including peak levels, RMS values,
and dynamic range. To isolate only the DC offset values, pipe the
standard error output through grep (or findstr
on Windows):
Linux / macOS:
ffmpeg -i input.wav -af "astats" -f null - 2>&1 | grep "DC offset"Windows (Command Prompt):
ffmpeg -i input.wav -af "astats" -f null - 2>&1 | findstr "DC offset"Interpreting the Results
The command output will display the measured DC offset for each individual audio channel. It looks similar to this:
[Parsed_astats_0 @ 0x7fa2c4004bc0] Channel: 1
[Parsed_astats_0 @ 0x7fa2c4004bc0] DC offset: 0.000000
[Parsed_astats_0 @ 0x7fa2c4004bc0] Channel: 2
[Parsed_astats_0 @ 0x7fa2c4004bc0] DC offset: -0.014522
- Value of
0.000000: The channel has no DC offset and is properly centered around the zero line. - Positive or Negative Values: Any non-zero number
indicates an offset. For example, a value of
-0.014522means the entire waveform in that channel is shifted downward by roughly 1.45% of the total digital scale.
Correcting the Offset
If a non-zero offset is detected, you can remove it using FFmpeg's
highpass filter set to an inaudible frequency (such as 5 Hz
to 20 Hz) or by applying the dcshift filter:
ffmpeg -i input.wav -af "highpass=f=10" output.wav