From e3feccc7d6612c533f1350daf5b3745ab49537a3 Mon Sep 17 00:00:00 2001
From: rogerdpack <rogerpack2005@gmail.com>
Date: Fri, 18 Nov 2016 16:05:57 -0700
Subject: [PATCH] allow %t in filepattern, based on patch from Yuval Adam

Signed-off-by: rogerdpack <rogerpack2005@gmail.com>
---
 doc/muxers.texi        | 13 +++++++++++++
 libavformat/hlsenc.c   | 12 ++++++------
 libavformat/img2enc.c  |  8 +++++---
 libavformat/internal.h | 18 ++++++++++++++++++
 libavformat/utils.c    | 47 +++++++++++++++++++++++++++++++++++++++++++----
 5 files changed, 85 insertions(+), 13 deletions(-)

diff --git a/doc/muxers.texi b/doc/muxers.texi
index 806182a..670fcca 100644
--- a/doc/muxers.texi
+++ b/doc/muxers.texi
@@ -622,6 +622,12 @@ If the pattern contains "%d" or "%0@var{N}d", the first filename of
 the file list specified will contain the number 1, all the following
 numbers will be sequential.
 
+If the pattern contains "%t", the frame's timestamps will be inserted
+in the filename like "00.00.00.000" for hours, minutes, seconds,
+and milliseconds.
+
+The "%t" and "%d" patterns may be used simultaneously.
+
 The pattern may contain a suffix which is used to automatically
 determine the format of the image files to write.
 
@@ -667,6 +673,13 @@ can be used:
 ffmpeg -f v4l2 -r 1 -i /dev/video0 -f image2 -strftime 1 "%Y-%m-%d_%H-%M-%S.jpg"
 @end example
 
+The following example uses the timestamp parameter to generate one
+image file per video frame from the input, and name it including its original
+timestamp.
+@example
+ffmpeg -i in.avi -vsync vfr -copyts img-%t.jpg
+@end example
+
 @subsection Options
 
 @table @option
diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
index e16fb0c..7666bfe 100644
--- a/libavformat/hlsenc.c
+++ b/libavformat/hlsenc.c
@@ -653,9 +653,9 @@ static int hls_start(AVFormatContext *s)
             av_strlcpy(vtt_oc->filename, c->vtt_basename,
                   sizeof(vtt_oc->filename));
     } else if (c->max_seg_size > 0) {
-        if (av_get_frame_filename2(oc->filename, sizeof(oc->filename),
+        if (ff_get_frame_filename3(oc->filename, sizeof(oc->filename),
             c->basename, c->wrap ? c->sequence % c->wrap : c->sequence,
-            AV_FRAME_FILENAME_FLAGS_MULTIPLE) < 0) {
+            AV_FRAME_FILENAME_FLAGS_MULTIPLE, 0) < 0) {
                 av_log(oc, AV_LOG_ERROR, "Invalid segment filename template '%s', you can try to use -use_localtime 1 with it\n", c->basename);
                 return AVERROR(EINVAL);
         }
@@ -684,16 +684,16 @@ static int hls_start(AVFormatContext *s)
                 }
                 av_free(fn_copy);
             }
