From patchwork Tue Dec 27 02:42:23 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Liu X-Patchwork-Id: 1939 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.89.21 with SMTP id n21csp1293843vsb; Mon, 26 Dec 2016 18:42:35 -0800 (PST) X-Received: by 10.194.87.103 with SMTP id w7mr26535137wjz.164.1482806555022; Mon, 26 Dec 2016 18:42:35 -0800 (PST) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id m70si44402417wmg.143.2016.12.26.18.42.33; Mon, 26 Dec 2016 18:42:34 -0800 (PST) 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; 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 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 E9754689C97; Tue, 27 Dec 2016 04:42:28 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm0-f48.google.com (mail-wm0-f48.google.com [74.125.82.48]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id DB54F689971 for ; Tue, 27 Dec 2016 04:42:22 +0200 (EET) Received: by mail-wm0-f48.google.com with SMTP id t79so275672092wmt.0 for ; Mon, 26 Dec 2016 18:42:25 -0800 (PST) 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=+4pJ+acvLwwYEaN1MFKqNGOEnwHFUcB+vJ8dtoR3zLY=; b=mvhU4XALqDlygxItvgmECLy7TIzfkJr+xsMz2ooKrb7D5kRPsEwLWmkr6KmrDIYyhA KI40cb0NDTxcKMWvmQ2QNeqhMLgDaYsgMgXdc9uN/H8orEg4nK2MlKZqvLJ8m8QjMr8P xhF9POGMKlhxVLuhb6T8Q/CXqHp6d4wRZJW5X6wSUa2yGDzFCdsKxharFoJUbQzioHu+ nT2+aM1OnRBNznSYBTH66q4yOyS1EY2RbqStCteyU0Q13DSmcwPDO9WJ3ywD0G5UECNz aUO2XJy9gZj9bBfD09S5gbyQZubQ2U9pA56+LZXaVyITOv3bV2uMI3xRNYdWjHkcJfmi Dx6g== 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=+4pJ+acvLwwYEaN1MFKqNGOEnwHFUcB+vJ8dtoR3zLY=; b=Y8G9Lv5xi8hW3CZCtiFmMbQi1DuDkYFYkIw1Gi5vEjGclbZl7Abz9gFtDuiVMI4cXy HYi/D3tLYogpX19lgOeEI0JpC5nUbUby3ys8RdHCt/qvNl8U+WnLhWzOkSrAGjTGu1Ul uuJwWTxbfHMwaNZ19e9mr86/ARNWD0YFCVEKkJGfdjLMG40CdDLBQkuacFx5vxZFh6an B4Pv3Akh+O2If3letekiMVCeAGaaGglo5GonEi1SNCD7d3bII6MfIzTzkan4xJS8H8Zv sztZjhNHIXnL9i8JqAplvyZwQ2F/0kEzIu7ipFdeiSZ1SKP6Wvh9Pe/GClZv3zLAP56b fJzw== X-Gm-Message-State: AIkVDXKRaUGgTYUQQNGuoxnI93x+BIE2W7Zm7ZihGKIhkeE2cLuV6PihoKApxKeSA07op0shbsMPpOvtkZz+/A== X-Received: by 10.28.21.1 with SMTP id 1mr31014232wmv.133.1482806544545; Mon, 26 Dec 2016 18:42:24 -0800 (PST) MIME-Version: 1.0 Received: by 10.194.171.106 with HTTP; Mon, 26 Dec 2016 18:42:23 -0800 (PST) In-Reply-To: <041a0c8b-e007-078c-c629-220f68cde177@vivanet.hu> References: <041a0c8b-e007-078c-c629-220f68cde177@vivanet.hu> From: Steven Liu Date: Tue, 27 Dec 2016 10:42:23 +0800 Message-ID: To: FFmpeg development discussions and patches X-Content-Filtered-By: Mailman/MimeDel 2.1.20 Subject: Re: [FFmpeg-devel] [PATCH] avformat/hlsenc: strftime identifiers and segment index in segment filenames 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" 2016-12-27 9:30 GMT+08:00 Bodecs Bela : > > > Dear All, > > > Putting date/time values into segment filenames is very usefull. > But to produce non-conflicting segment filenames with -use_localtime > option with date/time > values in hls_segment_filename option, sometimes is not enough. > Like in cases when multiple segments produced in the same second. > But hlsenc currently does not make possible to use segment index (%d) at > the > > same time whe use_localtime is in effect, due to identifier conflict. > This patch makes possible to use strftime identifiers and still put > segment index (%d) at same time in segment filenames by introducing > second_level_segment_index flag. When -use_localtime is active, > identifier %d is for month day index, so %%d is the segment index > placeholder. This enhanced behaviour only exists when new > second_level_segment_index flag is specified. > For instance putting 'segment_%Y%m%d%H%M%S_%%05d.ts' value into > -hls_segment_filename option and specifing -hls_flags > second_level_segment_index and -use_localtime 1, may produce segment > filename as 'segment_20161230235758_00002.ts' > > > An example: > > ffmpeg -loglevel info -y -f lavfi -i color=c=red:size=640x480:r=25 -f > lavfi -i anullsrc=r=44100:cl=stereo -c:v mpeg2video -g 25 -acodec aac > -cutoff 20000 -ac 2 -ar 44100 -ab 192k -f hls -hls_time 3 -hls_list_size > 5 -hls_flags delete_segments+second_level_segment_index -use_localtime 1 > -hls_segment_filename "segment_%Y%m%d%H%M%S_%%05d.ts" stream.m3u8 > > will produce segments filenames: > > .... > segment_20161227005902_00013.ts > segment_20161227005902_00014.ts > segment_20161227005902_00015.ts > segment_20161227005903_00016.ts > segment_20161227005903_00017.ts > segment_20161227005903_00018.ts > segment_20161227005903_00019.ts > segment_20161227005903_00020.ts > .... > > > thank you, > > best regards, > > Bela Bodecs > > > > > _______________________________________________ > ffmpeg-devel mailing list > ffmpeg-devel@ffmpeg.org > http://ffmpeg.org/mailman/listinfo/ffmpeg-devel > > Signed-off-by: Bela Bodecs --- doc/muxers.texi | 11 ++++++++++- libavformat/hlsenc.c | 22 ++++++++++++++++++++-- 2 files changed, 30 insertions(+), 3 deletions(-) typedef enum { @@ -717,7 +718,19 @@ static int hls_start(AVFormatContext *s) av_log(oc, AV_LOG_ERROR, "Could not get segment filename with use_localtime\n"); return AVERROR(EINVAL); } - + if (c->flags & HLS_SECOND_LEVEL_SEGMENT_INDEX) { + char * filename = av_strdup(oc->filename); // %%d will be %d after strftime + if (!filename) + return AVERROR(ENOMEM); + if (av_get_frame_filename2(oc->filename, sizeof(oc->filename), + filename, c->wrap ? c->sequence % c->wrap : c->sequence, + AV_FRAME_FILENAME_FLAGS_MULTIPLE) < 0) { + av_log(c, AV_LOG_ERROR, "Invalid second level segment filename template '%s', you can try to remove second_level_segment_index flag\n", filename); + av_free(filename); + return AVERROR(EINVAL); + } + av_free(filename); + } if (find_segment_by_filename(c->segments, oc->filename) || find_segment_by_filename(c->old_segments, oc->filename)) { av_log(c, AV_LOG_WARNING, "Duplicated segment filename detected: %s\n", oc->filename); @@ -900,7 +913,11 @@ static int hls_write_header(AVFormatContext *s) av_strlcat(hls->basename, pattern, basename_size); } } - + if (!hls->use_localtime && (hls->flags & HLS_SECOND_LEVEL_SEGMENT_INDEX)) { + av_log(hls, AV_LOG_ERROR, "second_level_segment_index hls_flag requires use_localtime to be true\n"); + ret = AVERROR(EINVAL); + goto fail; + } if(hls->has_subtitle) { if (hls->flags & HLS_SINGLE_FILE) @@ -1156,6 +1173,7 @@ static const AVOption options[] = { {"split_by_time", "split the hls segment by time which user set by hls_time", 0, AV_OPT_TYPE_CONST, {.i64 = HLS_SPLIT_BY_TIME }, 0, UINT_MAX, E, "flags"}, {"append_list", "append the new segments into old hls segment list", 0, AV_OPT_TYPE_CONST, {.i64 = HLS_APPEND_LIST }, 0, UINT_MAX, E, "flags"}, {"program_date_time", "add EXT-X-PROGRAM-DATE-TIME", 0, AV_OPT_TYPE_CONST, {.i64 = HLS_PROGRAM_DATE_TIME }, 0, UINT_MAX, E, "flags"}, + {"second_level_segment_index", "include segment index in segment filenames when use_localtime", 0, AV_OPT_TYPE_CONST, {.i64 = HLS_SECOND_LEVEL_SEGMENT_INDEX }, 0, UINT_MAX, E, "flags"}, {"use_localtime", "set filename expansion with strftime at segment creation", OFFSET(use_localtime), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, E }, {"use_localtime_mkdir", "create last directory component in strftime-generated filename", OFFSET(use_localtime_mkdir), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, E }, {"hls_playlist_type", "set the HLS playlist type", OFFSET(pl_type), AV_OPT_TYPE_INT, {.i64 = PLAYLIST_TYPE_NONE }, 0, PLAYLIST_TYPE_NB-1, E, "pl_type" }, diff --git a/doc/muxers.texi b/doc/muxers.texi index 075b8d3..3520120 100644 --- a/doc/muxers.texi +++ b/doc/muxers.texi @@ -443,12 +443,18 @@ This example will produce the playlist, @file{out.m3u8}, and segment files: @item use_localtime Use strftime on @var{filename} to expand the segment filename with localtime. -The segment number (%d) is not available in this mode. +The segment number is also available in this mode, but to use it, you need to specify second_level_segment_index +hls_flag and %%d will be the specifier. @example ffmpeg -i in.nut -use_localtime 1 -hls_segment_filename 'file-%Y%m%d-%s.ts' out.m3u8 @end example This example will produce the playlist, @file{out.m3u8}, and segment files: @file{file-20160215-1455569023.ts}, @file{file-20160215-1455569024.ts}, etc. +@example +ffmpeg -i in.nut -use_localtime 1 -hls_flags second_level_segment_index -hls_segment_filename 'file-%Y%m%d-%%04d.ts' out.m3u8 +@end example +This example will produce the playlist, @file{out.m3u8}, and segment files: +@file{file-20160215-0001.ts}, @file{file-20160215-0002.ts}, etc. @item use_localtime_mkdir Used together with -use_localtime, it will create up to one subdirectory which @@ -556,6 +562,9 @@ seeking. This flag should be used with the @code{hls_time} option. @item hls_flags program_date_time Generate @code{EXT-X-PROGRAM-DATE-TIME} tags. +@item hls_flags second_level_segment_index +Makes it possible to use segment indexes as %%d besides date/time values when use_localtime is on. + @item hls_playlist_type event Emit @code{#EXT-X-PLAYLIST-TYPE:EVENT} in the m3u8 header. Forces @option{hls_list_size} to 0; the playlist can only be appended to. diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c index e46e9b4..a20666a 100644 --- a/libavformat/hlsenc.c +++ b/libavformat/hlsenc.c @@ -66,6 +66,7 @@ typedef enum HLSFlags { HLS_SPLIT_BY_TIME = (1 << 5), HLS_APPEND_LIST = (1 << 6), HLS_PROGRAM_DATE_TIME = (1 << 7), + HLS_SECOND_LEVEL_SEGMENT_INDEX = (1 << 8), // include segment index in segment filenames when use_localtime e.g.: %%03d } HLSFlags;