[FFmpeg-devel] [PATCH] lavfi/drawtext: Add localtime_ms for millisecond precision
Thilo Borgmann
thilo.borgmann at mail.de
Tue Jun 8 19:42:54 EEST 2021
Hi,
add %{localtime_ms} function to the drawtext filter. Same as %{localtime} but with additional millisecond part.
-Thilo
-------------- next part --------------
From f3b32f3e2b01f50b4fa41f8432ccd5a5eeea1ec1 Mon Sep 17 00:00:00 2001
From: Kirill Pugin <ikir at fb.com>
Date: Tue, 8 Jun 2021 18:32:45 +0200
Subject: [PATCH] lavfi/drawtext: Add localtime_ms for millisecond precision
Suggested-By: ffmpeg at fb.com
---
doc/filters.texi | 4 ++++
libavfilter/vf_drawtext.c | 20 +++++++++++++++++++-
2 files changed, 23 insertions(+), 1 deletion(-)
diff --git a/doc/filters.texi b/doc/filters.texi
index 78faf767cf..bf073e0560 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -10953,6 +10953,10 @@ It can accept an argument: a strftime() format string.
The time at which the filter is running, expressed in the local time zone.
It can accept an argument: a strftime() format string.
+ at item localtime_ms
+Same as @code{localtime} but with millisecond precision.
+It can accept an argument: a strftime() format string.
+
@item metadata
Frame metadata. Takes one or two arguments.
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();
+ 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 },
{ "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