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.mp4Key Parameters Explained
The drawgrid filter relies on several key parameters to
define how the grid looks and where it is positioned:
w(width): The width of each grid cell in pixels. You can use numbers (e.g.,100) or variables likeiw(input width).h(height): The height of each grid cell in pixels. You can use numbers or variables likeih(input height).x(horizontal offset): The starting X-coordinate of the grid. Defaults to0.y(vertical offset): The starting Y-coordinate of the grid. Defaults to0.torthickness: The thickness of the grid lines in pixels. Defaults to1.corcolor: The color of the grid lines. You can specify color names (e.g.,red,blue,white) or hex codes (e.g.,0x00FF00). You can also append transparency using@(e.g.,white@0.5for 50% opacity).
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.mp42. 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.mp4Note: 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.mp44. 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