FFmpeg drawtext filter to Insert Dynamic Overlays, Scrolling Text, and Timestamps

Learn FFmpeg’s drawtext filter to dynamically overlay text on video and display information such as timecode, frame resolution, watermarks, etc. Also, let’s learn how to configure the font, font-size, position, background-color, alignment, multiple lines, etc. using FFmpeg’s drawtext filter.

Step 0: Ensure your FFmpeg Is Compiled with libfreetype

In order to use drawtext, you needed to have configured FFmpeg with --enable-libfreetype. As per the documentation, you need the following options as well if you want to,

  • enable default font fallback and the font option you need to configure FFmpeg with --enable-libfontconfig.
  • enable the text_shaping option, you need to configure FFmpeg with --enable-libfribidi.

Complete List of Options for drawtext

The complete list of options for drawtext filter can be accessed here. It is far too much for me to explain here, but, if you have any questions, that is the first place you should refer.

In this article, I’ll walk through several common use-cases that should make the concepts easy to understand.

Display Text on the Video using drawtext filter

Here is the commandline and an explanation of the options

ffmpeg -i inputClip.mp4 -vf "drawtext=text='My text starting at 640x360':x=640:y=360:fontsize=24:fontcolor=white" -c:a copy output.mp4

Here,

  • inputClip.mp4 is the input video on which you want to display the text; and the output (containing the text) is to be stored in output.mp4
  • no audio re-encoding as indicated by -c:a copy
  • we use the drawtext filter as indicated by the commands -vf "drawtext=........"
  • text='My text starting at 640x360' is the text that will be shown on the video (you could make it your name for watermarking the video, right?)
  • position of the text
    • x=640:y=360 indicates that the x and y coordinates as 640px and 360px. Also, as a side note, the video’s resolution is 1280x720.
    • font size is 24
    • font color is white

Let’s see how the output looks, shall we?

ffmpeg-drawtext-filter-dynamic-overlays-timecode-scrolling-text-credits

A better way to this is to offset the text by the length of the text that you are printing on the screen.

Confused?

If you look at the image above, you’ll see that it starts at the center of the video and extends towards the right.

If you want to center the text itself, then you can subtract the height and width of the rendered text when telling drawtext where to render the text.

Here’s how. You use the command x=(w-text_w)/2:y=(h-text_h)/2 and it will center the text. Here is our new commandline –

ffmpeg -i inputClip.mp4 -vf "drawtext=text='Centered Text':x=(w-text_w)/2:y=(h-text_h)/2:fontsize=24:fontcolor=white" -c:a copy output.mp4

Now, the text looks nice and pretty 🙂

ffmpeg-drawtext-filter-dynamic-overlays-timecode-scrolling-text-credits

Fantastic – you now know how to overlay text onto a video using FFmpeg’s drawtext filter. Do you think you can add your own watermark or copyright? Let’s try 🙂

Let’s modify the command as follows to indicate my name and the copyright symbol.

ffmpeg -i inputClip.mp4 -vf "drawtext=text='© Krishna':x=640:y=360:fontsize=24:fontcolor=white" -c:a copy output.mp4

This produces an output like this – looks good right? You can play around with the x and y coordinates to align the text the way you want to.

ffmpeg-drawtext-filter-dynamic-overlays-timecode-scrolling-text-credits

Adding Text with Background Color using FFmpeg’s drawtext filter

To add a background color, we need

ffmpeg -i inputClip.mp4 -vf "drawtext=text='© Krishna':x=(1100-text_w):y=(600-text_h):fontsize=32:fontcolor=black:box=1:[email protected]: boxborderw=5" -c:a copy output.mp4

The new commands here are –

  • box : this is either 1 (enabled) or 0 (disabled)
  • boxcolor: [email protected] implies a white colored box with a 50% opacity.
  • boxborderw is the width of the box’s border and the border color is taken from boxcolor.

And there you have it, text with a background. In this example, I switched the color of the text to black so that it contrasts well with a light background bounding box (which in-turn contrasts well with a dark background.)

ffmpeg-drawtext-filter-dynamic-overlays-timecode-scrolling-text-credits

Displaying TimeCodes / TimeStamps using FFmpeg’s drawtext filter

