FFmpeg drawtext多行

Jar*_*red 4 ffmpeg line multiline drawtext

我有代码:

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 )
Run Code Online (Sandbox Code Playgroud)

将文本写入视频文件.但是我想写出许多行文本,而不仅仅是在一行上.

请帮忙.谢谢

Ben*_*Ben 18

这个答案对您来说可能有点迟了,但您可以使用[in]标记在一个文件上指定多个drawtexts,并使用逗号列出每个drawtext.如果您通过各自的定位方法定位每个drawtext,则允许您使用多行.在您的示例中,命令行看起来像这样(将第一行放在屏幕中间,并将每个后续行放置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
Run Code Online (Sandbox Code Playgroud)


小智 14

查看ffmpeg(vs_drawtext.c)中的源代码:

static inline int is_newline(uint32_t c)
{
    return c == '\n' || c == '\r' || c == '\f' || c == '\v';
}
Run Code Online (Sandbox Code Playgroud)

所以你可以尝试插入\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:boxcolor=black@0.5:text='two^Llines'[out]"
Run Code Online (Sandbox Code Playgroud)

^L作为实际按Ctrl-L字符,而不是^L明显.

  • +1用于引用源代码.欢迎来到Stack Overflow! (4认同)
  • 要在大多数终端中输入“^L”,您可以先执行“Ctrl-V”,然后按“Ctrl-L”。 (2认同)

小智 12

我简单地在命令中添加了新行,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
Run Code Online (Sandbox Code Playgroud)

不需要 Ctrl+L、Ctrl+K 技巧!

即我只是在“你好”之后按下 Enter 键。

您可以编辑脚本文件甚至在 bash 命令行中执行此操作。


小智 5

我已经设法通过指定'textfile'参数并将我的文本放入此文件,从命令行开始工作.

有关更多帮助,请参见http://ffmpeg.org/libavfilter.html#drawtext.在Windows上使用ffmpeg build N-35057-g2c44aed,但重要的是你有最新版本的libavfilter.