From patchwork Fri Mar 18 12:20:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Liu X-Patchwork-Id: 34837 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6838:3486:0:0:0:0 with SMTP id ek6csp1725009nkb; Fri, 18 Mar 2022 05:21:13 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzZPgYSdJRIl326xkNNj1yLTPMBzPqlanBu6HJ53LAHEtBNy1c1YheBqgZM+gHvJua9vdKm X-Received: by 2002:a17:906:60d4:b0:6db:f0a8:f39e with SMTP id f20-20020a17090660d400b006dbf0a8f39emr8835145ejk.54.1647606072834; Fri, 18 Mar 2022 05:21:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647606072; cv=none; d=google.com; s=arc-20160816; b=AuMNUqiVDMLhNjvwRBHouXe44AU/GxD5XvQwR+kh2q+QCVQtBBqW/xkpKhOWKxnN8f O+AOqlAqQ5ciiYWRLF846PLeoOHCFUsFQh2N7petzBeIMLTNUBRrm/qr2I0n8PmddJFp RpH2Wr8ewWmFeRxBSASq3yZzFRrIpLwfvBv5WttpAfAwtF7bP7oSCMdBX+sf7+vBXU/W EJ6jIt8SHNkLNzwsG5lDcaSVNwHvSf7pmOxzxUl2KyX8ncZtXfpFwItICk89m4ouvyYs fkQy3Vajx10CWCxmjTmD4P2V+2COIY2aaCB6MQjFHtDfuwJxg7OK5uqG/UIvNqT8NryA 8S7w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:feedback-id:mime-version:message-id:date :to:from:delivered-to; bh=65dS4jq1VODAq9g/bSTWeI3YG9gXo/h6mNgnLg5y+vQ=; b=lwIYHin86BJUYH5J06A/yDxMrILpNuDloZqUFaRtsBDpg/+l9zueiz/+KdRvypaJ1Q DeJx6EZzTLJsdiGom5kdkWge72wstJcWP4XZBJZS9oCHH3Jra2cm+JcX1D2gSRq1KCJo y0+P6AaWPCqCBEZCRYCJYlVxWsSheMm3Q5mJOgWWIITWkdlEWUVa0tRKZekVo7E5ruj1 DVMnW3nUY/9Vk1EXSz5sc3l6XtNgn7tNKHMzs1JZCKOTFV+bHyBYmZhPdUpl6CNHJEzC GEjdbBH1oGJm4Jn81ho425vEEcIzw5ywe/NN+2Lp09eHDPd6TlpPOeqACrzFkm9D6vvu eVCA== ARC-Authentication-Results: i=1; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id f9-20020a50d549000000b00418e7ab3ed2si3496606edj.500.2022.03.18.05.21.11; Fri, 18 Mar 2022 05:21:12 -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; 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 A23D468B08F; Fri, 18 Mar 2022 14:21:07 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from smtpbg.qq.com (smtpbg138.qq.com [106.55.201.18]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id A798B68A2FB for ; Fri, 18 Mar 2022 14:20:59 +0200 (EET) X-QQ-mid: bizesmtp63t1647606054t7kd1yui Received: from localhost ( [103.107.217.225]) by bizesmtp.qq.com (ESMTP) with id ; Fri, 18 Mar 2022 20:20:53 +0800 (CST) X-QQ-SSF: 01100000002000Z0Z000000A0000000 X-QQ-FEAT: HDjpALELSmGJndvKdHS21B9A49goSRabTlPsoZhbMF5icjDmIcf5MFCMnK3hB AQvh3HMIW+N4wyloKtuh4ehz2w7lciUs9ELZoT6CrIAk7KkjEdNt7haETY3L4PT0TO0g1T1 qzcX+AK1PSJH0FKxB533RD5An49X0Mz641hlGNnntuglbIUKmTlCIMZkZzqLjA+Nq1bUJWk a2ZC8dVN1XTtSBRxllxIryPHACCm6J5pcJ1Gs6s+LrC/PFCMIz0ZXKvicpuXSPhNAIrL3KN t3/gT25tIk6/SgtTDId7YBu1Dxbg4AwVNX0OjuO89Lge3EvfSVVt+5GfY1rAnxu3l/8fdV4 zPFuIAk X-QQ-GoodBg: 0 From: Steven Liu To: ffmpeg-devel@ffmpeg.org Date: Fri, 18 Mar 2022 20:20:50 +0800 Message-Id: <20220318122050.10271-1-lq@chinaffmpeg.org> X-Mailer: git-send-email 2.25.0 MIME-Version: 1.0 X-QQ-SENDSIZE: 520 Feedback-ID: bizesmtp:chinaffmpeg.org:qybgspam:qybgspam10 Subject: [FFmpeg-devel] [PATCH] avformat/hlsenc: add hls_time_delta to cut segment at before hls_time 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: Steven Liu Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: UZdYxn9XxgME From: Steven Liu There have some video stream usually cannot get precise duration, for example: command: ffmpeg -i input.mp4 -c copy -copyts -f hls -hls_time 5 -hls_list_size 0 -hls_segment_type mpegts -t 20 a.m3u8 the first segment should split at 5.133333, because 5.133333 - 0.133333 is 5.0000, pkt->pts = [5.133333] vs->end_pts = 0.133333, flags = [1] but the second segment will cannot split at 10.133322, because 10.133322 - 5.133333 is 4.999989, it small than hls_time, pkt->pts = [10.133322] vs->end_pts = 5.133333, flags = [1] so add hls_time_delta for approximation value to hls_time. Signed-off-by: Steven Liu --- doc/muxers.texi | 8 ++++++++ libavformat/hlsenc.c | 4 +++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/doc/muxers.texi b/doc/muxers.texi index 1af603b7f6..c91a7a9216 100644 --- a/doc/muxers.texi +++ b/doc/muxers.texi @@ -778,6 +778,14 @@ Segment will be cut on the next key frame after this time has passed on the firs After the initial playlist is filled @command{ffmpeg} will cut segments at duration equal to @code{hls_time} +@item hls_time_delta @var{duration} +Set approximation value used for the segment duration. Default value is @var{0}. + +@var{duration} must be a time duration specification, +see @ref{time duration syntax,,the Time duration section in the ffmpeg-utils(1) manual,ffmpeg-utils}. + +Segment will be cut on the key frame before @code{hls_time_delta} to @code{hls_time}. + @item hls_time @var{duration} Set the target segment length. Default value is 2. diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c index 14eb7c4530..56cde7dd70 100644 --- a/libavformat/hlsenc.c +++ b/libavformat/hlsenc.c @@ -200,6 +200,7 @@ typedef struct HLSContext { int64_t time; // Set by a private option. int64_t init_time; // Set by a private option. + int64_t time_delta; int max_nb_segments; // Set by a private option. int hls_delete_threshold; // Set by a private option. uint32_t flags; // enum HLSFlags @@ -2489,7 +2490,7 @@ static int hls_write_packet(AVFormatContext *s, AVPacket *pkt) can_split = can_split && (pkt->pts - vs->end_pts > 0); if (vs->packets_written && can_split && av_compare_ts(pkt->pts - vs->start_pts, st->time_base, - end_pts, AV_TIME_BASE_Q) >= 0) { + end_pts - hls->time_delta, AV_TIME_BASE_Q) >= 0) { int64_t new_start_pos; int byterange_mode = (hls->flags & HLS_SINGLE_FILE) || (hls->max_seg_size > 0); @@ -3101,6 +3102,7 @@ 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}, {"hls_time", "set segment length", OFFSET(time), AV_OPT_TYPE_DURATION, {.i64 = 2000000}, 0, INT64_MAX, E}, {"hls_init_time", "set segment length at init list", OFFSET(init_time), AV_OPT_TYPE_DURATION, {.i64 = 0}, 0, INT64_MAX, E}, + {"hls_time_delta", "set approximation value used for the segment duration", OFFSET(time_delta), AV_OPT_TYPE_DURATION, {.i64 = 0}, 0, INT64_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}, {"hls_delete_threshold", "set number of unreferenced segments to keep before deleting", OFFSET(hls_delete_threshold), AV_OPT_TYPE_INT, {.i64 = 1}, 1, INT_MAX, E}, #if FF_HLS_TS_OPTIONS