Draw Text with Semi-Transparent Box in FFmpeg

Adding text overlay to a video often requires a background box to ensure the text remains legible against varying background colors. This article provides a straightforward guide on how to use FFmpeg's drawtext filter to create a highly readable text overlay with a semi-transparent black background box.

To draw a semi-transparent background box behind your text, you must use the drawtext video filter with three specific parameters: box, boxcolor, and boxborderw.

The FFmpeg Command

Here is the standard command structure to achieve this effect:

ffmpeg -i input.mp4 -vf "drawtext=text='Your Text Here':fontcolor=white:fontsize=24:box=1:boxcolor=black@0.5:boxborderw=10:x=(w-text_w)/2:y=(h-text_h)/2" -codec:a copy output.mp4

Parameter Breakdown

  • box=1: This enables the background box behind the text. By default, it is set to 0 (disabled).
  • boxcolor=black@0.5: This defines the color and the opacity of the background box. The color is set to black, and the @0.5 specifies an opacity of 50%. You can adjust this decimal value from 0.0 (fully transparent) to 1.0 (fully opaque). Alternatively, you can use hex values, such as 0x000000@0.5.
  • boxborderw=10: This acts as padding. It defines the width of the border (in pixels) around the text, expanding the background box so the text does not touch the edges.
  • fontcolor=white: Sets the color of the text to white, which contrasts well against the black semi-transparent background.
  • x=(w-text_w)/2:y=(h-text_h)/2: Centers the text box horizontally and vertically on the screen.

Customizing the Transparency and Padding

If you want a darker, more prominent box with more padding, you can increase the alpha value and the border width:

boxcolor=black@0.75:boxborderw=20

This configuration increases the opacity of the black box to 75% and expands the padding to 20 pixels, ensuring the text is easily readable even on highly detailed or bright video backgrounds.