[FFmpeg-devel] [PATCH 1/3] avformat/utils: add a function to standardize creation time

Marton Balint cus at passwd.hu
Sun Feb 28 14:28:19 CET 2016


This can be used for formats which write all format metadata as string to
files, therefore non-standard creation times such as 'now' will be parsed.

The standardized creation time is UTC ISO 8601 with microsecond precision.

Signed-off-by: Marton Balint <cus at passwd.hu>
---
 libavformat/internal.h |  8 ++++++++
 libavformat/utils.c    | 22 ++++++++++++++++++++++
 libavformat/version.h  |  2 +-
 3 files changed, 31 insertions(+), 1 deletion(-)

diff --git a/libavformat/internal.h b/libavformat/internal.h
index bc6a6c2..e8601db 100644
--- a/libavformat/internal.h
+++ b/libavformat/internal.h
@@ -560,6 +560,14 @@ void ff_format_io_close(AVFormatContext *s, AVIOContext **pb);
  */
 int ff_parse_creation_time_metadata(AVFormatContext *s, int64_t *timestamp, int return_seconds);
 
+/**
+ * Standardize creation_time metadata in AVFormatContext to an ISO-8601
+ * timestamp string.
+ *
+ * @param s AVFormatContext
+ * @return <0 on error
+ */
+int ff_standardize_creation_time(AVFormatContext *s);
 
 #define CONTAINS_PAL 2
 /**
diff --git a/libavformat/utils.c b/libavformat/utils.c
index 78255c3..ecb4f38 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -33,6 +33,7 @@
 #include "libavutil/parseutils.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/time.h"
+#include "libavutil/time_internal.h"
 #include "libavutil/timestamp.h"
 
 #include "libavcodec/bytestream.h"
@@ -4758,3 +4759,24 @@ int ff_parse_creation_time_metadata(AVFormatContext *s, int64_t *timestamp, int
     }
     return 0;
 }
+
+int ff_standardize_creation_time(AVFormatContext *s)
+{
+    int64_t timestamp;
+    int ret = ff_parse_creation_time_metadata(s, &timestamp, 0);
+    if (ret == 1) {
+        time_t seconds = timestamp / 1000000;
+        struct tm *ptm, tmbuf;
+        ptm = gmtime_r(&seconds, &tmbuf);
+        if (ptm) {
+            char buf[32];
+            if (!strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S", ptm))
+                return AVERROR_EXTERNAL;
+            av_strlcatf(buf, sizeof(buf), ".%06dZ", (int)(timestamp % 1000000));
+            av_dict_set(&s->metadata, "creation_time", buf, 0);
+        } else {
+            return AVERROR_EXTERNAL;
+        }
+    }
+    return ret;
+}
diff --git a/libavformat/version.h b/libavformat/version.h
index 82a8892..9f4ede7 100644
--- a/libavformat/version.h
+++ b/libavformat/version.h
@@ -31,7 +31,7 @@
 
 #define LIBAVFORMAT_VERSION_MAJOR  57
 #define LIBAVFORMAT_VERSION_MINOR  26
-#define LIBAVFORMAT_VERSION_MICRO 100
+#define LIBAVFORMAT_VERSION_MICRO 101
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
                                                LIBAVFORMAT_VERSION_MINOR, \
-- 
2.6.2



More information about the ffmpeg-devel mailing list