How to Use the dcshift Filter in FFmpeg

The dcshift filter in FFmpeg is a specialized audio filter used to apply a DC offset shift to an audio stream. This article explains what the dcshift filter does, details its syntax and parameters, and provides practical examples for correcting DC offset errors or intentionally modifying your audio files using FFmpeg.

Understanding DC Offset

DC offset is an audio anomaly where the entire audio waveform is displaced from the zero-amplitude center line. This is usually caused by faulty recording hardware or electrical interference. DC offset reduces the available headroom of an audio track, which can lead to premature clipping, distortion, and audible clicks at the beginning and end of playback. The dcshift filter helps resolve this by shifting the waveform back to the center.

Syntax and Parameters

The basic syntax for applying the dcshift filter in an FFmpeg command is:

ffmpeg -i input.wav -af "dcshift=shift=value:limitergain=value" output.wav

The filter accepts two main parameters:

Practical Examples

1. Removing a Known DC Offset

If you have analyzed your audio and know that it has a positive DC offset of 0.05, you can correct it by shifting the signal down by the same amount:

ffmpeg -i input.wav -af "dcshift=shift=-0.05" output.wav

2. Shifting with a Limiter to Prevent Distortion

When applying a large shift that might push the audio peaks into clipping territory, use the limitergain parameter to keep the signal within safe boundaries:

ffmpeg -i input.wav -af "dcshift=shift=0.15:limitergain=0.8" output.wav

3. Finding the DC Offset Value First

To know how much shift to apply, you can first measure the DC offset of your audio file using FFmpeg’s astats filter. Run the following command and look for the “DC offset” value in the console output:

ffmpeg -i input.wav -af astats -f null -

Once you identify the offset value from the output (for example, 0.012), you can apply the opposite value (in this case, -0.012) with the dcshift filter to normalize the audio center.