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

2022-12-27 Thread Steven Viola
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.00,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 
---
 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();
 tm = localtime_r(, );
+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(>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

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


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

2022-12-27 Thread Steven Viola
I have been using this patch and it has been working great. Surprised that
this functionality isn't already in the muxer.

Steven Viola
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".