ffmpegdrawtexttimecodes

Add a seconds counter text to a sliced video with ffmpeg that starts from 0


I'm trying to convert a small part of a screen recording video into a GIF with ffmpeg. I would like to add a seconds counter on it as the frame rate of the GIF file is reduced, so I can guess the time passing from the counter.

Normally, assuming to start at 43 seconds and cutting after 33 seconds, the example video would have been encoded to GIF with:

ffmpeg -i in.mp4 -vf "scale=300:-1,fps=5,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" -ss 43 -t 33 out.gif

To print the timecode, I add the following after fps=5,:

drawtext=text='%{pts\:hms}':font='Arial':fontcolor=red:fontsize=14:x=(w-text_w)/2:y=(h-text_h-6),

Doing so, prints a timecode on the output that starts from 00:00:43.000, not from zero. Similarly the timecode option:

drawtext=text='':font='Arial':fontcolor=red:fontsize=14:x=(w-text_w)/2:y=(h-text_h-6):timecode='00\:00\:00\:00':timecode_rate=5,

will ouput 00:00:43:0. There doesn't seem to have a way to use the "output" timecode (that starts from 0); also, I would like to have the seconds only.

How can I do that?


Solution

  • Formatting as seconds

    Apparently a way to do it is using the frame number, like so:

    drawtext=text='%{eif\:n/5-43\:d\:2}':font='Arial':fontcolor=red:fontsize=14:x=(w-text_w)/2:y=(h-text_h-6),
    

    that outputs 00, 01, etc. but the values have to be typed directly (hence not calculated by ffmpeg!). The %{eif\:n/5-43\:d\:2} part is composed like so:

    Formatting as mm:ss

    To display minutes and seconds, or if the duration of the video is greater than 60 seconds and you want to display e.g. 83 seconds like 01:23, use this syntax:

    drawtext=text='%{eif\:(n/5-43)/60\:d\:2}\:%{eif\:mod((n/5-43)\,60)\:d\:2}':font='Arial':fontcolor=red:fontsize=14:x=(w-text_w)/2:y=(h-text_h-6),
    

    Explanation: