Fix Audio DC Offset Using FFmpeg dcshift Filter
A DC offset in an audio recording occurs when the waveform is biased
away from the zero baseline, leading to digital clipping, pops at the
start and end of tracks, and reduced dynamic range. This article
explains how to detect a DC offset using FFmpeg’s analysis tools and how
to apply the dcshift filter to realign your audio and
restore its quality.
Before correcting the offset, you must first determine its value. You
can analyze your audio file using the FFmpeg astats (audio
statistics) filter. Run the following command in your terminal:
ffmpeg -i input.wav -af astats -f null -In the terminal output, look for the “DC offset” metric for each
channel. This value represents how far the waveform is shifted from
zero, typically shown as a small decimal value (such as
0.05 or -0.02).
Once you know the offset value, you can use the dcshift
filter to counteract it. The filter accepts a shift
parameter, which is the amount of DC shift to apply (ranging from -2.0
to 2.0). To remove the offset, apply the exact negative of the detected
DC offset value. For example, if your detected DC offset is
0.05, you need to shift it by -0.05:
ffmpeg -i input.wav -af "dcshift=shift=-0.05" output.wavIf the correction causes the audio peaks to exceed the maximum
digital volume, it can introduce clipping. To prevent this, you can use
the optional limitergain parameter. This parameter
specifies the gain to apply to the signal when it nears the maximum
levels. For example, to apply a shift of -0.05 with a
limiter gain of 0.05, run:
ffmpeg -i input.wav -af "dcshift=shift=-0.05:limitergain=0.05" output.wavThis ensures your audio waveform is perfectly centered on the zero-amplitude line, eliminating clicks and restoring headroom without introducing digital distortion.