[FFmpeg-devel] Patch for timestamp pattern in filename
Matthijs Tempels
matthijs at townsville.nl
Thu Jan 29 17:11:45 CET 2015
improved the code:
From 772232db7f6e1dc45e63929b0371acf0c13be7a1 Mon Sep 17 00:00:00 2001
From: Matthijs Tempels <matthijs at townsville.nl>
Date: Thu, 29 Jan 2015 16:33:46 +0100
Subject: [PATCH] Improved %t handling to add a timestamp to the filename
including milliseconds
---
libavformat/utils.c | 31 ++++++++++++++++++++++++++-----
1 file changed, 26 insertions(+), 5 deletions(-)
diff --git a/libavformat/utils.c b/libavformat/utils.c
index f6df49b..7280ebc 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -24,6 +24,8 @@
#include <stdarg.h>
#include <stdint.h>
+#include <sys/time.h>
+
#include "config.h"
#include "libavutil/avassert.h"
@@ -3804,12 +3806,16 @@ uint64_t ff_ntp_time(void)
int av_get_frame_filename(char *buf, int buf_size, const char *path, int number)
{
const char *p;
- char *q, buf1[20], c;
- int nd, len, percentd_found;
+ char *q, buf1[30], buf2[30], c;
+ int nd, len, percentd_found, percentt_found;
+ long ms; // Milliseconds
+ time_t s; // Seconds
+ struct timespec spec;
q = buf;
p = path;
percentd_found = 0;
+ percentt_found = 0;
for (;;) {
c = *p++;
if (c == '\0')
@@ -3824,7 +3830,7 @@ int av_get_frame_filename(char *buf, int buf_size, const char *path, int number)
switch (c) {
case '%':
- goto addchar;
+ goto addchar;
case 'd':
if (percentd_found)
goto fail;
@@ -3836,7 +3842,22 @@ int av_get_frame_filename(char *buf, int buf_size, const char *path, int number)
memcpy(q, buf1, len);
q += len;
break;
- default:
+ case 't':
+ if (percentt_found)
+ goto fail;
+ percentt_found = 1;
+ clock_gettime(CLOCK_REALTIME, &spec);
+ s = spec.tv_sec;
+ ms = round(spec.tv_nsec / 1.0e6); // Convert nanoseconds to milliseconds
+ strftime(buf2, sizeof(buf2), "%Y%m%d_%H%M%S", localtime(&s));
+ snprintf(buf1, sizeof(buf1), "%s%03ld", buf2, ms);
+ len = strlen(buf1);
+ if ((q - buf + len) > buf_size - 1)
+ goto fail;
+ memcpy(q, buf1, len);
+ q += len;
+ break;
+ default:
goto fail;
}
} else {
@@ -3845,7 +3866,7 @@ addchar:
*q++ = c;
}
}
- if (!percentd_found)
+ if (!percentd_found && !percentt_found)
goto fail;
*q = '\0';
return 0;
--
2.1.0
> Op 28 jan. 2015, om 14:24 heeft wm4 <nfxjfg at googlemail.com> het volgende geschreven:
>
> On Wed, 28 Jan 2015 14:03:06 +0100
> Matthijs Tempels <matthijs at townsville.nl <mailto:matthijs at townsville.nl>> wrote:
>
>> From 15952b6cb38ac2f532a2f35d50e9dc4f8320c1c5 Mon Sep 17 00:00:00 2001
>> From: Matthijs Tempels <matthijs at townsville.nl>
>> Date: Wed, 28 Jan 2015 13:59:54 +0100
>> Subject: [PATCH] Added the %t option to the filename pattern to add a
>> yyyyMMdd_HHmmssfff pattern to the filename
>>
>> ---
>> libavformat/utils.c | 18 +++++++++++++++++-
>> 1 file changed, 17 insertions(+), 1 deletion(-)
>>
>> diff --git a/libavformat/utils.c b/libavformat/utils.c
>> index f6df49b..b57136a 100644
>> --- a/libavformat/utils.c
>> +++ b/libavformat/utils.c
>> @@ -3805,11 +3805,13 @@ int av_get_frame_filename(char *buf, int buf_size, const char *path, int number)
>> {
>> const char *p;
>> char *q, buf1[20], c;
>> - int nd, len, percentd_found;
>> + int nd, len, percentd_found, percentt_found;
>> + struct timeval tv;
>>
>> q = buf;
>> p = path;
>> percentd_found = 0;
>> + percentt_found = 0;
>> for (;;) {
>> c = *p++;
>> if (c == '\0')
>> @@ -3836,6 +3838,20 @@ int av_get_frame_filename(char *buf, int buf_size, const char *path, int number)
>> memcpy(q, buf1, len);
>> q += len;
>> break;
>> + case 't':
>> + if (percentt_found)
>> + goto fail;
>> + percentt_found = 1;
>> + gettimeofday(&tv, NULL);
>> + int milli = tv.tv_usec / 1000;
>> + strftime(buf1, sizeof(buf1), "%Y%m%d_%H%M%S", localtime(&tv.tv_sec));
>
>> + sprintf(buf1, "%s%03d", buf1, milli);
>
> This line tries to append some stuff to buf1, but:
> - you can't use a string both as source and destination for sprintf
> - this can actually overflow the buf1, because strftime can fill it
> completely
>
>> + len = strlen(buf1);
>> + if ((q - buf + len) > buf_size - 1)
>> + goto fail;
>> + memcpy(q, buf1, len);
>> + q += len;
>> + break;
>> default:
>> goto fail;
>> }
>> --
>> 2.1.0
>> _______________________________________________
>> ffmpeg-devel mailing list
>> ffmpeg-devel at ffmpeg.org <mailto:ffmpeg-devel at ffmpeg.org>
>> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel <http://ffmpeg.org/mailman/listinfo/ffmpeg-devel>
>
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel at ffmpeg.org <mailto:ffmpeg-devel at ffmpeg.org>
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel <http://ffmpeg.org/mailman/listinfo/ffmpeg-devel>
More information about the ffmpeg-devel
mailing list