How to add text to video using ffmpeg and python

I am trying to add text in avi with ffmpeg and I cannot figure out what is 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() 
+4
source share
2 answers

The colon ":" and the backslash "\" have special meaning when specifying options for drawtext. Therefore, you can avoid them by translating ":" to "\" and "\" to "\\". You can also enclose the path to your font file in single quotes if 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 
+6
source

HA

Turns off the double colon ":" in C: \ Windows \ Fonts, etc., acts as a split, so when I entered the full path of the font, ffmpeg read my command as follows

source team

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

ffmpeg 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 this, you need to strengthen: in the path of the font file.

My last 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) 
+5
source

Source: https://habr.com/ru/post/901340/


All Articles