Can you insert text from a file in real time with ffmpeg streaming?

Tags: , ,

I have this code i use to stream a file and place the name of the show of the video at a certain time of the video ( name_of_show ) at the top of the screen, and the bottom of the screen it take the name of the show from video_title.txt and places it on the bottom of the screen.

What I want to do is find a way at a interval say 1 or 2 minutes, pull whatever text is currently in video_title.txt and place it in the video for a few seconds.

I would update video_title.txt from another python program. Here’s my current code below.

           command = [
        "ffmpeg" , "-re" , "-i" , video[0] ,
        "-vf" ,  "[in]drawtext=fontsize=40:fontcolor=white:box=1:boxcolor=black@0.8:boxborderw=5:fontfile=/home/fonts/timeless.ttf: text='" + name_of_show + "':x='W-(W+tw)*mod(t,10)/10':y='H/20':enable='between(t,50,70)',drawtext=fontsize=20:fontfile=/home/fonts/timeless.ttf:textfile=/video_title.txt:fontcolor=white:box=1:boxcolor=black@0.8:boxborderw=5:x=W-w+5:y=h-th-50:enable='1'[out]"
        "-vcodec" , "libx264", "-pix_fmt", "yuv420p",
        "-preset" , "medium" , "-r" , "30" , "-g" , "48" , "-b:v" , "2500k" ,
        "-acodec" , "libmp3lame" , "-ar" , "44100", "-threads" , "6" ,
        "-q:a" , "3" , "-b:a" , "712000" ,"-bufsize", "512k" , "-f" ,
        "flv" , STREAM_URL,

Ultimately the effect I am trying to achieve is a long stream of lets say 1 hour on twitch/youtube where someone could send a text message and it would appear in the stream.


Use the reload=1 and textfile options in drawtext. From the drawtext filter documentation:

If set to 1, the textfile will be reloaded before each frame. Be sure to update it atomically, or it may be read partially, or even fail.

A text file containing text to be drawn. The text must be a sequence of UTF-8 encoded characters.
This parameter is mandatory if no text string is specified with the parameter text.
If both text and textfile are specified, an error occurs.


ffmpeg -i input.mp4 -vf "drawtext=texfile=mytext.txt:reload=1:fontsize=22:fontcolor=white" output.mp4

To update atomically you can use mv or equivalent:

mv temp.txt mytext.txt

Source: stackoverflow