How to add text to a video with ffmpeg and python

与世无争的帅哥 提交于 2019-12-21 05:27:09

问题


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

回答1:


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



回答2:


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)


来源:https://stackoverflow.com/questions/8182725/how-to-add-text-to-a-video-with-ffmpeg-and-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!