我有代码:
import subprocess , os
ffmpeg = "C:\\ffmpeg_10_6_11.exe"
inVid = "C:\\test_in.avi"
outVid = "C:\\test_out.avi"
if os.path.exists( outVid ):
os.remove( outVid )
proc = subprocess.Popen(ffmpeg + " -i " + inVid + ''' -vf drawtext=fontfile=/Windows/Fonts/arial.ttf:text="onLine1 onLine2 onLine3":fontcolor=white:fontsize=20 -y ''' + outVid , shell=True, stderr=subprocess.PIPE)
proc.wait()
print proc.stderr.read()
os.startfile( outVid )
将文本写入视频文件。但我想写出很多行文本,而不是只将其全部写在一行上。
请帮忙。谢谢
这个答案对您来说可能有点晚了,但是您可以使用 [in] 标签在一个文件上指定多个绘图文本,并使用逗号列出每个绘图文本。如果您通过各自的定位方法来定位每个绘图文本,则这允许您使用多条线。在您的示例中,命令行看起来像这样(将第一行放在屏幕中间,并将后续的每一行向下 25 像素):
ffmpeg -i test_in.avi -vf "[in]drawtext=fontsize=20:fontcolor=White:fontfile='/Windows/Fonts/arial.ttf':text='onLine1':x=(w)/2:y=(h)/2, drawtext=fontsize=20:fontcolor=White:fontfile='/Windows/Fonts/arial.ttf':text='onLine2':x=(w)/2:y=((h)/2)+25, drawtext=fontsize=20:fontcolor=White:fontfile='/Windows/Fonts/arial.ttf':text='onLine3':x=(w)/2:y=((h)/2)+50[out]" -y test_out.avi
查看
ffmpeg
(vs_drawtext.c
)中的源代码:
static inline int is_newline(uint32_t c)
{
return c == '\n' || c == '\r' || c == '\f' || c == '\v';
}
因此您可以尝试在文本行中插入与
\f
或 \v
字符对应的 ^L
或 ^K
。例如:
-filter_complex "[in] drawtext=fontsize=40:fontcolor=white:fontfile=/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf:x=(w-tw)/2:y=(h-th)/2:box=1:[email protected]:text='two^Llines'[out]"
^L
是实际的 Ctrl-L 字符,而不是 ^
和 L
显然。
我简单地在命令中添加了新行,ffmpeg 正确处理了它。
ffmpeg -i input.avi -vf "[in]drawtext=fontsize=20:text='hello
world':x=(w)/2:y=(h)/2:fontcolor=white[out]" -y out.mp4
无需 Ctrl+L、Ctrl+K 破解!
即我只是在“你好”之后按了 Enter。
您可以编辑脚本文件甚至在 bash 命令行中进行操作。
我已经通过指定“textfile”参数并将我的文本放入此文件中,设法从命令行使其正常工作。
请参阅 http://ffmpeg.org/libavfilter.html#drawtext 获取更多帮助。在 Windows 上使用 ffmpeg 构建 N-35057-g2c44aed,但重要的是您拥有最新版本的 libavfilter。
TEXT=$(printf "$1")
在 shell 脚本中
将文本作为 shell 脚本参数,包括换行符
只需将文本分割成一定长度的行(20个字符,您可以根据需要设置),然后再传递给
ffmpeg
。
import subprocess , os
ffmpeg = "C:\\ffmpeg_10_6_11.exe"
inVid = "C:\\test_in.avi"
outVid = "C:\\test_out.avi"
length = 20
txt = "onLine1 onLine2 onLine3"
inpTxt = split_txt_into_multi_lines(txt,length)
if os.path.exists( outVid ):
os.remove( outVid )
proc = subprocess.Popen(ffmpeg + " -i " + inVid + f''' -vf drawtext=fontfile=/Windows/Fonts/arial.ttf:text={inpTxt}:fontcolor=white:fontsize=20 -y ''' + outVid , shell=True,
stderr=subprocess.PIPE)
proc.wait()
print proc.stderr.read()
os.startfile( outVid )
#################################################################
def split_txt_into_multi_lines(input_str: str, line_length: int):
words = input_str.split(" ")
line_count = 0
split_input = ""
for word in words:
line_count += 1
line_count += len(word)
if line_count > line_length:
split_input += "\n"
line_count = len(word) + 1
split_input += word
split_input += " "
else:
split_input += word
split_input += " "
return split_input
线程虽然很旧,但我也在寻找一个最简单的解决方案。根据答案之一中确定的来源,结果为 ascii 字符。
我测试过它,效果非常完美
-filter_complex "drawtext=text='StructuresinCLanguage':x=100:y=100:fon.........