From 292a7802fe906399fb59267eb87d93624920409d Mon Sep 17 00:00:00 2001
From: James Courtier-Dutton <James.Dutton@gmail.com>
Date: Tue, 25 Sep 2018 15:32:16 +0100
Subject: [PATCH] avformat/segment: strftime date sub-directories
Automatically create sub-directories if needed based on date.
E.g.
ffmpeg ... -timelimit 2147483647 -f segment -strftime 1 -segment_time 10 "%Y/%m/%d/%Y-%m-%d_%H-%M-%S.mkv"
Signed-off-by: James Courtier-Dutton <James.Dutton@gmail.com>
---
libavformat/segment.c | 15 +++++++++++++++
1 file changed, 15 insertions(+)
@@ -200,12 +200,27 @@ static int set_segment_filename(AVFormatContext *s)
if (seg->use_strftime) {
time_t now0;
struct tm *tm, tmpbuf;
+ const char *dir;
+ char *fn_copy;
time(&now0);
tm = localtime_r(&now0, &tmpbuf);
if (!strftime(buf, sizeof(buf), s->url, tm)) {
av_log(oc, AV_LOG_ERROR, "Could not get segment filename with strftime\n");
return AVERROR(EINVAL);
}
+ /* Automatically create directories if needed */
+ /* E.g. %Y/%m/%d/%Y-%m-%d_%H-%M-%S.mkv */
+ fn_copy = av_strdup(buf);
+ if (!fn_copy) {
+ return AVERROR(ENOMEM);
+ }
+ dir = av_dirname(fn_copy);
+ if (ff_mkdir_p(dir) == -1 && errno != EEXIST) {
+ av_log(oc, AV_LOG_ERROR, "Could not create directory %s with use_localtime_mkdir\n", dir);
+ av_free(fn_copy);
+ return AVERROR(errno);
+ }
+ av_free(fn_copy);
} else if (av_get_frame_filename(buf, sizeof(buf),
s->url, seg->segment_idx) < 0) {
av_log(oc, AV_LOG_ERROR, "Invalid segment filename template '%s'\n", s->url);
--
2.7.4