How to Use FFmpeg fftfilt for Frequency-Domain Filtering
The fftfilt filter in FFmpeg is a powerful tool designed
to apply two-dimensional Fast Fourier Transform (FFT) filtering to video
frames. By transitioning video data from the spatial domain to the
frequency domain, you can precisely manipulate specific spatial
frequencies to achieve effects like noise reduction, blurring,
sharpening, or pattern generation. This article provides a
straightforward guide on how fftfilt works, its primary
parameters, and practical command-line examples to get you started.
Understanding the fftfilt Filter
The fftfilt filter processes the luminance (Y) and
chrominance (U/V) planes of a video stream individually. It transforms
each frame into its frequency components, multiplies these components by
a user-defined mathematical expression (the weight), and then performs
an Inverse Fast Fourier Transform (IFFT) to convert the modified frame
back into viewable pixels.
Key Parameters
The filter uses separate parameters for the Y, U, and V planes:
dc_Y,dc_U,dc_V: Sets the DC (Direct Current) coefficient for the respective plane. This represents the average brightness or baseline color level of the frame.weight_Y,weight_U,weight_V: Sets the frequency-domain weight expression for each plane. These expressions dictate which frequencies are kept, boosted, or discarded.
Available Variables
Within your expressions, you can use the following variables to target specific frequency regions:
X: The current horizontal frequency coordinate.Y: The current vertical frequency coordinate.W: The width of the transform block (corresponds to the plane width).H: The height of the transform block (corresponds to the plane height).
Low-frequency coefficients (which represent smooth areas and overall
lighting) are located near the corners of the frequency domain
(coordinates close to 0 or near W and
H). High-frequency coefficients (representing sharp edges,
fine details, and noise) are located toward the center of the frequency
spectrum.
Practical Examples
1. Low-Pass Filter (Blurring / Noise Reduction)
A low-pass filter allows low frequencies to pass through while blocking high frequencies. This results in a smoothed, blurred image, which is highly effective for removing high-frequency analog noise.
To eliminate high horizontal and vertical frequencies, you can use an
expression that only keeps frequencies near the coordinates of
0:
ffmpeg -i input.mp4 -vf "fftfilt=weight_Y='lts(X,16)*lts(Y,16)'" -c:a copy output.mp4In this command, lts(X,16) acts as a threshold, only
passing frequencies where the horizontal index X is less
than 16.
2. High-Pass Filter (Edge Extraction)
A high-pass filter blocks low-frequency components and retains only high frequencies. This isolates edges, fine textures, and sudden transitions in the video.
To zero out the low frequencies near the corners and retain high-frequency details:
ffmpeg -i input.mp4 -vf "fftfilt=weight_Y='gts(X,15)*gts(Y,15)'" -c:a copy output.mp4This expression discards any frequencies below the threshold of 15, resulting in a dark frame where only sharp edges are visible.
3. Creating a Custom Frequency Pattern
You can use trigonometric functions to create periodic pattern modifications. For example, to apply a horizontal band-like modulation to the video’s luminance plane:
ffmpeg -i input.mp4 -vf "fftfilt=weight_Y='1+0.5*sin(X/10)'" -c:a copy output.mp4This modifies the weight of the frequencies based on a sine wave
relative to the horizontal coordinate X, adding a stylized,
repeating distortion pattern to the output.
4. Modifying the Brightness (DC Coefficient)
If you want to manually offset the average brightness of the
luminance plane while leaving the frequency weights untouched, you can
directly set the dc_Y parameter:
ffmpeg -i input.mp4 -vf "fftfilt=dc_Y=128" -c:a copy output.mp4This forces the DC component of the luminance plane to a constant value of 128, altering the baseline exposure of the video frame.