Question

I have the code:

import subprocess , os

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

if os.path.exists( outVid ):
os.remove( outVid )
proc = subprocess.Popen(ffmpeg + " -i " + inVid + ''' -vf drawtext=fontfile=/Windows/Fonts/arial.ttf:text="onLine1 onLine2 onLine3":fontcolor=white:fontsize=20 -y ''' + outVid , shell=True, stderr=subprocess.PIPE)
proc.wait()
print proc.stderr.read()
os.startfile( outVid )

to write text to a video file. But I want to write out many lines of text instead of just having it all on the one line.

Pls help. Thanks

Was it helpful?

Solution

This answer is probably a bit late for you, but you can specify multiple drawtexts on one file by using the [in] tag and listing each drawtext using commas. This allows you to use multiple lines if you orient each drawtext through their respective positioning methods. In your example, the command line would look something like this (puts the first line in the middle of the screen, and puts each subsequent line 25 pixels down):

ffmpeg -i test_in.avi -vf "[in]drawtext=fontsize=20:fontcolor=White:fontfile='/Windows/Fonts/arial.ttf':text='onLine1':x=(w)/2:y=(h)/2, drawtext=fontsize=20:fontcolor=White:fontfile='/Windows/Fonts/arial.ttf':text='onLine2':x=(w)/2:y=((h)/2)+25, drawtext=fontsize=20:fontcolor=White:fontfile='/Windows/Fonts/arial.ttf':text='onLine3':x=(w)/2:y=((h)/2)+50[out]" -y test_out.avi

OTHER TIPS

Looking at the source code in ffmpeg (vs_drawtext.c):

static inline int is_newline(uint32_t c)
{
    return c == '\n' || c == '\r' || c == '\f' || c == '\v';
}

so you can try inserting \f or \v in your text line which correspond to ^L or ^K characters. For example:

-filter_complex "[in] drawtext=fontsize=40:fontcolor=white:fontfile=/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf:x=(w-tw)/2:y=(h-th)/2:box=1:boxcolor=black@0.5:text='two^Llines'[out]"

^L being the actual Ctrl-L character and not ^ and L obviously.

I simple added new lines inside command and ffmpeg handled it properly.

ffmpeg -i input.avi -vf "[in]drawtext=fontsize=20:text='hello
world':x=(w)/2:y=(h)/2:fontcolor=white[out]" -y out.mp4

No Ctrl+L, Ctrl+K hacks are needed!

I.e. I just pressed Enter after 'hello'.

You can do it editing script file or even in bash command line.

I have managed to get this to work from the command line by specifying the 'textfile' parameter and putting my text into this file.

See http://ffmpeg.org/libavfilter.html#drawtext for more help. Using ffmpeg build N-35057-g2c44aed on windows, but the important thing is that you have recent version of the libavfilter.

TEXT=$(printf "$1")

In a shell script

With your text as your shell script argument including new line characters

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top