From patchwork Mon Sep 24 11:39:42 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Dutton X-Patchwork-Id: 10469 Delivered-To: ffmpegpatchwork@gmail.com Received: by 2002:a02:1286:0:0:0:0:0 with SMTP id 6-v6csp2386283jap; Mon, 24 Sep 2018 04:40:34 -0700 (PDT) X-Google-Smtp-Source: ACcGV60yMffr6RF9l1cIcKoJe1dawPp0auG0nHyOK0cJH++L4MElPBfA9968KmVq7QambnXkgRWc X-Received: by 2002:adf:8024:: with SMTP id 33-v6mr8033162wrk.16.1537789234720; Mon, 24 Sep 2018 04:40:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537789234; cv=none; d=google.com; s=arc-20160816; b=Pu8D6RY5MfZvBPkA7l3CQR7a+1lrcnYEpYo8C5tnIc/WLttA/9Gw+Qx4aHx8i3TiaR iureZqbD0eGUasI7WkB0mo35ydTdD6DTru/0BpUA2XfMbN21zHFvupNh8W7yV4dJAJuw fi7wzHFNORvcMQS970Rt4CBuAXGrbAmCHqPY8LtB51hpDTixrHo9QocUy3CCbWDc9W9L X+XbxuhVOpwt2aYm1qp+tUPw54oW7/nifAFeE6BY8NNJVkaPM71ZieJxKxAKKf4CrfEM voURukHX4QbQxvqOjqns166KICasPj8OzhXdlicEGIlO9LdHj/xHGeN5BjHyOEsOfvGx mAaQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:subject:to :message-id:date:from:references:in-reply-to:mime-version :dkim-signature:delivered-to; bh=LTKr57RhngR8YjM4K/uUJHGfFB6jDtp6SoZSneWL4/w=; b=I/p7j+kYevbKvFOLcELtGzXQp6E7iHJGyuKrzmCjHUPHROz5OitwHMKndI51zpEnMJ hmIg6zzTDmv2PYZRZIbtOWPqgTVelGlLIa6FFJTMkvhm+YR7wdUAqaOGBD0hryjkiDNc NywoYbr/hX8jXdkj/mv9yL3Rci0kJojydHx0+N0BLTCATBG7pk34iayYZ0wfB2QBg1Oj pXhNRF8xpWft6DXwliWj4iOlEc96Nvf01r6nYN/Li9wHx7vHCcL98uP87zLgcOegX4/L dzGFvfBqt78++w6eqnOX4PhIpK+MdaNUeI9VqxA/ISz3cds3x/nVthH3bld42/0G5AJW OCBg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=e0NQpR1H; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id e78-v6si5605357wma.92.2018.09.24.04.40.34; Mon, 24 Sep 2018 04:40:34 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=e0NQpR1H; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 6000A689FDE; Mon, 24 Sep 2018 14:40:16 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ot1-f51.google.com (mail-ot1-f51.google.com [209.85.210.51]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 92DE7689EED for ; Mon, 24 Sep 2018 14:40:09 +0300 (EEST) Received: by mail-ot1-f51.google.com with SMTP id i12-v6so19548392otl.1 for ; Mon, 24 Sep 2018 04:40:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to; bh=Ff9lg0/ZeQMX0eM/9ldxH5YBA5IivH40tjlDy0odB0E=; b=e0NQpR1H+RGCfHNNcpqlz2IhZVfaxaO2Q5UQMRFLCTT66tZ+7YTOlzf6SDUDDs1PPg esiVdE/7lxgyZ+j6fjOnW4g4+sleJPXKyXmUHlW6rvOo+vOGJXZnw2qhfL5mUAS35n7r 13O+1lOXEQLXMY++svIvjE/5VmeS0B4Cxl5mY91X7Mhqt84I+HX3kCN3lkhqgBaaW/I5 l7/1YpK1nGj20c/BDBNfYHJqldAaoGxbi6gcFG9NkOFPgbAFiiWTUKLSKv5ec4dazqNa 5ii70u9kLLzFQy9StxlHm0Mpru72oHwX2FSjYYQZgCGdVmGKgm+OVTqp2TSWupR0C4o5 A9xA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to; bh=Ff9lg0/ZeQMX0eM/9ldxH5YBA5IivH40tjlDy0odB0E=; b=QwEmkKT7iPwk2cJ73tbc3WR//zlu9pMe3ZMFHSJMcKApUhdJp/T31gA7TTf5pl2rlj r6ajma9SESQRLFxceAUy+9Wy+XhBfCohxjAqiMBFk0xTsHPI2DNEcxsn25Z2GqkIvQwu vXYbPum1mOyU06hc3NSByWSMZao6ekcIYWgZDA+Bb995B2H7nPZVQBnHw/fOgL+gcMpI EvIWm8F/qS1kaj8bfYcS9BEc2x+ndUl4ruZisEgyWmTeDBFw3yMw9hnTGwb8ZuEkssoW MgCk7o5/iFvJqSjBjOUJKH6UiCg7RKt727Ic3rsUu8/eIkIpKYRB7Fx1q7UKVF3+JsWv dMpw== X-Gm-Message-State: ABuFfohqxR9CLTGPr2f6a2irILSwYntvGaOtpwK2D3W50e9bkx+Y3Fb3 Vba0ctPzUw4ZYE/WR2pU9ShiIRdQH+rbnUoA6y07uw== X-Received: by 2002:a9d:bad:: with SMTP id 42-v6mr5834150oth.189.1537789223573; Mon, 24 Sep 2018 04:40:23 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a9d:2ef3:0:0:0:0:0 with HTTP; Mon, 24 Sep 2018 04:39:42 -0700 (PDT) In-Reply-To: <20180922162656.GB3200@michaelspb> References: <20180922162656.GB3200@michaelspb> From: James Courtier-Dutton Date: Mon, 24 Sep 2018 12:39:42 +0100 Message-ID: To: FFmpeg development discussions and patches X-Content-Filtered-By: Mailman/MimeDel 2.1.20 Subject: Re: [FFmpeg-devel] [PATCH] libavformat/segment: strftime date sub-directories X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From 6c74670e047bfc9e995c0f968c1688be768d13bd Mon Sep 17 00:00:00 2001 From: James Courtier-Dutton Date: Mon, 24 Sep 2018 12:34:54 +0100 Subject: [PATCH] avformat/segment: strftime date sub-directories 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 --- libavformat/segment.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/libavformat/segment.c b/libavformat/segment.c index 7fb4dc7..b2300d9 100644 --- a/libavformat/segment.c +++ b/libavformat/segment.c @@ -186,6 +186,39 @@ static int segment_mux_init(AVFormatContext *s) return 0; } +static int mkdir_p(const char *path) { + int ret = 0; + char *temp = av_strdup(path); + char *pos = temp; + char tmp_ch = '\0'; + + if (!path || !temp) { + return -1; + } + + if (!strncmp(temp, "/", 1) || !strncmp(temp, "\\", 1)) { + pos++; + } else if (!strncmp(temp, "./", 2) || !strncmp(temp, ".\\", 2)) { + pos += 2; + } + + for ( ; *pos != '\0'; ++pos) { + if (*pos == '/' || *pos == '\\') { + tmp_ch = *pos; + *pos = '\0'; + ret = mkdir(temp, 0755); + *pos = tmp_ch; + } + } + + if ((*(pos - 1) != '/') || (*(pos - 1) != '\\')) { + ret = mkdir(temp, 0755); + } + + av_free(temp); + return ret; +} + static int set_segment_filename(AVFormatContext *s) { SegmentContext *seg = s->priv_data; @@ -200,12 +233,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 (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); -- 2.7.4