Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set drawtext fontsize in ffmpeg according to resolutions ratio

How can I set fontsize for drawtext on video according to width and height of each resolutions, to a certain proportion?

ffmpeg -i Input.mp4 -vf drawtext="fontfile=OpenSans-Regular.ttf: \
text='New Music Video': fontcolor=white: fontsize=?: r=25: box=1: [email protected]: \ boxborderw=3: x=(20)/2: y=(h-text_h-20) " Output.mp4

I ask this question because when I set it to 24 fontsize=24 for example,it size is defferent at other resolutions,when convert is done.

like image 926
parsa Avatar asked Oct 26 '16 12:10

parsa


2 Answers

Since ffmpeg version 3.4, video filter drawtext supports arithmetic expressions in its fontsize parameter. For example, height divided by 30:

-vf drawtext="fontsize=(h/30): x=(w-text_w)/2: y=(h-text_h*2): text='Hello, World!': fontcolor=white: box=1: [email protected]: boxborderw=5"

This will use font size 24 on a 720p video and 36 on a 1080p video.

See also more examples on how to use filter drawtext in ffmpeg documentation.

like image 40
andrybak Avatar answered Oct 24 '22 13:10

andrybak


I have been faced with this question too,here is my solution: use various resolution's diagonal's rate , scale your textsize.

Here is details:

  1. Find a suitable textsize in your most used resolution. e.g. textsize=65 in resolution=1920*1080
  2. Use target video's resolution's diagonal divide by 1920*1080's diagonal,then multiply by your suitable textsize (65),result is what you want

kotlin code here:

(Math.sqrt((videoWidth*videoWidth + videoHeight*videoHeight).toDouble()) / 2203 * textsize).toInt()
like image 153
Rabbit Avatar answered Oct 24 '22 15:10

Rabbit