FFmpeg Drawbox: Position, Thickness, and Color
This guide explains how to use the FFmpeg drawbox video
filter to overlay custom rectangular shapes onto your videos. You will
learn how to precisely define the position, dimensions, border
thickness, and color of the box using standard FFmpeg command-line
syntax.
The Drawbox Filter Syntax
The basic syntax for the drawbox filter uses key-value
pairs separated by colons. The primary parameters required to customize
the box are:
drawbox=x=X_VAL:y=Y_VAL:w=WIDTH:h=HEIGHT:color=COLOR:t=THICKNESS1. Configuring Position (x, y) and Size (w, h)
The position of the box is determined by the x and
y coordinates of its top-left corner. The size is
controlled by the width (w) and height (h)
parameters.
x: The horizontal position of the top-left corner (in pixels) from the left edge of the video.y: The vertical position of the top-left corner (in pixels) from the top edge of the video.w: The width of the rectangle in pixels.h: The height of the rectangle in pixels.
You can use mathematical expressions and variables such as
iw (input width) and ih (input height). For
example, to center a \(200 \times 150\)
box, you can use: x=(iw-200)/2:y=(ih-150)/2:w=200:h=150
2. Configuring Color (color)
The color parameter sets the color of the rectangle’s
border (or fill). FFmpeg supports: * Color names:
Standard names like red, blue,
black, white, or yellow. *
Hexadecimal values: HTML-style hex codes, such as
0x00FF00 for green or 0xFF0000 for red. *
Opacity (Alpha channel): You can append an
@ symbol followed by a value between 0.0
(fully transparent) and 1.0 (fully opaque). For example,
red@0.5 creates a semi-transparent red box.
3. Configuring Thickness (t)
The thickness parameter t (or thickness)
controls the border width in pixels. * Border: Set
t to a specific pixel value (e.g., t=5 for a
5-pixel thick border). * Filled Box: To fill the entire
rectangle with solid color, set t=fill (or
t=max).
Complete Example Command
The following command overlays a red, semi-transparent rectangle that is 300 pixels wide, 200 pixels high, positioned 100 pixels from the left and 50 pixels from the top, with a border thickness of 8 pixels:
ffmpeg -i input.mp4 -vf "drawbox=x=100:y=50:w=300:h=200:color=red@0.6:t=8" -c:a copy output.mp4