如何使用 ffmpeg 和 python 向视频添加文本

发布于 2024-12-16 18:31:39 字数 378 浏览 1 评论 0原文

我一直在尝试使用 ffmpeg 将文本添加到 avi,但似乎无法正确执行。

请帮忙:

import subprocess

ffmpeg = "C:\\ffmpeg_10_6_11.exe"
inVid = "C:\\test_in.avi"
outVid = "C:\\test_out.avi"

proc = subprocess.Popen(ffmpeg + " -i " + inVid + " -vf drawtext=fontfile='arial.ttf'|text='test' -y " + outVid , shell=True, stderr=subprocess.PIPE)
proc.wait()
print proc.stderr.read()

I've been trying to add text to an avi with ffmpeg and I can't seem to get it right.

Please help:

import subprocess

ffmpeg = "C:\\ffmpeg_10_6_11.exe"
inVid = "C:\\test_in.avi"
outVid = "C:\\test_out.avi"

proc = subprocess.Popen(ffmpeg + " -i " + inVid + " -vf drawtext=fontfile='arial.ttf'|text='test' -y " + outVid , shell=True, stderr=subprocess.PIPE)
proc.wait()
print proc.stderr.read()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜`诱少女 2024-12-23 18:31:39

为绘图文本指定参数时,冒号“:”和反斜杠“\”具有特殊含义。所以你可以做的就是通过将“:”转换为“\:”和“\”转换为“\\”来转义它们。
您还可以将字体文件的路径用单引号括起来,以防路径包含空格。

所以你会有

ffmpeg -i C:\Test\rec\vid_1321909320.avi -vf drawtext=fontfile='C\:\\Windows\\Fonts\\arial.ttf':text=test vid_1321909320.flv

A colon ":" and a backslash "\" have special meaning when specifying the parameters for drawtext. So what you can do is to escape them by converting ":" to "\:" and "\" to "\\".
Also you can enclose the path to your font file in single quotes incase the path contains spaces.

So you will have

ffmpeg -i C:\Test\rec\vid_1321909320.avi -vf drawtext=fontfile='C\:\\Windows\\Fonts\\arial.ttf':text=test vid_1321909320.flv
明媚殇 2024-12-23 18:31:39

HA

原来 C:\Windows\Fonts 等中的双冒号“:”充当了分割,因此当我输入字体的完整路径时 ffmpeg 正在读取我的命令,如下所示

原始命令

" -vf drawtext=fontfile='C:\\Windows\\fonts\\arial.ttf'|text='test' "

ffmpeg 的解释

-vf drawtext=  # command

fontfile='C    # C is the font file because the : comes after it signalling the next key

arial.ttf'     # is the next key after fontfile = C (because the C is followed by a : signalling the next key)

:text          # is the value the key "arial.tff" is pointing to

='test'        # is some arb piece of information put in by that silly user

所以要修复它,您需要消除字体文件路径中的 : 。

我的最终工作代码:

import subprocess

ffmpeg = "C:\\ffmpeg_10_6_11.exe"
inVid = "C:\\test_in.avi"
outVid = "C:\\test_out.avi"

subprocess.Popen(ffmpeg + " -i " + inVid + ''' -vf drawtext=fontfile=/Windows/Fonts/arial.ttf:text=test ''' + outVid , shell=True)

HA

Turns out the double colon ":" in C:\Windows\Fonts etc was acting as a split so when i was inputting the font's full path ffmpeg was reading my command as follows

original command

" -vf drawtext=fontfile='C:\\Windows\\fonts\\arial.ttf'|text='test' "

ffmpeg's interpretation

-vf drawtext=  # command

fontfile='C    # C is the font file because the : comes after it signalling the next key

arial.ttf'     # is the next key after fontfile = C (because the C is followed by a : signalling the next key)

:text          # is the value the key "arial.tff" is pointing to

='test'        # is some arb piece of information put in by that silly user

So to fix it you need to elinate the : in the font file path.

My final working code:

import subprocess

ffmpeg = "C:\\ffmpeg_10_6_11.exe"
inVid = "C:\\test_in.avi"
outVid = "C:\\test_out.avi"

subprocess.Popen(ffmpeg + " -i " + inVid + ''' -vf drawtext=fontfile=/Windows/Fonts/arial.ttf:text=test ''' + outVid , shell=True)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文