[FFmpeg-devel] [PATCH] Add -strftime_mkdir to image2 and segment outputs (based on HLS).

Roger Hardiman opensource at rjh.org.uk
Sun Jun 13 09:29:07 EEST 2021


Add -strftime_mkdir to create any directories in the output file path for Image2 and Segment outputs. Based on existing function in HLS output.
This allows strftime re-writes eg %Y %d %d (Year/Month/Day) and %H %M %S (Hours/Minutes/Seconds) to be used in the directory path of files ffmpeg creates. This makes organising the output of ffmpeg easier, for example creating folders with Year-Month-Day on the fly.
Example output filename: /recordings/%Y_%m_%d/%H/video_%Y_%m_%d-%H_%M_%S.mp4
will expand to /recordings/2021_06_13/14/2021_06_13-14_01_50.mp4
with all the sub-directories created on the fly.

Patch contains source code and documentation file changes.

Signed-off-by: Roger Hardiman <opensource at rjh.org.uk>
---
 doc/muxers.texi        | 16 +++++++++++++++-
 libavformat/img2enc.c  | 15 +++++++++++++--
 libavformat/internal.h |  8 ++++++++
 libavformat/segment.c  | 24 ++++++++++++++++++++----
 libavformat/utils.c    | 15 +++++++++++++++
 5 files changed, 71 insertions(+), 7 deletions(-)

diff --git a/doc/muxers.texi b/doc/muxers.texi
index e77055e7ef..142b1864ea 100644
--- a/doc/muxers.texi
+++ b/doc/muxers.texi
@@ -907,7 +907,7 @@ is expanded in @var{filename}.
 @example
 ffmpeg -i in.nut -strftime 1 -strftime_mkdir 1 -hls_segment_filename '%Y%m%d/file-%Y%m%d-%s.ts' out.m3u8
 @end example
-This example will create a directory 201560215 (if it does not exist), and then
+This example will create a directory 20160215 (if it does not exist), and then
 produce the playlist, @file{out.m3u8}, and segment files:
 @file{20160215/file-20160215-1455569023.ts}, @file{20160215/file-20160215-1455569024.ts}, etc.
 
@@ -1419,6 +1419,10 @@ overwritten with new images. Default value is 0.
 If set to 1, expand the filename with date and time information from
 @code{strftime()}. Default value is 0.
 
+ at item strftime_mkdir @var{1|0}
+Used together with -strftime, when enabled it will create all subdirectories which
+are expanded in @var{filename}. Default value is @code{0}.
+
 @item protocol_opts @var{options_list}
 Set protocol options as a :-separated list of key=value parameters. Values
 containing the @code{:} special character must be escaped.
@@ -2402,6 +2406,10 @@ segments to write. If this is selected, the output segment name must
 contain a @code{strftime} function template. Default value is
 @code{0}.
 
+ at item strftime_mkdir @var{1|0}
+Used together with -strftime, when enabled it will create all subdirectories which
+are expanded in @var{filename}. Default value is @code{0}.
+
 @item break_non_keyframes @var{1|0}
 If enabled, allow segments to start on frames other than keyframes. This
 improves behavior on some players when the time between keyframes is
@@ -2484,6 +2492,12 @@ as live HLS source):
 ffmpeg -re -i in.mkv -codec copy -map 0 -f segment -segment_list playlist.m3u8 \
 -segment_list_flags +live -segment_time 10 out%03d.mkv
 @end example
+
+ at item
+Segment an RTSP stream and create new output files every 60 seconds. Create new folders automatically with -strftime_mkdir:
+ at example
+ffmpeg -i rtsp://192.168.1.195/h264 -f segment -segment_time 60 -strftime 1 -strftime_mkdir 1 -segment_format mp4 "/recordings/%Y-%m-%d/recording_%Y-%m-%d_%H-%M-%S.mp4"
+ at end example
 @end itemize
 
 @section smoothstreaming
