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?
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:
n
is the current frame number5
is the number of frames per seconds (was fps=5
above)43
is the number of seconds to subtract (from the start time -ss 43
)2
means a two-digit output, so 00 instead of 0To 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:
%{eif\:(n/5-43)/60\:d\:2}
n
is the current frame number5
is the number of frames per seconds (was fps=5
above)43
is the number of seconds to subtract (from the start time -ss 43
)/60
calculates the minutes from the seconds, e.g. 83 / 60 = 12
format as a two-digit output, so 00 instead of 0\:
:
(has to be escaped)%{eif\:mod((n/5-43)\,60)\:d\:2}
mod
calculates the remainder (see below)n
is the current frame number5
is the number of frames per seconds (was fps=5
above)43
is the number of seconds to subtract (from the start time -ss 43
)\,60
calculates remainder (seconds) from the minutes, e.g. 83 mod 60 = 232
format as a two-digit output, so 00 instead of 0