FFmpeg showspectrum: Window Function and Color Scale
The FFmpeg showspectrum filter transforms an audio
stream into a visual frequency spectrum, rendering audio amplitude and
frequencies over time. This guide explains how to customize this filter
by setting different window functions to control spectral leakage and
resolution, as well as applying custom color scales to optimize visual
clarity and contrast.
Understanding the
showspectrum Filter
The showspectrum filter takes an audio input and
generates a video output representing the audio's frequency
distribution. The two most critical parameters for visual tuning are
win_func, which sets the Discrete Fourier Transform (DFT)
windowing function, and color, which dictates the color
palette used to represent amplitude intensity.
Configuring the Window
Function (win_func)
A window function mitigates spectral leakage, which occurs when a signal's frequency does not align perfectly with discrete FFT bins. Different functions offer trade-offs between frequency resolution (main lobe sharpness) and amplitude accuracy (side lobe attenuation).
To configure the window function, pass the win_func (or
shorthand w) option inside the filter string:
ffmpeg -i input.mp3 -filter_complex "showspectrum=win_func=blackman" -pix_fmt yuv420p output.mp4Common window options include:
hann: The default window. Provides a balanced compromise between resolution and leakage suppression.hamming: Similar to Hann, offering slightly better main-lobe resolution but higher distant sidelobes.blackman: Strongly suppresses spectral leakage, making it ideal for clean, high-contrast visualization at the cost of slight peak-width broadening.rect: A rectangular window (no windowing). Provides the sharpest main lobe but severe leakage; best for detecting exact frequencies of pure sine waves.flattop: Minimizes amplitude measurement error, useful for measuring exact decibel levels across bins.kaiser: A flexible window that allows balancing sidelobes against lobe width.
Configuring the Color Scale
(color)
The color (or shorthand c) option defines
the color palette applied to the spectrum based on the signal intensity.
Selecting the right color map improves readability depending on whether
you are viewing subtle background noise or loud transient peaks.
Specify the palette name with the color parameter:
ffmpeg -i input.mp3 -filter_complex "showspectrum=color=viridis" -pix_fmt yuv420p output.mp4Supported color maps include:
channel: The default mode. Generates a distinct color for each audio channel.intensity: Single-channel intensity gradient (grayscale by default).rainbow: A classic multi-color spectrum ranging across common visible wavelengths.moreland: A smooth divergent color map ranging from blue to red.nebula: A high-contrast purple-blue-white space palette.fire/fiery: Warm palettes starting from black/red to yellow/white.fruit: A vibrant palette utilizing fruit-inspired hues.cool: A cool blue-to-cyan temperature gradient.magma: A perceptually uniform sequential palette running from black through purple and orange to light yellow.viridis: A perceptually uniform palette running from dark purple through blue and green to bright yellow, recommended for accessibility and accurate gradient representation.
Adjusting Intensity Scaling
(scale)
To ensure the chosen color palette distributes evenly across quiet
and loud signals, adjust the scale parameter alongside the
color configuration:
lin: Linear amplitude scale.sqrt: Square root scale (default), useful for general listening material.cbrt: Cubic root scale, reveals quieter background details.log: Logarithmic scale, standard for decibel-based visual representations.4thrt/5thrt: Fourth- and fifth-root scales for extreme dynamic range visualization.
Complete Working Example
To render a 1920x1080 spectrum using a blackman window
function, logarithmic frequency spacing, a magma color map,
and a logarithmic amplitude scale, use the following command:
ffmpeg -i input.wav -filter_complex \
"showspectrum=s=1920x1080:win_func=blackman:color=magma:scale=log:fscale=log" \
-c:v libx264 -pix_fmt yuv420p -c:a copy output.mp4To preview the spectrum in real time instead of saving to a file,
substitute ffmpeg with ffplay:
ffplay -f lavfi "amovie=input.wav,asplit[a][v];[v]showspectrum=s=1280x720:win_func=hann:color=viridis:scale=cbrt[out]"