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.wavThe filter accepts two main parameters:
shift: The amount of DC shift to apply to the audio signal. The value is a floating-point number representing the displacement. The range is from-2.0to2.0. A positive value shifts the waveform up, while a negative value shifts it down. The default value is0.0.limitergain: An optional parameter used to prevent digital clipping. When the waveform is shifted, the peaks of the audio might exceed the maximum allowed digital amplitude (0 dBFS). Setting alimitergainvalue between0.0and1.0applies a limiter to prevent this clipping. The default is0.0(limiter disabled).
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.wav2. 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.wav3. 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.