How to Create Audio Spectrum Video with FFmpeg

This article provides a step-by-step guide on how to convert an audio file into a visually appealing, real-time animated spectrum video using the FFmpeg showspectrum filter. You will learn the basic command structure, how to customize the visual output, and how to combine the generated video with the original audio track for a complete, shareable media file.

The showspectrum filter in FFmpeg converts input audio into a video stream that represents the audio frequency spectrum in real-time. This is highly useful for creating music visualizers for platforms like YouTube.

The Basic Command

To generate a simple audio spectrum video with default settings, use the following basic command:

ffmpeg -i input.mp3 -filter_complex "[0:a]showspectrum=s=1280x720[v]" -map "[v]" -map 0:a output.mp4

Here is a breakdown of what this command does: * -i input.mp3: Specifies your input audio file. * -filter_complex: Invokes FFmpeg’s complex filtergraph, which is required when handling multiple stream types or generating video from audio. * [0:a]showspectrum=s=1280x720[v]: Takes the audio stream from the first input ([0:a]), applies the showspectrum filter with a resolution size (s) of 1280x720 pixels, and outputs the resulting video stream labeled as [v]. * -map "[v]": Maps the newly generated video stream to the final output file. * -map 0:a: Maps the original audio stream from the input file to the final output file so the video has sound. * output.mp4: The final output video file.


Customizing the Visuals

The showspectrum filter comes with several parameters that allow you to customize the appearance of the animation.

1. Color Schemes (color)

You can change the color palette of the spectrum using the color option. Available presets include channel, mby, rainbow, cool, fiery, and intensity.

ffmpeg -i input.mp3 -filter_complex "[0:a]showspectrum=s=1280x720:color=fiery[v]" -map "[v]" -map 0:a output.mp4

2. Frequency Scale (scale)

The scale option determines how frequencies are spaced visually. Options include linear, sqrt (square root), cbrt (cubic root), and log (logarithmic). Logarithmic or cubic scales often look best for musical tracks.

ffmpeg -i input.mp3 -filter_complex "[0:a]showspectrum=s=1280x720:scale=log[v]" -map "[v]" -map 0:a output.mp4

3. Display Mode (mode)

You can choose how the channels are displayed. * combined: Overlaps all channels into a single display. * separate: Separates channels into distinct rows (ideal for stereo audio).

ffmpeg -i input.mp3 -filter_complex "[0:a]showspectrum=s=1280x720:mode=separate[v]" -map "[v]" -map 0:a output.mp4

4. Slide Mode (slide)

This control how the spectrum animation moves across the screen. Options include flow, scroll, pitch, and replace.

ffmpeg -i input.mp3 -filter_complex "[0:a]showspectrum=s=1280x720:slide=scroll[v]" -map "[v]" -map 0:a output.mp4

High-Quality Render Example

To ensure the output video is highly compatible with web browsers and social media platforms, you should specify the H.264 video codec (libx264) and set the pixel format to yuv420p.

Below is an optimized, production-ready command that combines multiple customization options:

ffmpeg -i input.mp3 -filter_complex "[0:a]showspectrum=s=1920x1080:color=rainbow:scale=cbrt:slide=scroll[v]" -map "[v]" -map 0:a -c:v libx264 -pix_fmt yuv420p -c:a aac -b:a 320k output.mp4

In this optimized command: * s=1920x1080: Renders the video in Full HD. * color=rainbow: Uses a vibrant, multi-colored spectrum. * scale=cbrt: Emphasizes mid-and-low frequencies visually. * -c:v libx264: Encodes the video using H.264. * -pix_fmt yuv420p: Ensures maximum playback compatibility across devices. * -c:a aac -b:a 320k: Re-encodes the audio to high-quality AAC at 320 kbps.