From patchwork Fri Jan 6 14:07:24 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bodecs Bela X-Patchwork-Id: 2075 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.89.21 with SMTP id n21csp5848936vsb; Fri, 6 Jan 2017 06:07:34 -0800 (PST) X-Received: by 10.28.107.21 with SMTP id g21mr3867822wmc.131.1483711654559; Fri, 06 Jan 2017 06:07:34 -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 rv12si89354089wjb.256.2017.01.06.06.07.34; Fri, 06 Jan 2017 06:07: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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 1DD1668A270; Fri, 6 Jan 2017 16:07:26 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail.vivacom.hu (mail.vivacom.hu [217.173.41.231]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 2907D68A0BB for ; Fri, 6 Jan 2017 16:07:19 +0200 (EET) Received: from localhost (localhost [127.0.0.1]) by mail.vivacom.hu (Postfix) with ESMTP id A10D18686D for ; Fri, 6 Jan 2017 15:07:24 +0100 (CET) X-Virus-Scanned: amavisd-new at example.com Received: from mail.vivacom.hu ([127.0.0.1]) by localhost (mail.vivacom.intra [127.0.0.1]) (amavisd-new, port 10024) with LMTP id ZGjl6SZLcHNk for ; Fri, 6 Jan 2017 15:07:22 +0100 (CET) Received: from [10.0.0.10] (mail.officeline.hu [217.173.32.91]) by mail.vivacom.hu (Postfix) with ESMTPA id 7E43288241 for ; Fri, 6 Jan 2017 15:07:22 +0100 (CET) To: FFmpeg development discussions and patches From: Bodecs Bela Message-ID: Date: Fri, 6 Jan 2017 15:07:24 +0100 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.6.0 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] avformat/hlsenc: start_number new options 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" Dear All, in avformat/hlsenc the start_number option starts the playlist sequence number (#EXT-X-MEDIA-SEQUENCE) from the specified number. Unless hls_flags single_file is set, it also specifies starting sequence numbers of segment and subtitle filenames. Sometimes it is usefull to have unique starting numbers at each run, but currently it is only achiveable by setting this parameter manually. This patch enables to set start_number parameter automatically for practically unique numbers. If start_number is set to -1, then the start number will be the seconds since epoch (1970-01-01 00:00:00). If set to -2, then the start number will be based on the current date/time value as YYYYmmddHHMMSS. e.g. 20161231235659. thank you, Bela Bodecs From 3901ca87d9918e9002681b55a6984d3d0d0eaea5 Mon Sep 17 00:00:00 2001 From: Bela Bodecs Date: Fri, 6 Jan 2017 14:52:08 +0100 Subject: [PATCH] avformat/hlsenc: start_number new options start_number option starts the playlist sequence number (#EXT-X-MEDIA-SEQUENCE) from the specified number. Unless hls_flags single_file is set, it also specifies starting sequence numbers of segment and subtitle filenames. Sometimes it is usefull to have unique starting numbers at each run, but currently it is only achiveable by setting this parameter manually. This patch enables to set start_number parameter automatically for practically unique numbers. If start_number is set to -1, then the start number will be the seconds since epoch (1970-01-01 00:00:00). If set to -2, then the start number will be based on the current date/time value as YYYYmmddHHMMSS. e.g. 20161231235659. Signed-off-by: Bela Bodecs --- doc/muxers.texi | 7 +++++-- libavformat/hlsenc.c | 22 ++++++++++++++++++++-- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/doc/muxers.texi b/doc/muxers.texi index 351cd8c..ae877c9 100644 --- a/doc/muxers.texi +++ b/doc/muxers.texi @@ -417,8 +417,11 @@ files, and limits the maximum number of segment files written to disk to @var{wrap}. @item start_number @var{number} -Start the playlist sequence number from @var{number}. Default value is -0. +Start the playlist sequence number (@code{#EXT-X-MEDIA-SEQUENCE}) from the specified @var{number}. +Unless @code{hls_flags single_file} is set, it also specifies starting sequence numbers of segment and subtitle filenames. +Default value is 0. +If set to -1, then the start number will be the seconds since epoch (1970-01-01 00:00:00). +If set to -2, then the start number will be based on the current date/time as YYYYmmddHHMMSS. e.g. 20161231235659. @item hls_allow_cache @var{allowcache} Explicitly set whether the client MAY (1) or MUST NOT (0) cache media segments. diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c index eeb450a..de37be5 100644 --- a/libavformat/hlsenc.c +++ b/libavformat/hlsenc.c @@ -39,6 +39,9 @@ #include "internal.h" #include "os_support.h" +#define HLS_START_SEQUNCE_AS_SECONDS_SINCE_EPOCH -1 +#define HLS_START_SEQUNCE_AS_FORMATTED_STRFTIME -2 + #define KEYSIZE 16 #define LINE_BUFFER_SIZE 1024 @@ -586,7 +589,7 @@ static int parse_playlist(AVFormatContext *s, const char *url) while (!avio_feof(in)) { read_chomp_line(in, line, sizeof(line)); if (av_strstart(line, "#EXT-X-MEDIA-SEQUENCE:", &ptr)) { - hls->sequence = atoi(ptr); + hls->sequence = strtoll(ptr, NULL, 10); } else if (av_strstart(line, "#EXT-X-DISCONTINUITY", &ptr)) { is_segment = 1; hls->discontinuity = 1; @@ -971,6 +974,21 @@ static int hls_write_header(AVFormatContext *s) int basename_size; int vtt_basename_size; + if (hls->start_sequence == HLS_START_SEQUNCE_AS_SECONDS_SINCE_EPOCH || hls->start_sequence == HLS_START_SEQUNCE_AS_FORMATTED_STRFTIME) { + time_t t = time(NULL); // we will need it in either case + if (hls->start_sequence == HLS_START_SEQUNCE_AS_SECONDS_SINCE_EPOCH) + hls->start_sequence = (int64_t)t; + else if (hls->start_sequence == HLS_START_SEQUNCE_AS_FORMATTED_STRFTIME) { + char b[21]; + struct tm *p, tmbuf; + if (!(p = localtime_r(&t, &tmbuf))) + return AVERROR(ENOMEM); + if (!strftime(b, sizeof(b), "%Y%m%d%H%M%S", p)) + return AVERROR(ENOMEM); + hls->start_sequence = strtoll(b, NULL, 10); + } + } + hls->sequence = hls->start_sequence; hls->recording_time = (hls->init_time ? hls->init_time : hls->time) * AV_TIME_BASE; hls->start_pts = AV_NOPTS_VALUE; @@ -1318,7 +1336,7 @@ static int hls_write_trailer(struct AVFormatContext *s) #define OFFSET(x) offsetof(HLSContext, x) #define E AV_OPT_FLAG_ENCODING_PARAM static const AVOption options[] = { - {"start_number", "set first number in the sequence", OFFSET(start_sequence),AV_OPT_TYPE_INT64, {.i64 = 0}, 0, INT64_MAX, E}, + {"start_number", "set first number in the sequence", OFFSET(start_sequence),AV_OPT_TYPE_INT64, {.i64 = 0}, -2, INT64_MAX, E}, {"hls_time", "set segment length in seconds", OFFSET(time), AV_OPT_TYPE_FLOAT, {.dbl = 2}, 0, FLT_MAX, E}, {"hls_init_time", "set segment length in seconds at init list", OFFSET(init_time), AV_OPT_TYPE_FLOAT, {.dbl = 0}, 0, FLT_MAX, E}, {"hls_list_size", "set maximum number of playlist entries", OFFSET(max_nb_segments), AV_OPT_TYPE_INT, {.i64 = 5}, 0, INT_MAX, E}, -- 2.5.3.windows.1