FFmpeg Drawtext Read Text from File Dynamically

Overlaying dynamic text onto videos is a crucial task for live streaming, automated video rendering, and displaying real-time data. This article explains how to use the FFmpeg drawtext filter to read text from an external file and update it dynamically on screen. You will learn the exact command-line syntax, key parameters like the reload option, and how to structure your files for seamless integration.

The Basic Command Syntax

To read text from an external file instead of hardcoding it into the FFmpeg command, you must use the textfile parameter inside the drawtext video filter.

Here is the fundamental command to achieve this:

ffmpeg -i input.mp4 -vf "drawtext=fontfile=/path/to/font.ttf:textfile='content.txt':reload=1:fontcolor=white:fontsize=24:x=(w-text_w)/2:y=h-50" -codec:a copy output.mp4

Parameter Breakdown

  • textfile='content.txt': This specifies the path to the plain text file containing the text you want to display. The file must be encoded in UTF-8.
  • reload=1: This is the key parameter for dynamic updates. When set to 1 (or enabled), FFmpeg will re-read the text file for every frame (or at regular intervals in player mode like ffplay), allowing you to update the contents of content.txt while the process is running.
  • fontfile: The path to a TrueType (.ttf) or OpenType (.otf) font. You must specify a valid font file for the text to render correctly.
  • fontcolor and fontsize: Controls the color and size of the rendered text.
  • x and y: Sets the horizontal and vertical coordinates of the text. In the example above, x=(w-text_w)/2 centers the text horizontally, and y=h-50 positions it 50 pixels from the bottom.

Practical Implementation Tips

Real-Time Updates with FFplay

If you want to test the dynamic updates in real-time on your screen before rendering a file, use ffplay:

ffplay -i input.mp4 -vf "drawtext=fontfile=arial.ttf:textfile='content.txt':reload=1:fontcolor=yellow:fontsize=32:x=10:y=10"

While the video is playing, open content.txt in a text editor, change the text, and save the file. The text overlay on the video screen will update instantly.

Automated Scripting

For live streams or dynamic dashboards, you can use an external Python, Bash, or Node.js script to write new data to content.txt (such as current time, song titles, or sensor data). FFmpeg will automatically grab the new content frame-by-frame as long as reload=1 is active.