How to Generate Spectrogram Video with FFmpeg

This guide explains how to convert an audio file into a dynamic, visual spectrogram video using the showspectrum filter in FFmpeg. You will learn the basic command syntax, how to map both audio and video streams correctly, and how to customize the visual appearance using different color palettes, dimensions, and frequency scaling modes.

Basic Spectrogram Generation

To convert an audio file into a video spectrogram while retaining the original audio track, run the following command in your terminal:

ffmpeg -i input.mp3 -filter_complex "[0:a]showspectrum=s=1920x1080:mode=combined:color=intensity:slide=scroll[v]" -map "[v]" -map 0:a -c:v libx264 -pix_fmt yuv420p -c:a aac output.mp4

Explanation of Core Options

  • -i input.mp3: Specifies the input audio source.
  • -filter_complex: Applies the complex filter graph to generate a video stream from the audio stream.
  • [0:a]: Targets the primary audio stream of the first input file.
  • [v]: Labels the newly generated video output from the filter.
  • -map "[v]" -map 0:a: Ensures the output contains both the generated spectrogram video stream and the original audio stream.
  • -c:v libx264 -pix_fmt yuv420p: Encodes the video with H.264 using a pixel format compatible with most modern media players.
  • -c:a aac: Re-encodes the audio to AAC format for MP4 container compatibility. Use -c:a copy if the input is already compatible.

Customizing the showspectrum Filter

The showspectrum filter accepts several parameters separated by colons (:) to control how the visualization behaves:

  • s (Size): Sets the output video resolution, such as 1280x720 or 1920x1080.
  • mode: Controls how audio channels are displayed. Options include combined (merges all channels), separate (displays each channel on its own row), and channel (displays individual channels).
  • color: Sets the color map used to represent signal intensity. Common options include channel, intensity, rainbow, fire, magma, and viridis.
  • slide: Defines how the spectrogram moves across the screen. Options include scroll (new samples appear on the right and scroll left), replace (a scanning bar overwrites the screen), and fullframe (updates the whole frame per step).
  • fscale: Configures the frequency axis. Use lin for linear scaling or log for logarithmic scaling, which better reflects human hearing perception.
  • scale: Configures the amplitude/intensity scaling. Options include lin, sqrt, cbrt, 4thrt, and log.

High-Quality Logarithmic Example

For musical audio, logarithmic frequency scaling with a distinct color gradient often produces the best result. The following command generates a 60 FPS, 1080p spectrogram with logarithmic frequency mapping and the magma color palette:

ffmpeg -i input.wav -filter_complex "[0:a]showspectrum=s=1920x1080:mode=combined:color=magma:slide=scroll:fscale=log:fps=60[v]" -map "[v]" -map 0:a -c:v libx264 -pix_fmt yuv420p -b:v 5M -c:a copy output.mp4