diff mbox

[FFmpeg-devel] libavformat/segment: strftime date sub-directories

Message ID 3A356D75-7489-4FCE-9573-FBC6B0064905@chinaffmpeg.org
State Superseded
Headers show

Commit Message

Liu Steven Sept. 25, 2018, 3:18 p.m. UTC
> On Sep 25, 2018, at 22:37, James Courtier-Dutton <james.dutton@gmail.com> wrote:
> 
> Updated patch.
> This one applies on top of
> commit bb660800a55f5171f77316941afe2e62534ee72c
> 
> 
> On 25 September 2018 at 15:00, Steven Liu <lq@chinaffmpeg.org> wrote:
> 
>> 
>> 
>>> On Sep 25, 2018, at 20:33, James Courtier-Dutton <james.dutton@gmail.com>
>> wrote:
>>> 
>>> On 25 September 2018 at 10:22, Moritz Barsnick <barsnick@gmx.net> wrote:
>>> 
>>>> 
>>>> Steven already posted a patch proposal to this list, factoring out the
>>>> function:
>>>> https://patchwork.ffmpeg.org/patch/10471/
>>>> http://ffmpeg.org/pipermail/ffmpeg-devel/2018-September/234538.html
>>>> 
>>>> 
>>> That looks fine by me. I can resubmit my segment.c patch once 10471
>> reaches
>>> master tree or at least somewhere I can cherry pick from.
>>> 
>>> Kind Regards
>>> 
>>> James
>>> _______________________________________________
>>> ffmpeg-devel mailing list
>>> ffmpeg-devel@ffmpeg.org
>>> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>> Okay, you can go on do this, i have pushed that patch.
>> 
>> Thanks
>> Steven
>> 
>> 
>> 
>> 
>> 
>> _______________________________________________
>> ffmpeg-devel mailing list
>> ffmpeg-devel@ffmpeg.org
>> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>> 
> <0001-avformat-segment-strftime-date-sub-directories.patch>_______________________________________________


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(+)

Comments

James Dutton Sept. 25, 2018, 4:52 p.m. UTC | #1
>
> 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"
>
>
>
>
> Maybe you want add an option named “strftime_mkdir” not
> “use_localtime_mkdir”.
> You should add the option describe into doc/muxer.texi
>
> reference to: https://trac.ffmpeg.org/ticket/7393
>
> > ffmpeg-devel mailing list
> > ffmpeg-devel@ffmpeg.org
> > http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> Thanks
> Steven
>
> Hi Steven,

I don't understand what you are suggesting?
There are no extra command line options to enable this feature.
It just works it out from the format string.
If you put a "/" in the format string, it creates the directories for you.
As per my example command line above.
diff mbox

Patch

diff --git a/libavformat/segment.c b/libavformat/segment.c
index 7fb4dc7..57fce8d 100644
--- a/libavformat/segment.c
+++ b/libavformat/segment.c
@@ -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);