How to Use the FFmpeg Histogram Filter

This article provides a comprehensive guide on using the FFmpeg histogram filter to visualize the color and luminance distribution of your videos. You will learn the core syntax, explore different display modes such as waveform and levels, and see practical examples of how to overlay histograms onto your videos for professional-grade analysis.

Understanding the Basic Syntax

The histogram filter takes an input video stream and outputs a new video containing only the histogram representation of that video. By default, the output video has a resolution of 256x256 pixels per component.

The simplest command to convert an input video into its histogram representation is:

ffmpeg -i input.mp4 -vf "histogram" output.mp4

Selecting Different Display Modes

FFmpeg supports multiple ways to display histogram data. You can customize this by using the mode parameter.

To specify a mode, append the mode parameter to the filter:

ffmpeg -i input.mp4 -vf "histogram=mode=waveform" output.mp4

Overlaying the Histogram onto the Original Video

To analyze a video in real time, you usually need to view the histogram overlaid on top of the original video. This is achieved by splitting the input video stream, turning one stream into a histogram, scaling it, and overlaying it back onto the original stream.

Use the following filtergraph to overlay a 320-pixel wide histogram in the top-right corner of your video:

ffmpeg -i input.mp4 -vf "split [main][tmp]; [tmp] histogram, scale=320:-1 [hist]; [main][hist] overlay=W-w-10:10" output.mp4

How this filtergraph works:

  1. split: Splits the input video into two identical streams named [main] and [tmp].
  2. histogram: Converts the [tmp] stream into a histogram.
  3. scale: Downscales the histogram to a width of 320 pixels while maintaining its aspect ratio.
  4. overlay: Places the downscaled histogram [hist] on top of the original [main] video, positioned 10 pixels from the right edge (W-w-10) and 10 pixels from the top edge (10).

Advanced Configuration Options

You can fine-tune the histogram output by adjusting additional parameters inside the filter:

Example: Logarithmic Waveform in Parade Mode

To generate a waveform histogram using a logarithmic scale with components displayed side-by-side, use:

ffmpeg -i input.mp4 -vf "histogram=mode=waveform:scale=log:display_mode=parade" output.mp4