Update FFmpeg Drawtext Text Dynamically

Updating text overlays during an active FFmpeg stream or recording can be achieved without restarting the encoding process. You can control the drawtext filter dynamically either by utilizing the file-reload mechanism, injecting commands via the sendcmd filter, or transmitting runtime commands over an IPC socket using the zmq (ZeroMQ) filter.

Method 1: The File Reload Method (Simplest Approach)

The easiest way to update text dynamically is by reading the string from an external text file and configuring the drawtext filter to reload the file for every frame.

Run the following command:

ffmpeg -i input.mp4 -vf "drawtext=textfile=dynamic.txt:reload=1:fontfile=arial.ttf:fontsize=24:fontcolor=white:x=50:y=50" -c:a copy output.mp4
  • textfile=dynamic.txt: Specifies the path to the text file containing your overlay string.
  • reload=1: Instructs FFmpeg to re-read dynamic.txt on every frame.

Whenever you write new content to dynamic.txt on your filesystem, FFmpeg immediately updates the rendered overlay in the output stream.

Method 2: Real-Time Commands Using the ZeroMQ (zmq) Filter

If you need to push text updates programmatically over a network or IPC socket, compile FFmpeg with --enable-libzmq and prepend the zmq filter to your filtergraph.

1. Start FFmpeg with ZMQ and Drawtext

ffmpeg -i input.mp4 -vf "zmq='bind_address=tcp\\://127.0.0.1\\:5555',drawtext=fontfile=arial.ttf:text='Initial Text':fontsize=24:fontcolor=white:x=50:y=50" output.mp4

2. Send the Update Command

You can send commands using the zmqsend utility provided with FFmpeg, or using a simple script in languages like Python.

To update the text using zmqsend:

echo "Parsed_drawtext_1 reinit text='New Dynamic Text'" | zmqsend -b "tcp://127.0.0.1:5555"
  • Parsed_drawtext_1: The default identifier FFmpeg assigns to the first drawtext filter in the chain. You can assign a custom name to the filter using the drawtext@my_text=... syntax, allowing you to target it with my_text reinit text='...'.
  • reinit: The command sent to the filter to reinitialize its parameters with the new text value.

Method 3: Timed Changes Using the sendcmd Filter

If the text changes are predetermined according to timestamps, the sendcmd filter can execute dynamic text updates sequentially.

1. Create a Command File (commands.txt)

2.0 drawtext reinit text 'Text at 2 Seconds';
5.5 drawtext reinit text 'Text at 5.5 Seconds';
10.0 drawtext reinit text 'Text at 10 Seconds';

2. Execute FFmpeg with sendcmd

ffmpeg -i input.mp4 -vf "sendcmd=f=commands.txt,drawtext=fontfile=arial.ttf:text='Starting Text':fontsize=24:fontcolor=white:x=50:y=50" output.mp4

At each specified timestamp, sendcmd triggers the reinit directive on the drawtext filter, updating the displayed string automatically.