17

我有代码:

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 )

将文本写入视频文件。但我想写出多行文本,而不是全部写在一行上。

请帮忙。谢谢

4

5 回答 5

30

这个答案对您来说可能有点晚了,但是您可以使用 [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
于 2012-06-21T14:07:26.733 回答
23

查看ffmpegvs_drawtext.c)中的源代码:

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

因此您可以尝试在与or字符对应的文本行中插入\for 。例如:\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]"

^L是实际的 Ctrl-L 字符^L显然不是。

于 2012-10-11T09:39:08.917 回答
14

我简单地在命令中添加了新行,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 命令行中进行。

于 2015-11-22T17:26:20.907 回答
6

通过指定'textfile'参数并将我的文本放入此文件中,我设法从命令行使其工作。

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

于 2011-11-28T03:20:42.560 回答
0

文本=$(printf "$1")

在 shell 脚本中

将您的文本作为您的 shell 脚本参数,包括换行符

于 2020-03-11T22:01:33.927 回答