您可以使用ffmpeg流实时插入文件中的文本吗? [英] Can you insert text from a file in real time with ffmpeg streaming?
问题描述
我使用此代码来流式传输文件,并将视频节目的名称放在视频的特定时间(name_of_show)放在屏幕顶部,在屏幕底部以该名称命名video_title.txt中的节目片段,并将其放置在屏幕底部.
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.
我想做的是找到一种间隔时间,例如1或2分钟,拉出video_title.txt中当前存在的任何文本,然后将其放置在视频中几秒钟.
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.
我将从另一个python程序更新video_title.txt.这是下面的当前代码.
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,
最终,我要实现的效果是在twitch/youtube上漫长的通话时间,比如说1个小时,有人可以发送短信,该短信会出现在信息流中.
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.
推荐答案
在drawtext中使用 reload = 1
和 textfile
选项.从 drawtext过滤器文档:
Use the reload=1
and textfile
options in drawtext. From the drawtext filter documentation:
重新加载
如果设置为1,则文本文件将在每个文件之前重新加载框架.请确保以原子方式进行更新,否则可能会被部分读取,甚至失败.
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.
文本文件
包含要绘制的文本的文本文件.文本必须是UTF-8编码字符的序列.
如果未使用参数 text
指定文本字符串,则此参数是必需的.
如果同时指定了 text
和 textfile
,则会发生错误.
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
或等效版本:
To update atomically you can use mv
or equivalent:
mv temp.txt mytext.txt
这篇关于您可以使用ffmpeg流实时插入文件中的文本吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!