How to Use FFmpeg showspectrumpic
This article provides a straightforward guide on how to use the
FFmpeg showspectrumpic filter to generate a visual
spectrogram from an audio file. You will learn the exact commands to
create static spectrogram images, customize their appearance (such as
size, color, and scale), and convert the resulting image into a video
file to pair with your audio.
Basic showspectrumpic Command
The showspectrumpic filter is designed to analyze an
entire audio file and output a single, high-resolution static image of
its frequency spectrum.
To generate a basic spectrogram image from an audio file, use the following command:
ffmpeg -i input.mp3 -lavfi showspectrumpic output.pngCustomizing the Spectrogram
You can customize the output image by passing parameters to the filter. Common options include:
s(Size): Sets the resolution of the output image (width x height).color(Color channel): Sets the color map. Options includerainbow,cool,hot,intensity, andgreen.scale(Frequency scale): Sets the scale display. Options includelin(linear),log(logarithmic),sqrt(square root), and4thrt(fourth root).legend(Legend): Enables or disables the frequency and decibel legend (1 to enable, 0 to disable).
Example with Custom Parameters:
To create a 1920x1080 logarithmic spectrogram with a “cool” color scheme and a legend, use this command:
ffmpeg -i input.mp3 -lavfi "showspectrumpic=s=1920x1080:scale=log:color=cool:legend=1" output.pngCreating a Video from the Spectrogram Image
Because showspectrumpic only outputs a static image, you
must combine that image back with your original audio file to create a
video. You can do this by looping the static image for the duration of
the audio:
ffmpeg -loop 1 -i output.png -i input.mp3 -c:v libx264 -tune stillimage -c:a aac -b:a 192k -pix_fmt yuv420p -shortest video_spectrogram.mp4Command breakdown:
-loop 1: Loops the input image.-tune stillimage: Optimizes the video encoder for static images.-pix_fmt yuv420p: Ensures maximum compatibility across video players.-shortest: Stops encoding when the audio file ends.
Alternative: Direct Video Spectrogram
If you prefer a dynamic, moving video spectrogram instead of a static
image, use the showspectrum filter instead of
showspectrumpic. This generates a real-time animated video
representation of the audio frequencies:
ffmpeg -i input.mp3 -filter_complex "[0:a]showspectrum=s=1280x720:mode=combined:color=rainbow[v]" -map "[v]" -map 0:a -c:v libx264 -c:a aac output.mp4