Search code examples
filterffmpegdrawtext

ffmpeg drawtext - only draw within clipping rectangle


The ffmpeg documentation on filters doesn't have anything on a clipping rectangle for drawtext. I'm hoping there might be a setting I can put before a video filter to set a clip rect, an undocumented feature or a clever workaround so that you can draw the text only within a certain bounding box on the output video.

I'm trying to overlay movie titles over a ticking clock video. These might be long and I don't want them to spill over onto part of the clock. I know how to overlay text already. I am currently working around this by using a shorter version of the title.

Is this something that requires filter graphs or intermediate video files or can it be done with a simple video filter?


Solution

  • enter image description here

    Method 1: subtitles filter

    Assuming you want simple clipping, and not bounded word wrapping, you can use ASS subtitles. See Visual Typesetting - Aegisub.

    Once you make your subtitles file the ffmpeg command is simple:

    ffmpeg -i input -filter_complex "subtitles=subs.ass" output
    

    If you want to avoid using Aegisub you can manually make simple SRT style subs and then use the force_style option to set the clipping co-ordinates. See the subtitles filter documentation.

    Method 2: crop

    Another method would be to use crop, drawtext, and overlay filters. You can adapt the example in Blur specific area (without a mask).