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:

Available Variables

Within your expressions, you can use the following variables to target specific frequency regions:

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.mp4

In 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.mp4

This 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.mp4

This 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.mp4

This forces the DC component of the luminance plane to a constant value of 128, altering the baseline exposure of the video frame.