[FFmpeg-devel] [PATCH] lavfi/drawtext: Add localtime_ms for millisecond precision

"zhilizhao(赵志立)" quinkblack at foxmail.com
Wed Jun 16 09:23:38 EEST 2021



> On Jun 15, 2021, at 3:14 AM, Thilo Borgmann <thilo.borgmann at mail.de> wrote:
> 
> Am 08.06.21 um 18:42 schrieb Thilo Borgmann:
>> Hi,
>> 
>> add %{localtime_ms} function to the drawtext filter. Same as %{localtime} but with additional millisecond part. 
> 
> Ping for a volunteer to actually have a look at the patch for review.
> 
> Thanks,
> Thilo
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel at ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> 
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request at ffmpeg.org with subject "unsubscribe”.
> 

> diff --git a/libavfilter/vf_drawtext.c b/libavfilter/vf_drawtext.c
> index 382d589e26..684f954236 100644
> --- a/libavfilter/vf_drawtext.c
> +++ b/libavfilter/vf_drawtext.c
> @@ -53,6 +53,7 @@
>  #include "libavutil/parseutils.h"
>  #include "libavutil/timecode.h"
>  #include "libavutil/time_internal.h"
> +#include "libavutil/time.h"
>  #include "libavutil/tree.h"
>  #include "libavutil/lfg.h"
>  #include "libavutil/detection_bbox.h"
> @@ -1049,11 +1050,27 @@ static int func_strftime(AVFilterContext *ctx, AVBPrint *bp,
>      struct tm tm;
>  
>      time(&now);
> -    if (tag == 'L')
> +    if (tag == 'L' || tag == 'M')
>          localtime_r(&now, &tm);
>      else
>          tm = *gmtime_r(&now, &tm);
>      av_bprint_strftime(bp, fmt, &tm);
> +
> +    if (tag == 'M') {
> +        char ms[5] = {0};
> +        // get time returns time since epoch in micro seconds, localtime_r would
> +        // already take care of representing seconds part, so we need to get
> +        // milliseconds, to do that we round up microseconds to seconds
> +        // then convert seconds back to microseconds - this will drop microsecond
> +        // precision, then compute difference between av_gettime result and
> +        // rounded up sec -> usec representation and convert that to milliseconds
> +        int64_t usecs      = av_gettime();

There is no guarantee that when av_gettime() is called, it’s in the same second as ’time(&now)’
was called. For example:

time(&now) was called at 10.999999 second, and return 10.
av_gettime() was called at 11.001 second

It will show 10.001, which should be 11.001.

> +        int64_t secs       = usecs / 1000000;
> +        int64_t msec_delta = (usecs - secs * 1000000) / 1000;
> +        snprintf(ms, 5, ".%03d", (int)msec_delta);
> +        av_bprint_append_data(bp, ms, 4);
> +    }
> +
>      return 0;
>  }
>  
> @@ -1153,6 +1170,7 @@ static const struct drawtext_function {
>      { "pts",       0, 3, 0,   func_pts      },
>      { "gmtime",    0, 1, 'G', func_strftime },
>      { "localtime", 0, 1, 'L', func_strftime },
> +    { "localtime_ms", 0, 1, 'M', func_strftime },

Then why not add a gmtime_ms?

>      { "frame_num", 0, 0, 0,   func_frame_num },
>      { "n",         0, 0, 0,   func_frame_num },
>      { "metadata",  1, 2, 0,   func_metadata },
> -- 
> 2.20.1 (Apple Git-117)



More information about the ffmpeg-devel mailing list