[FFmpeg-user] How add expression in drawtext's text
Jim DeLaHunt
from.ffmpeg-user at jdlh.com
Mon Jun 4 10:28:40 EEST 2018
On 2018-06-03 20:54, qw wrote:
> Hi,
>
>
> I can use the following command to add frame number on video:
>
>
> /usr/local/bin/ffmpeg -t 180 -i input.mp4 -acodec libfdk_aac -ac 2 -b:a 48k -vcodec libx264 -b:v 500k -g 25 \
> -vf "fps=fps=25, scale=w=320:h=240, drawtext='box=1:fontcolor=red:fontsize=20:fontfile=OpenSans-Regular.ttf:text=%{frame_num}'" \
> -f mp4 out.mp4
>
>
> But the width of the text box will increase as frame number increases. I want to make the width of the text box unchanged. If the largest frame number is 99999, I want to make the width of the text is of 5 numbers. How to do it?
As Gyan pointed out, the %{eif...} expression will give you padding of
zeroes on the left. Specifically, here is your example modified to use
this feature:
/usr/local/bin/ffmpeg -t 180 -i input.mp4 -acodec libfdk_aac -ac 2 -b:a 48k -vcodec libx264 -b:v 500k -g 25 \
-vf "fps=fps=25, scale=w=320:h=240, drawtext='box=1:fontcolor=red:fontsize=20:fontfile=OpenSans-Regular.ttf:text=%{eif\:frame_num\:d\:5}'" \
-f mp4 out.mp4
I don't see a direct way to have the drawtext filter provide blank space
padding to the left of your expression. But, you can draw the background
box with a drawbox filter, and then draw the text without a box using
the drawtext filter. You will have to adjust some numbers by trial and
error to make the result look right.
Here is your example modified to give the appearance of a frame number
left-padded with blanks:
/usr/local/bin/ffmpeg -t 180 -i input.mp4 -acodec libfdk_aac -ac 2 -b:a 48k -vcodec libx264 -b:v 500k -g 25 \
-vf "fps=fps=25, scale=w=320:h=240, \
drawbox=x=0:y=0:width=60:height=20*0.65:thickness=fill:color=white, \
drawtext='x=0+60-(20*0.6)*if(gt(n,9999),5,if(gt(n,999),4,if(gt(n,99),3,if(gt(n,9),2,1)))):'\
'y=0:box=0:fontcolor=red:fontsize=20:fontfile=OpenSans-Regular.ttf:text=%{frame_num}'" \
-f mp4 out.mp4
Here is an explanation of the special numbers I used. They all depend on
the proportions of the glyphs drawn by the font which you choose.
Drawbox's "height" of 20*0.65 is the fontsize you use in drawtext,
multiplied by a number based on the size of box which drawtext would
have drawn. Increase it a little bit to give a margin for error. 0.65
worked for the font I tested with.
Drawbox's "x" and "y" values are set to the upper-left corner of the box
on which the text is drawn.
Drawtext's "x" value can be thought of as "leftside location"+"width of
box" - "width of one digit" * "number of digits".
The "leftside location" is the same number as drawbox's "x" value.
The "width of box" value is drawbox's "width" value.
The "width of one digit expression", here "(20*0.6)", is drawbox's
fontsize value times a constant based on the width of the font's
digits. 0.6 worked for the font I tested with.
The "number of digits" expressions is a chain of "if n>9999 then 4 else
if ..." expressions. It returns 5, 4, 3, 2, or 1, depending on the value
of "n". Note that "n" and "frame_num" are different names for the same
thing, but in an expression for "x", you can only use the name "n".
Drawtext's "y" value is set to the same number as drawbox's "y" value.
Now I tested these examples with my video and my font. They may not work
exactly the same in your system. You may have to adjust the numbers. You
may also have to fix any silly mistakes I made. But I hope this helps
you solve your problem.
--
--Jim DeLaHunt, jdlh at jdlh.com http://blog.jdlh.com/ (http://jdlh.com/)
multilingual websites consultant
355-1027 Davie St, Vancouver BC V6E 4L2, Canada
Canada mobile +1-604-376-8953
More information about the ffmpeg-user
mailing list