How to Create a Spectrogram Video with FFmpeg

This article provides a straightforward guide on how to use FFmpeg's showspectrum filter to convert any audio file into a visually dynamic, animated spectrogram video. You will learn the basic command structure, understand the key parameters for customizing the visualization, and see practical examples to help you generate high-quality audio visualizers.

The showspectrum filter in FFmpeg converts input audio into a video stream that represents the audio frequency spectrum over time. To generate a basic animated spectrogram video from an audio file, use the following command:

ffmpeg -i input.mp3 -filter_complex "showspectrum=s=1280x720:mode=combined:color=rainbow:scale=sqrt" -c:a copy output.mp4

Parameter Breakdown

To customize the look and feel of your spectrogram, you can adjust several parameters inside the -filter_complex flag:

  • s (Size): Defines the resolution of the output video. In the example above, s=1280x720 sets the resolution to 720p HD. You can change this to 1920x1080 for Full HD.
  • mode: Controls how multi-channel audio is displayed.
    • combined: Merges all channels into a single visualization.
    • separate: Displays each channel (e.g., Left and Right) in its own separate row.
  • color: Sets the color map for the spectrum. Popular options include rainbow, cool, fire, intensity, magma, and viridis.
  • scale: Sets the amplitude scale, which dictates how the intensity of frequencies is scaled visually.
    • lin: Linear scale.
    • sqrt: Square root scale (good for general visualization).
    • log: Logarithmic scale (better for representing human hearing perception).
  • slide: Defines how the spectrum moves across the screen.
    • replace: The drawing starts over from the left once it reaches the right edge.
    • scroll: The spectrum continuously scrolls from right to left.

Advanced Example with Custom Styling

For a more detailed, high-resolution visualization with a logarithmic scale and a scrolling effect, you can run:

ffmpeg -i input.wav -filter_complex "showspectrum=s=1920x1080:mode=separate:color=fire:scale=log:slide=scroll" -c:v libx264 -pix_fmt yuv420p -b:a 320k output.mp4

In this command:

  • The video resolution is set to 1080p (1920x1080).
  • The audio channels are visualised separately (mode=separate) using a hot color palette (color=fire).
  • The spectrum scrolls continuously (slide=scroll).
  • -pix_fmt yuv420p is added to ensure the output video is compatible with standard media players and web browsers.
  • The audio is re-encoded to AAC at 320 kbps (-b:a 320k) to maintain high audio fidelity.