Search code examples
textffmpegdrawtext

FFMpeg drawtext width?


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?


Solution

  • 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.