-        } else if (av_get_frame_filename2(oc->filename, sizeof(oc->filename),
+        } else if (ff_get_frame_filename3(oc->filename, sizeof(oc->filename),
                                   c->basename, c->wrap ? c->sequence % c->wrap : c->sequence,
-                                  AV_FRAME_FILENAME_FLAGS_MULTIPLE) < 0) {
+                                  AV_FRAME_FILENAME_FLAGS_MULTIPLE, 0) < 0) {
             av_log(oc, AV_LOG_ERROR, "Invalid segment filename template '%s' you can try to use -use_localtime 1 with it\n", c->basename);
             return AVERROR(EINVAL);
         }
         if( c->vtt_basename) {
-            if (av_get_frame_filename2(vtt_oc->filename, sizeof(vtt_oc->filename),
+            if (ff_get_frame_filename3(vtt_oc->filename, sizeof(vtt_oc->filename),
                               c->vtt_basename, c->wrap ? c->sequence % c->wrap : c->sequence,
-                              AV_FRAME_FILENAME_FLAGS_MULTIPLE) < 0) {
+                              AV_FRAME_FILENAME_FLAGS_MULTIPLE, 0) < 0) {
                 av_log(vtt_oc, AV_LOG_ERROR, "Invalid segment filename template '%s'\n", c->vtt_basename);
                 return AVERROR(EINVAL);
             }
diff --git a/libavformat/img2enc.c b/libavformat/img2enc.c
index 1297b1a..12a1e12 100644
--- a/libavformat/img2enc.c
+++ b/libavformat/img2enc.c
@@ -80,10 +80,12 @@ static int write_packet(AVFormatContext *s, AVPacket *pkt)
     VideoMuxData *img = s->priv_data;
     AVIOContext *pb[4];
     char filename[1024];
-    AVCodecParameters *par = s->streams[pkt->stream_index]->codecpar;
+    AVStream *stream = s->streams[ pkt->stream_index ];
+    AVCodecParameters *par = stream->codecpar;
     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(par->format);
     int i;
     int nb_renames = 0;
+    int64_t ts = av_rescale_q(pkt->pts, stream->time_base, AV_TIME_BASE_Q);
 
     if (!img->is_pipe) {
         if (img->update) {
@@ -97,9 +99,9 @@ static int write_packet(AVFormatContext *s, AVPacket *pkt)
                 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,
+        } else if (ff_get_frame_filename3(filename, sizeof(filename), img->path,
                                           img->img_number,
-                                          AV_FRAME_FILENAME_FLAGS_MULTIPLE) < 0 &&
+                                          AV_FRAME_FILENAME_FLAGS_MULTIPLE, ts) < 0 &&
                    img->img_number > 1) {
             av_log(s, AV_LOG_ERROR,
                    "Could not get frame filename number %d from pattern '%s' (either set updatefirst or use a pattern like %%03d within the filename pattern)\n",
diff --git a/libavformat/internal.h b/libavformat/internal.h
index 9d614fb..2da04d9 100644
--- a/libavformat/internal.h
+++ b/libavformat/internal.h
@@ -356,6 +356,24 @@ void ff_reduce_index(AVFormatContext *s, int stream_index);
 enum AVCodecID ff_guess_image2_codec(const char *filename);
 
 /**
+ * Return in 'buf' the path with '%d' replaced by a number.
+ *
+ * Also handles the '%0nd' format where 'n' is the total number
+ * of digits and '%%'.
+ * Also handles the '%t' format where 't' is the timestamp.
+ *
+ * @param buf destination buffer
+ * @param buf_size destination buffer size
+ * @param path numbered sequence string
+ * @param number frame number
+ * @param flags AV_FRAME_FILENAME_FLAGS_*
+ * @param ts frame timestamp in AV_TIME_BASE fractional seconds.
+ * @return 0 if OK, -1 on format error
+ */
+int ff_get_frame_filename3(char *buf, int buf_size,
+                          const char *path, int number, int flags, int64_t ts);
+
+/**
  * Perform a binary search using av_index_search_timestamp() and
  * AVInputFormat.read_timestamp().
  *
diff --git a/libavformat/utils.c b/libavformat/utils.c
index 5664646..1c2744c 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -4391,15 +4391,18 @@ uint64_t ff_ntp_time(void)
     return (av_gettime() / 1000) * 1000 + NTP_OFFSET_US;
 }
 
-int av_get_frame_filename2(char *buf, int buf_size, const char *path, int number, int flags)
+int ff_get_frame_filename3(char *buf, int buf_size, const char *path, int number, int flags, int64_t ts)
 {
     const char *p;
     char *q, buf1[20], c;
-    int nd, len, percentd_found;
+    int nd, len, percentd_found, percentt_found;
+    int hours, mins, secs, ms;
+    int64_t abs_ts;
 
     q = buf;
     p = path;
     percentd_found = 0;
+    percentt_found = 0;
     for (;;) {
         c = *p++;
         if (c == '\0')
@@ -4428,6 +4431,37 @@ int av_get_frame_filename2(char *buf, int buf_size, const char *path, int number
                 memcpy(q, buf1, len);
                 q += len;
                 break;
+            case 't':
+                if (!(flags & AV_FRAME_FILENAME_FLAGS_MULTIPLE) && percentt_found) {
+                    av_log(NULL, AV_LOG_ERROR, "double %%t not allowed");
+                    goto fail;
+                }
+                if (ts == 0) {
+                    av_log(NULL, AV_LOG_DEBUG, "%%t but no ts, using 0"); // necessary for first frame on some streams
+                }
+                percentt_found = 1;
+                abs_ts = llabs(ts);
+                ms = abs_ts % AV_TIME_BASE;
+                abs_ts /= AV_TIME_BASE;
+                secs = abs_ts % 60;
+                abs_ts /= 60;
+                mins = abs_ts % 60;
+                abs_ts /= 60;
+                hours = abs_ts;
+                if (ts < 0)
+                      snprintf(buf1, sizeof(buf1),
+                             "-%02d.%02d.%02d.%03d", hours, mins, secs, ms);
+                else
+                    snprintf(buf1, sizeof(buf1),
+                             "%02d.%02d.%02d.%03d", hours, mins, secs, ms);
+                len = strlen(buf1);
+                if ((q - buf + len) > buf_size - 1) {
+                   av_log(NULL, AV_LOG_ERROR, "%%t size overflow");
+                   goto fail;
+                }
+                memcpy(q, buf1, len);
+                q += len;
+                break;
             default:
                 goto fail;
             }
@@ -4437,7 +4471,7 @@ addchar:
                 *q++ = c;
         }
     }
-    if (!percentd_found)
+    if (!percentd_found && !percentt_found)
         goto fail;
     *q = '\0';
     return 0;
@@ -4448,7 +4482,12 @@ fail:
 
 int av_get_frame_filename(char *buf, int buf_size, const char *path, int number)
 {
-    return av_get_frame_filename2(buf, buf_size, path, number, 0);
+    return ff_get_frame_filename3(buf, buf_size, path, number, 0, 0);
+}
+
+int av_get_frame_filename2(char *buf, int buf_size, const char *path, int number, int flags)
+{
+    return ff_get_frame_filename3(buf, buf_size, path, number, flags, 0);
 }
 
 void av_url_split(char *proto, int proto_size,
-- 
2.9.3 (Apple Git-75)

