FFmpeg testsrc2 Color Gradient Video Guide

This article provides a quick and practical guide on how to use the testsrc2 video source filter in FFmpeg to generate a color gradient test video. You will learn the basic command structure, how to customize the video’s resolution, frame rate, and duration, and how to render the final output file.

The testsrc2 filter is a built-in FFmpeg virtual input device that generates a visually detailed test pattern. This pattern features a continuous color gradient background, a moving color bar, and real-time rendering statistics like frame numbers and timestamps, making it ideal for testing encoders and color fidelity.

Basic Command Structure

To generate a default 10-second video using the testsrc2 filter, run the following command in your terminal:

ffmpeg -f lavfi -i testsrc2 -t 10 output.mp4

Here is what each parameter does: * -f lavfi: Tells FFmpeg to use the Libavfilter virtual input device. * -i testsrc2: Specifies testsrc2 as the input source. * -t 10: Sets the duration of the output video to 10 seconds. * output.mp4: The name and format of the resulting video file.

Customizing Resolution and Frame Rate

By default, testsrc2 generates a video at a resolution of 320x240 pixels at 25 frames per second (fps). You can customize these parameters directly inside the filter argument using the size (or s) and rate (or r) options.

To generate a Full HD (1080p) video at 60 fps, use this command:

ffmpeg -f lavfi -i testsrc2=size=1920x1080:rate=60 -t 5 output.mp4

In this command: * size=1920x1080 sets the width and height. * rate=60 sets the frame rate to 60 fps. * The colon : is used to separate multiple arguments within the filter.

Choosing the Right Encoder

By default, FFmpeg will choose an encoder based on your output file extension. To ensure high compatibility and compression efficiency, you can explicitly define the H.264 video encoder (libx264) and set the pixel format to yuv420p so the gradient plays correctly on all standard media players:

ffmpeg -f lavfi -i testsrc2=s=1280x720:r=30 -c:v libx264 -pix_fmt yuv420p -t 15 gradient_test.mp4