您可以使用 ffmpeg 流从文件实时插入文本吗?
Can you insert text from a file in real time with ffmpeg streaming?
我有这段代码用于流式传输文件并将视频在特定时间 (name_of_show) 的节目名称放在屏幕顶部,屏幕取自 video_title.txt 的节目名称并将其放在屏幕底部。
我想做的是每隔 1 分钟或 2 分钟找到一种方法,拉出当前 video_title.txt 中的任何文本并将其放入视频中几秒钟。
我会从另一个 python 程序更新 video_title.txt。下面是我当前的代码。
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,
最终我想要达到的效果是一长串 twitch/youtube 上的 1 小时,有人可以发送短信,它会出现在信息流中。
在 drawtext 中使用 reload=1
和 textfile
选项。来自 drawtext filter documentation:
reload
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.
textfile
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
要自动更新,您可以使用 mv
或等价物:
mv temp.txt mytext.txt
我有这段代码用于流式传输文件并将视频在特定时间 (name_of_show) 的节目名称放在屏幕顶部,屏幕取自 video_title.txt 的节目名称并将其放在屏幕底部。
我想做的是每隔 1 分钟或 2 分钟找到一种方法,拉出当前 video_title.txt 中的任何文本并将其放入视频中几秒钟。
我会从另一个 python 程序更新 video_title.txt。下面是我当前的代码。
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,
最终我想要达到的效果是一长串 twitch/youtube 上的 1 小时,有人可以发送短信,它会出现在信息流中。
在 drawtext 中使用 reload=1
和 textfile
选项。来自 drawtext filter documentation:
reload
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.
textfile
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 parametertext
.
If bothtext
andtextfile
are specified, an error occurs.
示例:
ffmpeg -i input.mp4 -vf "drawtext=texfile=mytext.txt:reload=1:fontsize=22:fontcolor=white" output.mp4
要自动更新,您可以使用 mv
或等价物:
mv temp.txt mytext.txt