How to Use FFmpeg Drawgrid Filter to Overlay a Grid

This article provides a quick guide on how to use the FFmpeg drawgrid video filter to overlay grid lines onto your videos. You will learn the basic command syntax, the essential parameters for customizing the grid’s size, position, color, and thickness, and practical examples like creating a “rule of thirds” grid.

The Basic Syntax

To draw a grid on a video, you use the -vf (video filter) flag followed by the drawgrid filter name and its parameters. The basic structure of the command looks like this:

ffmpeg -i input.mp4 -vf "drawgrid=w=width:h=height:t=thickness:c=color" output.mp4

Key Parameters Explained

The drawgrid filter relies on several key parameters to define how the grid looks and where it is positioned:

Practical Examples

1. Drawing a Simple 100x100 Pixel Grid

This command overlays a red grid where each square is 100 pixels wide and 100 pixels high, with a line thickness of 2 pixels.

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

2. Creating a Rule of Thirds Grid

To create a standard photographic “rule of thirds” grid, you can use mathematical expressions with the input video dimensions (iw and ih). This divides the screen into a 3x3 grid regardless of the video’s resolution.

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

Note: white@0.5 creates a semi-transparent white line so the grid does not completely block the underlying video.

3. Creating a Centered Crosshair

If you only want a horizontal and vertical line intersecting directly in the center of the screen, set the grid width and height to half of the input dimensions, and offset the start point.

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

4. Specifying Grid Colors with Hex Codes

You can use hexadecimal values to match exact brand colors. The following command overlays a blue grid using a hex code:

ffmpeg -i input.mp4 -vf "drawgrid=w=50:h=50:t=1:c=0x0000FF" output.mp4