How to Use the FFmpeg drawgrid Filter

This article provides a practical guide on how to use the drawgrid filter in FFmpeg to overlay a grid on your videos. You will learn the basic syntax, key parameters such as cell size, position, line thickness, and color, and see real-world examples ranging from simple grids to creating a “rule of thirds” overlay for video analysis.

The drawgrid filter in FFmpeg allows you to draw a grid of colored lines over an input video. This is highly useful for video analysis, alignment, calibration, or creating specific visual effects.

Basic Syntax and Parameters

The basic syntax for the drawgrid filter is:

-vf "drawgrid=w=width:h=height:x=x_offset:y=y_offset:c=color:t=thickness"

Here are the primary parameters you can configure:

Practical Examples

1. Draw a Simple 100x100 Pixel Grid

To overlay a simple red grid where each square is 100 by 100 pixels, and the lines are 2 pixels thick, use the following command:

ffmpeg -i input.mp4 -vf "drawgrid=w=100:h=100:t=2:c=red" output.mp4

2. Create a “Rule of Thirds” Grid

For video editing and composition analysis, you may want to divide the screen into a 3x3 grid. You can achieve this by using the input width (iw) and input height (ih) variables:

ffmpeg -i input.mp4 -vf "drawgrid=w=iw/3:h=ih/3:t=3:c=blue" output.mp4

This command automatically calculates the dimensions of the grid cells based on the resolution of the input video, drawing blue lines that divide the screen into equal thirds.

3. Grid with Offsets and Semi-Transparent Lines

If you want to shift the grid starting point and make the lines less distracting, you can define offsets (x and y) and use a semi-transparent white color:

ffmpeg -i input.mp4 -vf "drawgrid=x=50:y=50:w=200:h=200:t=1:c=white@0.4" output.mp4

In this example, the grid starts 50 pixels away from the top-left corner, and the white grid lines are rendered with 40% opacity.