scripts:ffmpeg:add_countdown_to_video

Add (burn in) countdown timer to video

I'm not sure who has a use for it, but I've used it to act as a countdown in a slideshow video.

To do this, first, you need to get the length of the video, then draw the text onto the video frame.

I recommend using this in a shell or bash script. First, find the duration as follows, where $f is the input video filename, and duration is the output duration variable.

duration=$(ffprobe -loglevel error -show_entries format=duration -of default=nw=1:nk=1 "$f");

Next, you can use ffmpeg to add the countdown text

ffmpeg -i "$f" -c:v libtheora -q:v 7 -vf "drawtext=fontfile=/usr/share/fonts/truetype/dejavu/DejaVuSansMono-Bold.ttf:text='%{eif\:$duration-t\:d}':fontcolor=yellow:fontsize=24:x=50:y=50:box=1:boxcolor=black@0.5:boxborderw=10,format=yuv444p" output.ogv

Here, you need to provide the path to the font you want to use (I recommend something monospaced so the timer stays still).

Then, the text parameter takes the duration variable from earlier, and subtracts the current time of the video in seconds. This results in a countdown timer of how many seconds are left. Font color is the colour, and fontsize is the size of text. The x and y parameters specify where on screen it should appear. You can use w or h to represent the width and height of the video, so 50,50 is top left, w-tw-50,50 is top right, so on. This also adds a box behind the text with a 10px border, with a 50% black opacity. Makes it easier to read, but you can mess with it or remove it entirely.

As always, you can daisy chain video filters with , if you want multiple text boxes, or resize and countdown, etc.

  • scripts/ffmpeg/add_countdown_to_video.txt
  • Last modified: 2022-04-06 21:43
  • by Tony