7

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: boxcolor=black@0.3: \ 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.

Parsa Saei
  • 1,263
  • 5
  • 21
  • 38
  • 1
    `fontsize` doesn't support expressions yet, only integer values. Unless you want to patch it in, determine the required size and pass it to the `ffmpeg` command. – aergistal Oct 26 '16 at 13:09
  • The hack way to do this is to draw the text on a transparent BG and then use scale2ref to size the text and then overlay it. – Gyan Oct 26 '16 at 18:04
  • @Mulvya Can you give me a sample code or any example?how can draw text on a transparent BG? – Parsa Saei Oct 27 '16 at 06:16
  • @Mulvya It seems cannot work as your mention,because when you change the size of Background,text size not changes. And some characters of text fade out,because just BG box size decreased. – Parsa Saei Oct 30 '16 at 12:00
  • I'm busy with work now, so can supply a command later, but basic flow is 1)create transparent BG 2)draw text on BG 3)use scale2ref to resize BG w/ text. – Gyan Oct 30 '16 at 12:12

2 Answers2

7

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: boxcolor=black@0.5: 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.

andrybak
  • 2,129
  • 2
  • 20
  • 40
6

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()
Rabbit
  • 480
  • 5
  • 13