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

Jar*_*red 4 python video text ffmpeg drawtext

我一直在尝试使用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()
Run Code Online (Sandbox Code Playgroud)

ade*_*tum 6

在为drawtext指定参数时,冒号":"和反斜杠"\"具有特殊含义.所以你可以做的是通过将":"转换为"\:"并将"\"转换为"\\"来逃避它们.您也可以用单引号将字体文件的路径括起来,路径中包含空格.

所以你会有

ffmpeg -i C:\Test\rec\vid_1321909320.avi -vf drawtext=fontfile='C\:\\Windows\\Fonts\\arial.ttf':text=test vid_1321909320.flv
Run Code Online (Sandbox Code Playgroud)


Jar*_*red 5

原来,双冒号":"在C:\ WINDOWS \字体等被作为分割所以当我输入字体的完整路径的ffmpeg正在读我的命令,如下所示

原始命令

" -vf drawtext=fontfile='C:\\Windows\\fonts\\arial.ttf'|text='test' "
Run Code Online (Sandbox Code Playgroud)

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

所以要修复它,你需要在字体文件路径中删除:

我最后的工作代码:

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