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-i input.mp4: Specifies the input video.-vf "drawtext=...": Applies the text filter.text='Your Text Here': The actual text string you want to display.fontcolor=white: Sets the text color (supports standard names or hex codes like0xFFFFFF).fontsize=24: Sets the size of the font in pixels.
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.mp4On 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:
worW: Input video widthhorH: Input video heighttwortext_w: Rendered text widththortext_h: Rendered text height
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.mp4Perfectly 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.mp4Bottom-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.mp4Adding 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.mp4box=1: Enables the background box.boxcolor=black@0.5: Sets the box color to black with a 50% opacity (alpha) level.boxborderw=10: Adds a 10-pixel padding around the text inside the box.