Use FFmpeg fftfilt for 2D Frequency Domain Filtering
This article explains how to use the FFmpeg fftfilt
filter to apply 2D frequency-domain filtering to video streams. You will
learn the core concepts behind the filter, the mathematical variables
used to target specific frequencies, and practical command-line examples
for implementing low-pass, high-pass, and custom band-pass filters to
modify your video’s spatial frequencies.
Understanding the fftfilt Filter
The fftfilt filter in FFmpeg performs a 2D Fast Fourier
Transform (FFT) on each video frame to transition it from the spatial
domain to the frequency domain. In this domain, you can manipulate
specific frequency coefficients using custom mathematical expressions.
After applying your filter, FFmpeg performs an Inverse Fast Fourier
Transform (IFFT) to convert the frame back into the viewable spatial
domain.
This process is highly effective for tasks like noise reduction, image sharpening, edge detection, and removing repetitive pattern noise (like moiré patterns).
Basic Syntax and Variables
The basic syntax for the fftfilt filter is:
ffmpeg -i input.mp4 -vf "fftfilt=weight_Y='expression':weight_U='expression':weight_V='expression'" output.mp4You can target the Luma (Y) and Chroma (U,
V) channels independently.
To define your filter expressions, FFmpeg provides several built-in variables representing the frequency coordinates:
XandY: The horizontal and vertical coordinate of the current frequency coefficient.WandH: The width and height of the channel matrix.hypot(X,Y): The distance from the top-left corner (0,0), which represents the lowest frequency (DC component). Ashypot(X,Y)increases, you target higher spatial frequencies.
The output of your expression acts as a multiplier (weight) for each
frequency coefficient. A weight of 1 keeps the frequency
intact, while 0 completely removes it.
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 blurred or smoothed image, which is useful for removing high-frequency grain and noise.
To keep only the frequencies within a radius of 30 pixels from the origin:
ffmpeg -i input.mp4 -vf "fftfilt=weight_Y='lte(hypot(X,Y),30)'" output.mp4lte(hypot(X,Y),30)evaluates to1if the frequency coordinate is within 30 pixels of the origin, and0otherwise.
2. High-Pass Filter (Edge Detection / Sharpening)
A high-pass filter blocks low frequencies (smooth gradients) and passes high frequencies (sharp details and edges).
To eliminate all frequencies within a radius of 15 pixels of the origin:
ffmpeg -i input.mp4 -vf "fftfilt=weight_Y='gt(hypot(X,Y),15)'" output.mp4gt(hypot(X,Y),15)evaluates to1for all high frequencies beyond a radius of 15, removing the overall illumination and leaving only sharp transitions and edges.
3. Band-Pass Filter
A band-pass filter isolates a specific range of frequencies. This is highly useful if you want to target and eliminate a specific repetitive pattern or frequency band of noise.
To pass only frequencies between a radius of 10 and 50:
ffmpeg -i input.mp4 -vf "fftfilt=weight_Y='between(hypot(X,Y),10,50)'" output.mp44. Custom Frequency Modification (Sharpening Effect)
Instead of completely cutting off frequencies with 1 and
0, you can amplify certain frequencies to sharpen the
video. For example, to boost mid-to-high frequencies while leaving low
frequencies untouched:
ffmpeg -i input.mp4 -vf "fftfilt=weight_Y='1+gt(hypot(X,Y),20)*0.5'" output.mp4- This expression keeps the base weight at
1for low frequencies, but multiplies frequencies beyond a radius of 20 by1.5, boosting fine image details.