Generate Gradient Test Patterns with FFmpeg testsrc2

This guide explains how to generate synthetic gradient test pattern videos using FFmpeg's built-in testsrc2 filter source. You will learn the command syntax required to produce these patterns, the available parameters for customizing resolution, frame rate, and duration, and how to export the output as a video file or a still image.

Understanding the testsrc2 Filter

The testsrc2 filter is part of FFmpeg's libavfilter library. Unlike the standard testsrc filter, which generates solid color blocks and basic geometric figures, testsrc2 generates a continuous color gradient background alongside a moving rectangle and frame counter. This makes it ideal for testing color banding, display gradients, codec compression artifacts, and motion handling.

Basic Command Syntax

To generate a test pattern video using testsrc2, invoke FFmpeg using the lavfi (Libavfilter virtual input) format flag:

ffmpeg -f lavfi -i testsrc2 -t 10 -c:v libx264 output.mp4

In this command:

  • -f lavfi: Specifies the Libavfilter virtual input device.
  • -i testsrc2: Loads the testsrc2 filter source with default parameters (320x240 resolution, 25 fps).
  • -t 10: Sets the recording duration to 10 seconds.
  • -c:v libx264: Encodes the video stream using the H.264 codec.

Customizing Resolution, Frame Rate, and Duration

You can customize the output parameters directly inside the filter declaration using key-value pairs separated by colons:

ffmpeg -f lavfi -i testsrc2=size=1920x1080:rate=60:duration=15 -c:v libx264 output.mp4

Key configuration options include:

  • size (or s): Sets the frame dimensions (e.g., 1920x1080, 1280x720, or standard abbreviations like hd1080). The default is 320x240.
  • rate (or r): Defines the frame rate in frames per second (e.g., 30, 60, or 24000/1001). The default is 25.
  • duration (or d): Defines the stream duration in seconds. This can be used alternatively to the -t input flag.
  • alpha: Specifies the alpha value for the moving overlay, with values ranging from 0 to 255.

Generating a Still Gradient Image

If you only need a single reference frame of the gradient rather than a continuous video, output a single image by setting the frame count to one:

ffmpeg -f lavfi -i testsrc2=size=3840x2160 -vframes 1 gradient_pattern.png

This renders a single 4K Ultra HD frame containing the gradient pattern and exports it as a PNG file.