How to Draw a Blue Rectangle in FFmpeg with Drawbox

This guide explains how to use FFmpeg's drawbox video filter to overlay a solid blue rectangle onto a specific region of a video. By defining the horizontal and vertical coordinates, width, height, and color parameters, you can easily mask, censor, or highlight specific areas within your video stream.

The Basic Command

To draw a solid blue rectangle, use the -vf (video filter) option with the drawbox filter.

ffmpeg -i input.mp4 -vf "drawbox=x=100:y=100:w=300:h=200:color=blue:t=fill" -c:a copy output.mp4

Parameter Breakdown

  • x: The horizontal position of the box's top-left corner, measured in pixels from the left edge of the video frame.
  • y: The vertical position of the box's top-left corner, measured in pixels from the top edge of the video frame.
  • w: The width of the rectangle in pixels.
  • h: The height of the rectangle in pixels.
  • color: The color of the rectangle. You can use standard color names (like blue) or hexadecimal notation (such as 0x0000FF). You can also append an opacity value using the @ symbol (e.g., blue@1.0 for 100% opacity or blue@0.5 for 50% transparency).
  • t: The thickness of the border. Setting t=fill fills the entire rectangle, making it solid. If this parameter is omitted or set to a number, FFmpeg will only draw an outline.

Using Precise Color Codes

If you need a specific shade of blue, define it using a hex color code:

ffmpeg -i input.mp4 -vf "drawbox=x=50:y=50:w=400:h=250:color=#0055FF:t=fill" -c:a copy output.mp4

Centering the Box Dynamically

FFmpeg supports mathematical expressions and built-in variables such as in_w (input width) and in_h (input height). To center a 400x300 blue box in the frame:

ffmpeg -i input.mp4 -vf "drawbox=x=(in_w-400)/2:y=(in_h-300)/2:w=400:h=300:color=blue:t=fill" -c:a copy output.mp4