Search code examples
ffmpegtimestampdrawtextffplay

When using ffplay, how to eliminate milliseconds from timestamp display in the video-filter


So when specifying a video-filter to display current video-time in 'hms' layout, the filter appends a millisecond value after the seconds-field. I want to eliminate the milliseconds portion.

So far, my invoked cmd looks like:

ffplay myfile.mp4 -vf "drawtext=text='%{pts \: hms}':fontsize=20:fontcolor=white: box=1: boxcolor=black"

i.e. instead of: 00:00:01.001 I want just 00:00:01 to display.

[One might have thought that there would exist an 'hmsm' for the former, and that 'hms' would be the latter, but it just doesn't work that way.] (sigh)


Solution

  • According to the documentation:

    hms stands for a formatted [-]HH:MM:SS.mmm timestamp with millisecond accuracy.

    And there's no way to customize this timestamp format. At least not for the hms format.
    The same documentation:

    If the format is set to localtime or gmtime, a third argument may be supplied: a strftime() format string. By default, YYYY-MM-DD HH:MM:SS format will be used.

    So text='%{pts\:gmtime}' without any additional arguments puts out 1970-01-01 00:00:00 (Unix epoch).

    To have it put out 00:00:00 set the 2nd argument to 0 and the 3rd to %H:%M:%S.

    As the pts function can take up to 3 arguments, make sure you escape the colons twice on Windows and three times on Unix to prevent %M and %S from being interpreted as a possible 4th or 5th argument.

    Windows:

    ffplay -vf "drawtext=text='%{pts\:gmtime\:0\:%H\\\:%M\\\:%S}':x=1:y=1:fontsize=20:fontcolor=white:box=1:boxcolor=black:boxborderw=2" myfile.mp4
    

    Unix:

    ffplay -vf "drawtext=text='%{pts\:gmtime\:0\:%H\\\\\:%M\\\\\:%S}':x=1:y=1:fontsize=20:fontcolor=white:box=1:boxcolor=black:boxborderw=2" myfile.mp4
    

    Compared to your initial command I've also added :x=1:y=1 and :boxborderw=2. It adds a small border around the text and positions the text nicely centered.
    It's a matter of taste of course. You can remove it if you don't like it.