diff --git a/libavformat/img2enc.c b/libavformat/img2enc.c
index 7b5133d300..33d6d09b52 100644
--- a/libavformat/img2enc.c
+++ b/libavformat/img2enc.c
@@ -42,6 +42,7 @@ typedef struct VideoMuxData {
     char target[4][1024];
     int update;
     int use_strftime;
+    int use_strftime_mkdir;
     int frame_pts;
     const char *muxer;
     int use_rename;
@@ -130,6 +131,7 @@ static int write_packet(AVFormatContext *s, AVPacket *pkt)
     VideoMuxData *img = s->priv_data;
     AVIOContext *pb[4] = {0};
     char filename[1024];
+    char *fname;
     AVCodecParameters *par = s->streams[pkt->stream_index]->codecpar;
     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(par->format);
     int ret, i;
@@ -166,8 +168,16 @@ static int write_packet(AVFormatContext *s, AVPacket *pkt)
         av_dict_copy(&options, img->protocol_opts, 0);
         snprintf(img->tmp[i], sizeof(img->tmp[i]), "%s.tmp", filename);
         av_strlcpy(img->target[i], filename, sizeof(img->target[i]));
-        if (s->io_open(s, &pb[i], img->use_rename ? img->tmp[i] : filename, AVIO_FLAG_WRITE, &options) < 0) {
-            av_log(s, AV_LOG_ERROR, "Could not open file : %s\n", img->use_rename ? img->tmp[i] : filename);
+        fname = (img->use_rename ? img->tmp[i] : filename);
+        if (img->use_strftime_mkdir) {
+            if ((ff_mkdir_filename(fname)) < 0) {
+                av_log(s, AV_LOG_ERROR, "Failed to create directory path for '%s'\n", fname);
+                ret = AVERROR(EIO);
+                goto fail;
+            }
+        }
+        if (s->io_open(s, &pb[i], fname, AVIO_FLAG_WRITE, &options) < 0) {
+            av_log(s, AV_LOG_ERROR, "Could not open file : %s\n", fname);
             ret = AVERROR(EIO);
             goto fail;
         }
@@ -243,6 +253,7 @@ static const AVOption muxoptions[] = {
     { "update",       "continuously overwrite one file", OFFSET(update),  AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0,       1, ENC },
     { "start_number", "set first number in the sequence", OFFSET(img_number), AV_OPT_TYPE_INT,  { .i64 = 1 }, 0, INT_MAX, ENC },
     { "strftime",     "use strftime for filename", OFFSET(use_strftime),  AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, ENC },
+    { "strftime_mkdir", "create directory components in strftime-generated filename", OFFSET(use_strftime_mkdir), 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 },
     { "protocol_opts", "specify protocol options for the opened files", OFFSET(protocol_opts), AV_OPT_TYPE_DICT, {0}, 0, 0, ENC },
diff --git a/libavformat/internal.h b/libavformat/internal.h
index c6adf7b872..8795be43ba 100644
--- a/libavformat/internal.h
+++ b/libavformat/internal.h
@@ -430,6 +430,14 @@ do {\
  */
 int ff_mkdir_p(const char *path);
 
+/**
+ * Automatically create sub-directories from a filename with a full path
+ *
+ * @param fn will create sub-directories required by the filename with a full path
+ * @return 0, or < 0 on error
+ */
+int ff_mkdir_filename(const char *fn);
+
 char *ff_data_to_hex(char *buf, const uint8_t *src, int size, int lowercase);
 
 /**
diff --git a/libavformat/segment.c b/libavformat/segment.c
index 24490f1280..44d8de1dad 100644
--- a/libavformat/segment.c
+++ b/libavformat/segment.c
@@ -93,6 +93,7 @@ typedef struct SegmentContext {
     AVIOContext *list_pb;  ///< list file put-byte context
     int64_t time;          ///< segment duration
     int use_strftime;      ///< flag to expand filename with strftime
+    int use_strftime_mkdir; ///< flag to mkdir dirname in timebased filename
     int increment_tc;      ///< flag to increment timecode if found
 
     char *times_str;       ///< segment times specification string
@@ -248,6 +249,13 @@ static int segment_start(AVFormatContext *s, int write_header)
     if ((err = set_segment_filename(s)) < 0)
         return err;
 
+    if (seg->use_strftime_mkdir) {
+        if ((err = ff_mkdir_filename(oc->url)) < 0) {
+            av_log(s, AV_LOG_ERROR, "Failed to create directory path for '%s'\n", oc->url);
+            return err;
+        }
+    }
+
     if ((err = s->io_open(s, &oc->pb, oc->url, AVIO_FLAG_WRITE, NULL)) < 0) {
         av_log(s, AV_LOG_ERROR, "Failed to open segment '%s'\n", oc->url);
         return err;
@@ -674,6 +682,7 @@ static int seg_init(AVFormatContext *s)
     AVDictionary *options = NULL;
     int ret;
     int i;
+    char *filename;
 
     seg->segment_count = 0;
     if (!seg->write_header_trailer)
@@ -756,10 +765,16 @@ static int seg_init(AVFormatContext *s)
     oc = seg->avf;
 
     if (seg->write_header_trailer) {
-        if ((ret = s->io_open(s, &oc->pb,
-                              seg->header_filename ? seg->header_filename : oc->url,
-                              AVIO_FLAG_WRITE, NULL)) < 0) {
-            av_log(s, AV_LOG_ERROR, "Failed to open segment '%s'\n", oc->url);
+        filename = (seg->header_filename ? seg->header_filename : oc->url);
+        if (seg->use_strftime_mkdir) {
+            if ((ret = ff_mkdir_filename(filename)) < 0) {
+                av_log(s, AV_LOG_ERROR, "Failed to create directory path for '%s'\n", filename);
+                return ret;
+            }
+        }
+
+        if ((ret = s->io_open(s, &oc->pb, filename, AVIO_FLAG_WRITE, NULL)) < 0) {
+            av_log(s, AV_LOG_ERROR, "Failed to open segment '%s'\n", filename);
             return ret;
         }
         if (!seg->individual_header_trailer)
@@ -1034,6 +1049,7 @@ static const AVOption options[] = {
     { "segment_start_number", "set the sequence number of the first segment", OFFSET(segment_idx), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
     { "segment_wrap_number", "set the number of wrap before the first segment", OFFSET(segment_idx_wrap_nb), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
     { "strftime",          "set filename expansion with strftime at segment creation", OFFSET(use_strftime), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, E },
+    { "strftime_mkdir", "create directory component in strftime-generated filename", OFFSET(use_strftime_mkdir), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, E},
     { "increment_tc", "increment timecode between each segment", OFFSET(increment_tc), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, E },
     { "break_non_keyframes", "allow breaking segments on non-keyframes", OFFSET(break_non_keyframes), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, E },
 
diff --git a/libavformat/utils.c b/libavformat/utils.c
index 5582d108d0..7335f820be 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -4745,6 +4745,21 @@ int ff_mkdir_p(const char *path)
     return ret;
 }
 
+int ff_mkdir_filename(const char *fn) {
+    char *fn_copy = av_strdup(fn);
+    if (!fn_copy)
+        return AVERROR(ENOMEM);
+    else {
+        const char *dir = av_dirname(fn_copy);
+        if (ff_mkdir_p(dir) == -1 && errno != EEXIST) {
+            av_freep(&fn_copy);
+            return AVERROR(errno);
+        }
+    }
+    av_freep(&fn_copy);
+    return 0;
+}
+
 char *ff_data_to_hex(char *buff, const uint8_t *src, int s, int lowercase)
 {
     int i;
-- 
2.25.1



More information about the ffmpeg-devel mailing list