How to Add Text to Video Using FFmpeg Drawtext

Adding text overlays, watermarks, or subtitles to videos is a common editing task easily handled by FFmpeg’s powerful drawtext filter. This guide provides a straightforward tutorial on how to use the drawtext filter to overlay text onto your videos, covering basic syntax, font customization, positioning, and adding background boxes.

Basic Syntax of the drawtext Filter

To use the drawtext filter, you must apply it using the video filter (-vf) flag. A basic command requires you to specify the text you want to display, the font color, and the font size.

ffmpeg -i input.mp4 -vf "drawtext=text='Your Text Here':fontcolor=white:fontsize=24" output.mp4

Specifying a Font File

By default, FFmpeg will attempt to use a system font, but it is highly recommended to explicitly define a font file (.ttf or .otf) to ensure the text renders correctly across different operating systems.

ffmpeg -i input.mp4 -vf "drawtext=fontfile=/path/to/font.ttf:text='Hello World':fontcolor=white:fontsize=36" output.mp4

On Windows, paths usually point to C\:/Windows/Fonts/arial.ttf (note the escaped colon \:). On macOS and Linux, point to the appropriate directory in /Library/Fonts/ or /usr/share/fonts/.

Positioning the Text

You can position your text anywhere on the screen using the x (horizontal) and y (vertical) coordinates. FFmpeg provides built-in variables to make positioning easier:

Top-Left Corner (with margin)

To place text 20 pixels from the top and left edges:

ffmpeg -i input.mp4 -vf "drawtext=text='Top Left':fontcolor=yellow:fontsize=30:x=20:y=20" output.mp4

Perfectly Centered

To center the text horizontally and vertically:

ffmpeg -i input.mp4 -vf "drawtext=text='Centered Text':fontcolor=white:fontsize=30:x=(w-tw)/2:y=(h-th)/2" output.mp4

Bottom-Right Corner

To place text in the bottom-right corner with a 20-pixel margin:

ffmpeg -i input.mp4 -vf "drawtext=text='Bottom Right':fontcolor=white:fontsize=30:x=w-tw-20:y=h-th-20" output.mp4

Adding a Background Box

If your video background is busy, adding a solid or semi-transparent background box behind your text will make it much easier to read. Use the box, boxcolor, and boxborderw parameters:

ffmpeg -i input.mp4 -vf "drawtext=text='Readable Text':fontcolor=white:fontsize=28:box=1:boxcolor=black@0.5:boxborderw=10:x=(w-tw)/2:y=h-th-50" output.mp4