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:
w(orwidth): The width of each grid cell. This can be a pixel value or an expression (e.g.,iw/3for one-third of the input width).h(orheight): The height of each grid cell.x: The horizontal offset for the first grid line (default is 0).y: The vertical offset for the first grid line (default is 0).c(orcolor): The color of the grid lines. You can use standard color names (likered,blue,white) or hex codes. You can also specify opacity, such aswhite@0.5.t(orthickness): The thickness of the grid lines in pixels (default is 1).
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.mp42. 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.mp4This 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.mp4In this example, the grid starts 50 pixels away from the top-left corner, and the white grid lines are rendered with 40% opacity.