简体   繁体   English

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

[英]How to add text to a video with ffmpeg and python

I've been trying to add text to an avi with ffmpeg and I can't seem to get it right. 我一直在尝试使用ffmpeg向avi添加文本,我似乎无法做到正确。

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()

A colon ":" and a backslash "\\" have special meaning when specifying the parameters for drawtext. 在为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

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 原来在C:\\ Windows \\ Fonts等中的双冒号“:”作为一个分裂,所以当我输入字体的完整路径时,ffmpeg正在读取我的命令,如下所示

original command 原始命令

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

ffmpeg's interpretation 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

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM