diff mbox series

[FFmpeg-devel] avformat/segment: add -strftime_mkdir option

Message ID b9a1f8cc-3b36-36b2-ceb6-f45c7c520374@sx.gl
State New
Headers show
Series [FFmpeg-devel] avformat/segment: add -strftime_mkdir option | expand

Checks

Context Check Description
yinshiyou/configure_loongarch64 warning Failed to apply patch
andriy/configure_x86 warning Failed to apply patch

Commit Message

George-Cristian Jiglau Oct. 10, 2022, 8:53 a.m. UTC
This enabled automatically creating directories for strftime-formatted
segment names.

Signed-off-by: George-Cristian Jiglau <george@mux.ro>
---
  doc/muxers.texi       | 11 +++++++++++
  libavformat/segment.c | 18 +++++++++++++++++-
  2 files changed, 28 insertions(+), 1 deletion(-)

              av_log(oc, AV_LOG_ERROR, "Could not get segment filename 
with strftime\n");
              return AVERROR(EINVAL);
          }
+        if (seg->use_strftime_mkdir) {
+            const char *dir;
+            char *fn_copy = av_strdup(oc->url);
+            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 strftime_mkdir\n", dir);
+                av_freep(&fn_copy);
+                return AVERROR(errno);
+            }
+            av_freep(&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);
@@ -1038,6 +1053,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 components 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 },
  -- 2.36.2
diff mbox series

Patch

diff --git a/doc/muxers.texi b/doc/muxers.texi
index 4edbb22b00..77792379d9 100644
--- a/doc/muxers.texi
+++ b/doc/muxers.texi
@@ -2445,6 +2445,17 @@  segments to write. If this is selected, the 
output segment name must
  contain a @code{strftime} function template. Default value is
  @code{0}.
  +@item strftime_mkdir @var{1|0|
+Used together with -strftime, it will create all subdirectories of the
+expanded segment name. Default value is @code{0}.
+
+@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 hierarchy 2016/02/15 (if any of 
them do not exist), and then
+produce the playlist, @file{out.m3u8}, and segment files:
+@file{2016/02/15/file-20160215-1455569023.ts}, 
@file{2016/02/15/file-20160215-1455569024.ts}, etc.
+
  @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
diff --git a/libavformat/segment.c b/libavformat/segment.c
index c904e20708..ef95b34beb 100644
--- a/libavformat/segment.c
+++ b/libavformat/segment.c
@@ -93,9 +93,11 @@  typedef struct SegmentContext {
      int list_type;         ///< set the list type
      AVIOContext *list_pb;  ///< list file put-byte context
      int64_t time;          ///< segment duration
-    int use_strftime;      ///< flag to expand filename with strftime
      int increment_tc;      ///< flag to increment timecode if found
  +    int use_strftime;       ///< flag to expand filename with strftime
+    int use_strftime_mkdir; ///< flag to mkdir dirname in strftime filename
+
      char *times_str;       ///< segment times specification string
      int64_t *times;        ///< list of segment interval specification
      int nb_times;          ///< number of elments in the times array
@@ -203,6 +205,19 @@  static int set_segment_filename(AVFormatContext *s)