[FFmpeg-devel] [PATCH] avformat/segment: Add strftime list prefix formatting

Steven Viola sviola at criticalmention.com
Tue Dec 27 23:46:39 EET 2022


In the segment muxer, when `strftime` is enabled, apply formatting
to the `segment_list_entry_prefix` string if it's set.

```
ffmpeg -i in.mkv -codec copy -map 0 -f segment \
    -segment_list out.csv -segment_list_entry_prefix %Y/ \
    -strftime 1 "/var/www/html/%Y/%Y%m%d%H%M%S.ts"
```

Will produce a CSV with the following:

2022/20221227205722.ts,0.000000,8.089222
2022/20221227205730.ts,8.089222,18.065856
2022/20221227205740.ts,18.065856,28.075856
2022/20221227205750.ts,28.075856,38.085856

If strftime is not set, then no formatting will be applied to
segment_list_entry_prefix.

Signed-off-by: Steven Viola <sviola at criticalmention.com>
---
 libavformat/segment.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)

diff --git a/libavformat/segment.c b/libavformat/segment.c
index c904e20708..4563bff732 100644
--- a/libavformat/segment.c
+++ b/libavformat/segment.c
@@ -190,7 +190,9 @@ static int set_segment_filename(AVFormatContext *s)
     size_t size;
     int ret;
     char buf[1024];
+    char prefix_buf[1024];
     char *new_name;
+    char *new_prefix = NULL;
 
     if (seg->segment_idx_wrap)
         seg->segment_idx %= seg->segment_idx_wrap;
@@ -199,14 +201,24 @@ static int set_segment_filename(AVFormatContext *s)
         struct tm *tm, tmpbuf;
         time(&now0);
         tm = localtime_r(&now0, &tmpbuf);
+        if (seg->entry_prefix) {
+            if (!strftime(prefix_buf, sizeof(prefix_buf), seg->entry_prefix, tm)) {
+                av_log(oc, AV_LOG_ERROR, "Could not get prefix with strftime\n");
+                return AVERROR(EINVAL);
+            }
+            new_prefix = av_strdup(prefix_buf);
+        }
         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);
         }
-    } 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);
-        return AVERROR(EINVAL);
+    } 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);
+            return AVERROR(EINVAL);
+        }
+        new_prefix = av_strdup(seg->entry_prefix);
     }
     new_name = av_strdup(buf);
     if (!new_name)
@@ -215,13 +227,13 @@ static int set_segment_filename(AVFormatContext *s)
 
     /* copy modified name in list entry */
     size = strlen(av_basename(oc->url)) + 1;
-    if (seg->entry_prefix)
-        size += strlen(seg->entry_prefix);
+    if (new_prefix)
+        size += strlen(new_prefix);
 
     if ((ret = av_reallocp(&seg->cur_entry.filename, size)) < 0)
         return ret;
     snprintf(seg->cur_entry.filename, size, "%s%s",
-             seg->entry_prefix ? seg->entry_prefix : "",
+             new_prefix ? new_prefix : "",
              av_basename(oc->url));
 
     return 0;
-- 
2.31.1



More information about the ffmpeg-devel mailing list