FFmpeg drawgrid: Configure Spacing, Thickness, and Color

The FFmpeg drawgrid video filter allows you to overlay a grid on top of a video, which is highly useful for video analysis, alignment, and editing. This guide provides a straightforward explanation of how to configure the key parameters of the drawgrid filter—specifically grid spacing, line thickness, and color—complete with practical command-line examples.

The Basic Syntax

To use the drawgrid filter, you apply it via the -vf (video filter) flag. The basic parameter structure for defining spacing, thickness, and color is:

drawgrid=w=width:h=height:t=thickness:c=color
  • w (width): The width of each grid cell in pixels.
  • h (height): The height of each grid cell in pixels.
  • t (thickness): The thickness of the grid lines in pixels (default is 1).
  • c (color): The color of the grid lines.

Configuring Grid Spacing

You can set grid spacing using absolute pixel values or relative expressions based on the input video dimensions.

Using Fixed Pixel Values

To create a grid where each cell is 100 pixels wide and 100 pixels high:

ffmpeg -i input.mp4 -vf "drawgrid=w=100:h=100" output.mp4

Using Relative Expressions

You can use variables like iw (input width) and ih (input height) to make the grid scale with the video resolution. For example, to create a 3x3 "rule of thirds" grid, divide the input width and height by 3:

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

Configuring Line Thickness

The t parameter controls the thickness of the grid lines in pixels. If you want highly visible lines, increase this value.

To draw a grid with a line thickness of 5 pixels:

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

Configuring Grid Color

The c parameter accepts standard color names (such as red, blue, yellow, white, black) or hexadecimal color values. You can also append an opacity value to make the grid semi-transparent.

Using Color Names

To draw a red grid:

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

Using Hex Codes and Transparency

To use a specific hex color (e.g., neon green #00FF00) with 50% opacity, append @0.5 to the color definition:

ffmpeg -i input.mp4 -vf "drawgrid=w=100:h=100:c=0x00FF00@0.5" output.mp4

Practical Example: Combining All Configurations

To put it all together, the following command creates a grid with cells scaled to 1/10th of the video size, a line thickness of 3 pixels, and a semi-transparent yellow color:

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