[FFmpeg-devel] [PATCH v2] lavf/img2enc: add support for option strftime_start_realtime

Jun Li junli1026 at gmail.com
Fri Apr 19 03:59:27 EEST 2019


Currently the strftime option generate timestamp based on generation
time. The new option would calcualte timestamp from start_time_realtime
and pkt->pts, based on output's timescale.
---
 doc/muxers.texi       |  5 +++++
 libavformat/img2enc.c | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 39 insertions(+)

diff --git a/doc/muxers.texi b/doc/muxers.texi
index 83ae017d6c..ee99ef621e 100644
--- a/doc/muxers.texi
+++ b/doc/muxers.texi
@@ -1193,6 +1193,11 @@ overwritten with new images. Default value is 0.
 @item strftime
 If set to 1, expand the filename with date and time information from
 @code{strftime()}. Default value is 0.
+
+ at item strftime_start_realtime
+If set to 1, expand the filename with date and time information from
+ at code{strftime()}, starting from start_time_realtime and calcualted
+from pkt->pts in UTC time. Default value is 0.
 @end table
 
 The image muxer supports the .Y.U.V image file format. This format is
diff --git a/libavformat/img2enc.c b/libavformat/img2enc.c
index bec4bf81dd..0f27e5ceaf 100644
--- a/libavformat/img2enc.c
+++ b/libavformat/img2enc.c
@@ -26,6 +26,7 @@
 #include "libavutil/log.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
+#include "libavutil/time.h"
 #include "libavutil/time_internal.h"
 #include "avformat.h"
 #include "avio_internal.h"
@@ -45,6 +46,7 @@ typedef struct VideoMuxData {
     int frame_pts;
     const char *muxer;
     int use_rename;
+    int strftime_start_realtime;
 } VideoMuxData;
 
 static int write_header(AVFormatContext *s)
@@ -105,6 +107,37 @@ static int write_packet(AVFormatContext *s, AVPacket *pkt)
                 av_log(s, AV_LOG_ERROR, "Cannot write filename by pts of the frames.");
                 return AVERROR(EINVAL);
             }
+        } else if (img->strftime_start_realtime) {
+            int64_t start_realtime = s->start_time_realtime;
+            int64_t timestamp = 0; 
+            struct tm *tm;
+            time_t sec = 0;
+
+            if (start_realtime == 0 || start_realtime == AV_NOPTS_VALUE) {
+                // the value is not set by either user or encoder, try use creation_time
+                if (ff_parse_creation_time_metadata(s, &start_realtime, 0) != 0)
+                    av_log(s, AV_LOG_INFO, "Use creation_time as start_realtime.\n");
+                
+                if (start_realtime == 0 || start_realtime == AV_NOPTS_VALUE) {
+                    av_log(s, AV_LOG_WARNING, "Could not get start_time_realtime, set value to now.\n");
+                    timestamp = av_gettime();
+                    s->start_time_realtime = timestamp;
+                } else {
+                    s->start_time_realtime = start_realtime;
+                }
+            }
+            
+            if (!timestamp) {
+                int64_t offset = av_rescale_q(pkt->pts, s->streams[0]->time_base, AV_TIME_BASE_Q);
+                timestamp = s->start_time_realtime + offset;
+            }
+
+            sec = timestamp / AV_TIME_BASE;
+            tm = gmtime(&sec);
+            if (!strftime(filename, sizeof(filename), img->path, tm)) {
+                av_log(s, AV_LOG_ERROR, "Could not get frame filename with strftime\n");
+                return AVERROR(EINVAL);
+            }
         } else if (av_get_frame_filename2(filename, sizeof(filename), img->path,
                                           img->img_number,
                                           AV_FRAME_FILENAME_FLAGS_MULTIPLE) < 0 &&
@@ -215,6 +248,7 @@ static const AVOption muxoptions[] = {
     { "strftime",     "use strftime for filename", OFFSET(use_strftime),  AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, ENC },
     { "frame_pts",    "use current frame pts for filename", OFFSET(frame_pts),  AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, ENC },
     { "atomic_writing", "write files atomically (using temporary files and renames)", OFFSET(use_rename), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, ENC },
+    { "strftime_start_realtime", "use strftime for filename and timestamp calculated from start_time_realtime", OFFSET(strftime_start_realtime), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, ENC },
     { NULL },
 };
 
-- 
2.17.1



More information about the ffmpeg-devel mailing list