FFmpeg Drawtext Load Text From File Dynamically
Overlaying dynamic text onto videos is a common requirement for live
streams, automated video generation, and real-time monitoring displays.
This guide explains how to use the FFmpeg drawtext video
filter to load text from an external file and automatically update the
video overlay in real-time as the file’s content changes.
The Core FFmpeg Command
To load text from an external file dynamically, you must use the
textfile parameter to specify the file path, and set the
reload parameter to 1.
Here is the basic command structure:
ffmpeg -i input.mp4 -vf "drawtext=fontfile=Arial.ttf:textfile=status.txt:reload=1:fontcolor=white:fontsize=24:x=50:y=50" -codec:a copy output.mp4Parameter Breakdown
drawtext: The video filter used for drawing text over the video.fontfile: The path to the TrueType (.ttf) or OpenType (.otf) font file. FFmpeg requires a valid font to render text.textfile: The path to the plain text file (e.g.,status.txt) containing the text you want to display.reload=1: The key parameter that tells FFmpeg to check and reload the text file before rendering each frame. Without this, FFmpeg will only read the file once at the beginning of the process.fontcolorandfontsize: Controls the visual appearance of the rendered text.xandy: The coordinate position of the text on the video frame.
Real-Time Updates During Live Streams
If you are running a live stream or generating an infinite loop video, you can dynamically update the contents of your text file from an external script (such as Python, Bash, or Node.js).
For example, you can write a script that updates
status.txt with the current time, weather data, or system
performance metrics:
echo "Current CPU Temp: 45C" > status.txtFFmpeg will detect the change on the next frame refresh and display the updated string without interrupting the video stream.
Handling File Access Conflicts
When writing to the text file while FFmpeg is reading it, you might occasionally get a blank frame if FFmpeg reads the file mid-write. To prevent this, write the new content to a temporary file first, and then perform an atomic replace (rename) operation:
echo "New Dynamic Text" > status.tmp && mv status.tmp status.txt