[FFmpeg-devel] [PATCH 2/2] avfilter/vf_drawtext: add rtctime

Zhao Zhili quinkblack at foxmail.com
Thu Jun 10 16:25:02 EEST 2021


Compared to gmtime and localtime, rtctime has higher resolution.
For example, it can be used to show the end-to-end latency. On
the same host, publish a stream by:

./ffmpeg \
    -re -f lavfi -i "color=color=blue:size=1280x720:rate=60" \
    -c:v libx264 \
    -tune zerolatency \
    -vf "drawtext=text='push %{rtctime\:hms} pts %{pts\:hms}':x=10:y=(h-th)/2:fontsize=30:box=1:boxcolor=white:boxborderw=30" \
    -f flv $url

Use ffplay to show the latency:
./ffplay -threads 1 \
    -vf "drawtext=text='play %{rtctime\:hms} pts %{pts\:hms}':x=10:y=th:fontsize=30:box=1:boxcolor=white:boxborderw=30,setpts=PTS*4/5" \
    "$url"
---
 doc/filters.texi          |  3 +++
 libavfilter/version.h     |  2 +-
 libavfilter/vf_drawtext.c | 24 ++++++++++++++++++++----
 3 files changed, 24 insertions(+), 5 deletions(-)

diff --git a/doc/filters.texi b/doc/filters.texi
index 78faf767cf..b2d4660327 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -10953,6 +10953,9 @@ 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 rtctime
+The time at which the filter is running, can use the same format as @samp{pts}.
+
 @item metadata
 Frame metadata. Takes one or two arguments.
 
diff --git a/libavfilter/version.h b/libavfilter/version.h
index f12bc876ae..5052681653 100644
--- a/libavfilter/version.h
+++ b/libavfilter/version.h
@@ -31,7 +31,7 @@
 
 #define LIBAVFILTER_VERSION_MAJOR   8
 #define LIBAVFILTER_VERSION_MINOR   0
-#define LIBAVFILTER_VERSION_MICRO 101
+#define LIBAVFILTER_VERSION_MICRO 102
 
 
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
diff --git a/libavfilter/vf_drawtext.c b/libavfilter/vf_drawtext.c
index f7b9c25e62..e20b556a87 100644
--- a/libavfilter/vf_drawtext.c
+++ b/libavfilter/vf_drawtext.c
@@ -51,6 +51,7 @@
 #include "libavutil/opt.h"
 #include "libavutil/random_seed.h"
 #include "libavutil/parseutils.h"
+#include "libavutil/time.h"
 #include "libavutil/timecode.h"
 #include "libavutil/time_internal.h"
 #include "libavutil/tree.h"
@@ -959,12 +960,11 @@ static int func_pict_type(AVFilterContext *ctx, AVBPrint *bp,
     return 0;
 }
 
-static int func_pts(AVFilterContext *ctx, AVBPrint *bp,
-                    char *fct, unsigned argc, char **argv, int tag)
+static int func_time_common(AVFilterContext *ctx, AVBPrint *bp,
+                            char *fct, unsigned argc, char **argv, int tag,
+                            double pts)
 {
-    DrawTextContext *s = ctx->priv;
     const char *fmt;
-    double pts = s->var_values[VAR_T];
     int ret;
 
     fmt = argc >= 1 ? argv[0] : "flt";
@@ -1019,6 +1019,21 @@ static int func_pts(AVFilterContext *ctx, AVBPrint *bp,
     return 0;
 }
 
+static int func_pts(AVFilterContext *ctx, AVBPrint *bp,
+                    char *fct, unsigned argc, char **argv, int tag)
+{
+    DrawTextContext *s = ctx->priv;
+    double pts = s->var_values[VAR_T];
+    return func_time_common(ctx, bp, fct, argc, argv, tag, pts);
+}
+
+static int func_rtctime(AVFilterContext *ctx, AVBPrint *bp,
+                        char *fct, unsigned argc, char **argv, int tag)
+{
+    double pts = av_gettime() / 1000000.0;
+    return func_time_common(ctx, bp, fct, argc, argv, tag, pts);
+}
+
 static int func_frame_num(AVFilterContext *ctx, AVBPrint *bp,
                           char *fct, unsigned argc, char **argv, int tag)
 {
@@ -1153,6 +1168,7 @@ static const struct drawtext_function {
     { "pts",       0, 3, 0,   func_pts      },
     { "gmtime",    0, 1, 'G', func_strftime },
     { "localtime", 0, 1, 'L', func_strftime },
+    { "rtctime",   0, 3, 0,   func_rtctime  },
     { "frame_num", 0, 0, 0,   func_frame_num },
     { "n",         0, 0, 0,   func_frame_num },
     { "metadata",  1, 2, 0,   func_metadata },
-- 
2.31.1



More information about the ffmpeg-devel mailing list