1

I'm using drawtext to print some text on an animated GIF.

Everything is working, but I'm unable to specify a bounding box and make the text wrap.

I'm currently using this:

ffmpeg -i image.gif -filter_complex "drawtext=textfile=text.txt:x=main_w/2 - text_w/2:y=main_h/2 - text_h/2:fontfile=Roboto-Regular.ttf:fontsize=24:fontcolor=000000" image_out.gif

Is there a way to wrap text?

nkkollaw
  • 1,947
  • 1
  • 19
  • 29
  • I would use subtitles instead. Plenty of [examples](http://stackoverflow.com/a/21369850/1109017) on this site or see [documentation](https://ffmpeg.org/ffmpeg-filters.html#subtitles) and [FFmpeg wiki](https://trac.ffmpeg.org/wiki/HowToBurnSubtitlesIntoVideo). If you must use drawtext then just add the line breaks in your `text.txt`. – llogan Apr 30 '17 at 20:51
  • I see. Would that allow me to have text wrap, though? I don't see any advantage in using subtitles instead of text from quickly looking over the documentation. – nkkollaw Apr 30 '17 at 21:00
  • Yes, subtitles would automatically wrap the text. – llogan Apr 30 '17 at 21:51
  • I'll try it! Thanks. If you add it as an answer I'll accept it after I've tested it :-) – nkkollaw Apr 30 '17 at 22:17

1 Answers1

4

You can use the FFmpeg subtitles filter for automatic word wrapping and to place the text in the middle center.

There are many subtitle formats, but this answer has examples for ASS and SRT subtitles. ASS supports more formatting options, but SRT is a simpler format and can be modified with the force_style option in the subtitles filter.

enter image description here

ASS subtitles

Make your subtitles in Aegisub. Click the button that looks like a pink "S" to open the Styles Manager. Click edit. Choose Alignment value 5. Save subtitles file as ASS format.

ffmpeg example:

ffmpeg -i input -filter_complex subtitles=subs.srt -c:a copy output

SRT subtitles

SRT is simpler than ASS but lacks features so you may need to use the force_style option in the subtitles filter. You can make these subtitles with a text editor. Example SRT file:

1
00:00:00,000 --> 00:00:05,000
Text with
manual line break
and with automatic word wrapping of long lines.

2
00:00:07,000 --> 00:00:10,000
Another line. Displays during 7-10 seconds.

ffmpeg example:

ffmpeg -i input -filter_complex "subtitles=subs.srt:force_style='Alignment=10,Fontsize=24'" -c:a copy output
  • For more force_style options look at the Styles section in the contents of an ASS file and refer to ASS Tags.

  • In this case, Alignment is using the legacy line position numbers, but the ASS example above is using the modern "numpad" number system.

llogan
  • 121,796
  • 28
  • 232
  • 243
  • Thanks. I wish there was UTF-8 support in the ASS format, but it works. – nkkollaw May 02 '17 at 00:28
  • ...do you know if there's any way to specify the font directory, or even just the font the subs should be in? – nkkollaw May 02 '17 at 01:16
  • @nbrogi In `force_style` you can use something like `Fontname=Arial`, or in SRT use something like `Foo` (untested), or just choose the font in Aegisub. – llogan May 02 '17 at 02:21
  • Yes, I was looking for something like fontfile=/var/fonts/IMPACT.TTF – nkkollaw May 02 '17 at 09:44
  • how can i do same for the drawtext? it that possible for text using drawtext? – Milan Tejani Dec 25 '19 at 04:17
  • @llogan can we use `subtitles` filter for dynamic subtitles? For example my subtitles are not coming from the original source and I don't have a srt or ASS file, I am extracting the audio from the source and sending it to Microsoft Azure to recognize it on realtime and then I am adding it using drawtext filter which looks ugly and is not much flexible. – Muhammad Feb 01 '22 at 05:48