This is a very useful application of the drawtext filter and is used in demonstrating low-latency applications or visual quality testing so that one knows precisely what the timestamps/timecodes are at each time.

ffmpeg -i inputClip.mp4 -vf "drawtext=text='timestamp: %{pts \: hms}': x=500: y=500: fontsize=32:[email protected]: box=1: [email protected]" -c:a copy output.mp4

This uses the timestamp and pts options to display time in hour:min:sec format using the hms format specifier. The notations and formatting are complex in my opion! So, a lot of trial and error might be needed before you format your display correctly.

Here is how the video looks. Hope Vimeo shows you the video without a lot of delay 🙂

And here is the same command, but using the flt option to provide microsecond time accuracy! Fancy 🙂

ffmpeg -i inputClip.mp4 -vf "drawtext=text='timestamp: %{pts \: flt}': x=500: y=500: fontsize=32:[email protected]: box=1: [email protected]" -c:a copy output.mp4
ffmpeg-drawtext-filter-dynamic-overlays-timecode-scrolling-text-credits

Display Movie Credits using FFmpeg’s drawtext filter

Finally, let’s learn how to show a movie’s credits using the draw text filter. Here are two main concepts that you need to understand.

  • providing a lot of text: you can’t do this via the commandline, so, you need to read a text file that contains the text. And you can read that using the textfile option.
  • and, specify the speed of scrolling using the y text position. Here, you can provide an equation instead of a constant number. You start off by telling by FFmpeg that the y-position is h - 80*t so that everytime the value of time increases, the value of h - 80*t* decreases, and the text is displayed higher. Makes sense?

Tip: change 80 to 100 or 120 and see the effect on the scrolling speed.

ffmpeg -i inputClip.mp4 -vf "drawtext=textfile=credits.txt: x=200: y=h-80*t: fontsize=36:[email protected]: box=1: [email protected]" -c:a copy outputCredits.mp4

Here is the output:

That’s it for this tutorial on using FFmpeg’s drawtext filter to produce dynamic overlays on your videos. It is a very versatile and handy tool that you can use to overlay text, timecodes, credits, copyrights notices on your videos.

If you are interested in video compression, check out our comparison of LCEVC (MPEG5 Part 2) vs H.264/AVC. Stunning results, I tell you 🙂

lcevc avc ffmeg
krishna rao vijayanagar
Krishna Rao Vijayanagar
Founder at OTTVerse

Krishna Rao Vijayanagar, Ph.D., is the Editor-in-Chief of OTTVerse, a news portal covering tech and business news in the OTT industry.

With extensive experience in video encoding, streaming, analytics, monetization, end-to-end streaming, and more, Krishna has held multiple leadership roles in R&D, Engineering, and Product at companies such as Harmonic Inc., MediaMelon, and Airtel Digital. Krishna has published numerous articles and research papers and speaks at industry events to share his insights and perspectives on the fundamentals and the future of OTT streaming.

Pallycon April NAB 2024

11 thoughts on “FFmpeg drawtext filter to Insert Dynamic Overlays, Scrolling Text, and Timestamps”

  1. Pingback: Reverse a Video with FFmpeg is Ridiculously Easy - OTTVerse

  2. Pingback: Reversing Video and Audio Using FFmpeg – A2M1N

  3. Pingback: FFmpeg Drawtext Filter for Overlays, Scrolling Text, Timestamps on Videos - Your Cheer

  4. Pingback: FFmpeg Drawtext Filter for Overlays, Scrolling Text, Timestamps on Videos – Hacker News Robot

  5. Pingback: === popurls.com === popular today

  6. I tried a lot of stuff (even the official docs of ffmpeg), but somehow nothing seems to work. Your commands did. Thank you! 🕺

  7. sagar bhujbal

    unicode text working but some compound words showing as split like श्री showing शरी, please suggest solution for this.

  8. Pingback: What is FFmpeg? Usage, Benefits, and Installation Simplified - USA Domain Hosting

  9. Pingback: Otras recetas • Other recipes – ARTELEKU-NOGUI

Leave a Comment

Your email address will not be published. Required fields are marked *

Enjoying this article? Subscribe to OTTVerse and receive exclusive news and information from the OTT Industry.