1

I'm trying to find a way to add a countdown text to videos using FFmpeg.

I want the text to be "Time left: mm:ss" but to avoid leading zeros for minutes.

So, for example:

  • Time left: 10:01
  • Time left: 10:00
  • Time left: 9:59
  • ...
  • Time left: 1:00
  • Time left: 00:59

I also want the text to disappear before showing 00:00

All videos are under 1 hour, so no need to address cases where duration >= 60m

6

1 Answer 1

1

I have tested these commands on Windows but on Linux it should be performed in a similar way:

First you have to get the duration in seconds of the video:

ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 input.mp4

Next you use the seconds obtained in the ffprobe command that I will specify as "$duration" (use also the numbers behind the dot ex: 3600.48993) here:

ffmpeg -f lavfi -i "color=color=#00000000@0:size=600x100:duration=$duration:r=30,format=rgba,settb=AVTB,drawtext=text='Time left\: %{pts\:gmtime\:0\:%#M\\\:%S}':fontsize=80:fontcolor=black:bordercolor=white:borderw=3:x=20:y=(H-th)/2:font=calibri" -i input.mp4 -filter_complex "[0]reverse[timer];[1][timer]overlay=x=0:y=H-h:enable='between(t,0,$duration-1)'" -y -c:v libx264 -c:a copy output.mp4

You may want to create a bash script or something to automate this...

The output is something like the link I posted before: output.gif

Explanation:

  • color= creates a timer with a transparent background
  • [0]reverse[timer] reverts the order of the timer going from end time to 0
  • enable='between(t,0,$duration-1)' removes the last second from the timer since you don't want 00:00...
  • {pts\:gmtime\:0\:%#M\\\:%S} creates a timer with Minutes %#M and Seconds %S, the # in %#M removes leading zeros but in Linux I guess it is %-M instead. The number of backslashes in %#M\\\:%S is probably different in Linux too. Try one, two, four and see what works for you.
2
  • I just noticed that the reverse also reverses the entire video stream, not only the timer Commented Mar 13 at 11:21
  • Well you have to apply it on the color only, before making the overlay.... Commented Mar 13 at 12:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .