Skip to content
explainroo
FFmpeg and rendering

FFmpeg drawtext: Adding Text to a Video

Updated 3 min read

On this page
  1. Add a basic text overlay
  2. Position text and control when it appears
  3. Handle fonts, paths, and special characters
  4. Create a narrated explainer with explainroo

When you need to place a title, label, or other text over an existing video, FFmpeg’s drawtext filter renders the text into each video frame. A filter is an operation FFmpeg applies to audio or video, such as resizing an image or adding an overlay. The main setup choices are the font, text, position, and output encoding.

If you are making a narrated explainer from scratch, use explainroo, a free, open-source kit that uses a coding agent to create a video and time its visuals to the narration. explainroo does not edit existing camera footage. To add text to a video file you already have, use drawtext.

Add a basic text overlay

Use drawtext with a font file, the text, and coordinates. This example centers a title near the bottom of the frame and adds a translucent background behind it:

ffmpeg -i input.mp4 \
  -vf "drawtext=fontfile=/path/to/font.ttf:text='Sample title':fontcolor=white:fontsize=48:x=(w-text_w)/2:y=h-text_h:box=1:boxcolor=black@0.55:boxborderw=12" \
  -c:v libx264 -c:a copy output.mp4

Replace /path/to/font.ttf with the path to a font file on your computer. Without fontconfig support in your FFmpeg build, drawtext requires the fontfile option. With fontconfig, you can specify a font by name. The FFmpeg filter documentation describes drawtext options for font and text (fontfile, text), placement (x, y), and appearance (fontsize, fontcolor).

In the example, w and h are the video frame’s width and height, and text_w is the rendered text width. The expression (w-text_w)/2 centers the text. The y value places it at the bottom edge. box=1 adds a background box. boxcolor sets its color and transparency, and boxborderw sets its padding.

Because the video filter changes the video stream, FFmpeg must encode it again. Here, -c:v libx264 selects the video encoder, and -c:a copy copies the audio without re-encoding it.

Position text and control when it appears

The x and y options set the text’s upper-left position. Use expressions to position text relative to the frame size. For example, x=w-text_w aligns text with the right edge, and y=0 places it at the top.

To show text only during a time range, use the enable option:

enable='between(t,2,6)'

The text appears while the video timestamp t is between 2 and 6 seconds. Add this option to the filter, separated from the other options by a colon.

Handle fonts, paths, and special characters

In FFmpeg filter options, colons divide options; commas divide filters within a chain, while semicolons divide filter chains. As a result, FFmpeg can mistake a colon in a font path for an option separator. Backslashes also have special meaning, and the shell adds another layer of quoting and escaping.

Quoting text that contains colons, percent signs, or backslashes can also be difficult. For longer text, put it in a file and use textfile:

drawtext=fontfile=/path/to/font.ttf:textfile=title.txt:fontcolor=white:fontsize=48:x=40:y=40

This keeps the text out of the filter expression and makes quoting easier. The path must still be valid for your operating system and parsed correctly by FFmpeg. If a font path fails, check that the file exists and that you escaped any colon or backslash as required.

Create a narrated explainer with explainroo

Use explainroo to make a new explainer video with narration and visuals timed to the spoken words. It creates an MP4 on your computer and uses FFmpeg to assemble the video. The coding agent writes script.md for the narration and scenes.js for the drawings. explainroo then generates the voice, sets the timing, and renders the video.

Give a coding agent that can run shell commands this prompt, replacing the bracketed topic:

Make me a short explainer video about [your topic]. Use explainroo for it: read its AGENTS.md and follow the steps.

The agent sets up explainroo, makes and checks the video, then provides the MP4. The kit creates still frames and checks the layout to catch text that overlaps or gets cut off. It also checks the spoken words against the recording. explainroo supports several video layouts. Captions that light up word by word are included in tall, portrait, and square videos.

Free and open source

Let your AI agent make the video

explainroo lets a coding agent like Claude Code or Codex make narrated explainer videos and product demos. The voice, the word timing and the rendering run on your own computer, with no API key and no cost per video.

Copy this into your AI agent

Make me a short explainer video about [your topic]. Use explainroo for it: clone https://github.com/vincentsch/explainroo, read its AGENTS.md and follow the steps.

Getting started Example videos GitHub

Made with explainroo: How a cache makes a website faster

More on ffmpeg and rendering

All guides on ffmpeg and rendering