Overlay a Custom Grid on Video Using FFmpeg drawgrid

This guide demonstrates how to overlay a custom visual grid onto any video using FFmpeg's built-in drawgrid video filter. You will learn the core syntax, understand the fundamental parameters for customizing grid dimensions, colors, and line thickness, and explore practical command-line examples such as creating standard pixel grids and a dynamic "rule of thirds" overlay.


Understanding the drawgrid Filter Syntax

The drawgrid filter is passed to FFmpeg via the -vf (video filter) flag. Its basic syntax follows key-value pairs separated by colons:

ffmpeg -i input.mp4 -vf "drawgrid=w=WIDTH:h=HEIGHT:t=THICKNESS:c=COLOR" -c:a copy output.mp4

Key Parameters

To customize the grid, you can configure several primary parameters:

  • w (width): The width of each grid cell in pixels. Can be a fixed number or an expression (e.g., iw/3 for one-third of the input width).
  • h (height): The height of each grid cell in pixels. Can also be an expression (e.g., ih/3).
  • t (thickness): The thickness of the grid lines in pixels. Defaults to 0 (which draws a 1-pixel line).
  • c (color): The color of the lines. Accepts color names (e.g., red, blue, white) or hex values (e.g., 0xFFFFFF), with an optional opacity parameter defined by an @ symbol (e.g., white@0.5 for 50% opacity).
  • x (horizontal offset): The horizontal starting coordinate of the first grid line. Defaults to 0.
  • y (vertical offset): The vertical starting coordinate of the first grid line. Defaults to 0.

Practical Examples

1. Basic Fixed-Pixel Grid

To draw a simple 100x100 pixel grid across the entire video with 2-pixel thick red lines:

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

2. Semi-Transparent Rule of Thirds Overlay

You can use variables like iw (input width) and ih (input height) to create proportional grids. To create a standard 3x3 photography/cinematography framing grid with semi-transparent yellow lines:

ffmpeg -i input.mp4 -vf "drawgrid=w=iw/3:h=ih/3:t=2:c=yellow@0.6" -c:a copy output.mp4

3. Centered Grid with Offsets

If you want the grid lines to align from a specific starting coordinate rather than the top-left edge (0,0), define the x and y offsets:

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

4. High-Precision Crosshair / 2x2 Grid

To split a video directly down the center into four equal quadrants:

ffmpeg -i input.mp4 -vf "drawgrid=w=iw/2:h=ih/2:t=1:c=green" -c:a copy output.mp4