From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id A3EBF46A92 for ; Wed, 2 Aug 2023 14:35:21 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 8BFC468C63A; Wed, 2 Aug 2023 17:35:19 +0300 (EEST) Received: from nef.ens.fr (nef2.ens.fr [129.199.96.40]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 1CB3668B1FD for ; Wed, 2 Aug 2023 17:35:13 +0300 (EEST) X-ENS-nef-client: 129.199.129.80 ( name = phare.normalesup.org ) Received: from phare.normalesup.org (phare.normalesup.org [129.199.129.80]) by nef.ens.fr (8.14.4/1.01.28121999) with ESMTP id 372EZC8k028775 ; Wed, 2 Aug 2023 16:35:12 +0200 Received: by phare.normalesup.org (Postfix, from userid 1001) id 5B955EB5B7; Wed, 2 Aug 2023 16:35:12 +0200 (CEST) Date: Wed, 2 Aug 2023 16:35:12 +0200 From: Nicolas George To: FFmpeg development discussions and patches Message-ID: References: <20230801201025.33541-1-alexandre.schmidt@gmail.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20230801201025.33541-1-alexandre.schmidt@gmail.com> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.4.3 (nef.ens.fr [129.199.96.32]); Wed, 02 Aug 2023 16:35:12 +0200 (CEST) Subject: Re: [FFmpeg-devel] [PATCH] avformat/img2dec: added option -strftime_mkdir to allow directory creation if the strftime pattern include non-existing directories, similarly to how hls muxer does. X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Alexandre Heitor Schmidt Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: Alexandre Heitor Schmidt (12023-08-01): > doc/demuxers.texi: Documented how to use the new parameter. > > Usage example: > > ffmpeg -i /dev/video0 -strftime 1 -strftime_mkdir 1 "/tmp/%Y/%m/%Y_%m_%d-%H_%M_%S.jpg" > --- > doc/muxers.texi | 13 +++++++++++++ > libavformat/img2enc.c | 28 +++++++++++++++++++++++----- > 2 files changed, 36 insertions(+), 5 deletions(-) Hi. Thanks for the patch. See my remark below. > > diff --git a/doc/muxers.texi b/doc/muxers.texi > index f6071484ff..1d03fef84b 100644 > --- a/doc/muxers.texi > +++ b/doc/muxers.texi > @@ -1421,6 +1421,19 @@ 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. > > +@item strftime_mkdir > +Used together with -strftime, if set to 1, it will create all subdirectories > +which are expanded in @var{filename}. > +@example > +ffmpeg -i /dev/video0 -strftime 1 -strftime_mkdir 1 "/tmp/%Y/%m/%Y_%m_%d-%H_%M_%S.jpg" > +@end example > +This example will read all frames from /dev/video0 and save each frame into > +@file{/tmp/%Y_%m/%Y_%m_%d-%H_%M_%S.jpg}, creating the necessary directory > +structure if it doesn't exist. Supposing the current date at the time of the > +image creation is 1978-04-27 15:01:02, the directory @file{/tmp/1978/04} > +would be created - if it didn't existed - prior from saving the output file > +@file{1978_04_27-15_01_02.jpg} into it. > + > @item atomic_writing > Write output to a temporary file, which is renamed to target filename once > writing is completed. Default is disabled. > diff --git a/libavformat/img2enc.c b/libavformat/img2enc.c > index 9b8ec06cea..238f281467 100644 > --- a/libavformat/img2enc.c > +++ b/libavformat/img2enc.c > @@ -44,6 +44,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; > @@ -157,6 +158,22 @@ static int write_packet(AVFormatContext *s, AVPacket *pkt) > av_log(s, AV_LOG_ERROR, "Could not get frame filename with strftime\n"); > return AVERROR(EINVAL); > } > + > + // whether to create non-existing directory structure > + if (img->use_strftime_mkdir) { > + const char *dir; > + char *fn_copy = av_strdup(filename); > + if (!fn_copy) > + return AVERROR(ENOMEM); > + dir = av_dirname(fn_copy); > + if (ff_mkdir_p(dir) == -1 && errno != EEXIST) { > + av_log(s, AV_LOG_ERROR, "Could not create directory %s with use_strftime_mkdir\n", dir); > + av_freep(&fn_copy); > + return AVERROR(errno); > + } This is copied from hlsenc.c. Please factor it instead. As far as I can see, ff_mkdir_p() is used exactly twice now and will be thrice with this patch. And every time, it is invoked after av_dirname(), so I suggest to turn it into ff_mkdir_p_parent(). That would also fix the missing error checks in format_name(). > + av_log(s, AV_LOG_VERBOSE, "Created directory %s\n", dir); > + av_freep(&fn_copy); > + } > } else if (img->frame_pts) { > if (av_get_frame_filename2(filename, sizeof(filename), s->url, pkt->pts, AV_FRAME_FILENAME_FLAGS_MULTIPLE) < 0) { > av_log(s, AV_LOG_ERROR, "Cannot write filename by pts of the frames."); > @@ -252,12 +269,13 @@ static int query_codec(enum AVCodecID id, int std_compliance) > #define OFFSET(x) offsetof(VideoMuxData, x) > #define ENC AV_OPT_FLAG_ENCODING_PARAM > 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(start_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 }, > - { "frame_pts", "use current frame pts for filename", OFFSET(frame_pts), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, ENC }, > + { "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(start_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 structure for filename, if needed", 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 }, > + { "protocol_opts", "specify protocol options for the opened files", OFFSET(protocol_opts), AV_OPT_TYPE_DICT, {0}, 0, 0, ENC }, > { NULL }, > }; > Regards, -- Nicolas George _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".