From patchwork Wed Dec 21 15:38:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gyan Doshi X-Patchwork-Id: 39741 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:bc95:b0:ad:ade2:bfd2 with SMTP id fx21csp5936708pzb; Wed, 21 Dec 2022 07:39:42 -0800 (PST) X-Google-Smtp-Source: AMrXdXtUYDoJ9CzDUpmBKkKbzLCLY8zoXmGZqiwmwJ/7jOQvDK052HVYjnRUyBjLoYv88OrPP9c4 X-Received: by 2002:a17:906:4cd2:b0:7c0:e5c6:2a74 with SMTP id q18-20020a1709064cd200b007c0e5c62a74mr1635598ejt.34.1671637182063; Wed, 21 Dec 2022 07:39:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671637182; cv=none; d=google.com; s=arc-20160816; b=WrWo5UJOaKbEhZGraYpFy5hrUZXCG29fPH5gAxVCfbMThHMhiIfv0lXherVTvqU8RJ juaZyweikofTZRt5NeqzolglwnyYazhKZjId7Qg7fGotldUdVh3PZh4qlHbBwHo/6xL7 neYleIKJVkvoDTFEOXCvPXkaLj0EMdgF9X4Nrjcer80nIX+qhe9btRRI4YgkDatPWREi I4FcsohKQ+eWwW5i7J4SqXkyhnmtQ3wk4kAnIeuWf4y5HyjM4PXhsJ6GQpKrPJj+zJHN MZvQXjivtXuO3rNHS+RxthI+KZZebv606J0yhm46yRHj9WCPcZlCTqPtB6zgI4pyUzWj ECBQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:message-id:date:to:from :delivered-to; bh=6leScP8TnxgHPOXKUyuBIjrqUtD2HN8KSXGniAEje+Q=; b=nuPZYnFdOhYHoOeET+T8hK5kS3vUhLeIV6nVRMzs6IeiEI6ijnQXd2DEubBbehx1kF XaGq5r7i7d1JmKJgWXnxM8tQnWtzMVXhVhfoAvY1oIXXf7u7RvACYmtwuzOIuNkw6RHX AwCxH01BUtg5tTiGstAsoqfcfUMfqz0SYecG7M7yDo22a2SsD3Lro0jtjf81CY086rjp iCQtMeCl6rcoiSwarZFE9HDBLvC9Pt7nDk16oIdqtsHFWIRebtB5kY1+3GwsY7X8xtX+ j78UtwO4I2X+NU3R37ALrzsclXDcIRu7GLlkgjYl20heqbCse9oApYMor+6kKLL55s0W cyVw== 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 qa10-20020a170907868a00b0082bed0f9405si6567775ejc.509.2022.12.21.07.39.41; Wed, 21 Dec 2022 07:39:42 -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 6A8656802C4; Wed, 21 Dec 2022 17:39:37 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mout-p-202.mailbox.org (mout-p-202.mailbox.org [80.241.56.172]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 8AF4A68981B for ; Wed, 21 Dec 2022 17:39:29 +0200 (EET) Received: from smtp102.mailbox.org (smtp102.mailbox.org [10.196.197.102]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-202.mailbox.org (Postfix) with ESMTPS id 4Ncd0b1JGTz9slR for ; Wed, 21 Dec 2022 16:39:27 +0100 (CET) From: Gyan Doshi To: ffmpeg-devel@ffmpeg.org Date: Wed, 21 Dec 2022 21:08:57 +0530 Message-Id: <20221221153857.2515-1-ffmpeg@gyani.pro> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] avformat/segment: add option min_seg_duration 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: OfXPhWxk9fAp New option can be used to avoid creating very short segments with inputs whose GOP size is variable or unharmonic with segment_time. Only effective with segment_time. --- doc/muxers.texi | 5 +++++ libavformat/segment.c | 12 +++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/doc/muxers.texi b/doc/muxers.texi index 4edbb22b00..ed5341be39 100644 --- a/doc/muxers.texi +++ b/doc/muxers.texi @@ -2369,6 +2369,11 @@ Note that splitting may not be accurate, unless you force the reference stream key-frames at the given time. See the introductory notice and the examples below. +@item min_seg_duration @var{time} +Set minimum segment duration to @var{time}, the value must be a duration +specification. This prevents the muxer ending segments at a duration below +this value. Only effective with @code{segment_time}. Default value is "0". + @item segment_atclocktime @var{1|0} If set to "1" split at regular clock time intervals starting from 00:00 o'clock. The @var{time} value specified in @option{segment_time} is diff --git a/libavformat/segment.c b/libavformat/segment.c index c904e20708..c19c2a94ae 100644 --- a/libavformat/segment.c +++ b/libavformat/segment.c @@ -93,6 +93,7 @@ typedef struct SegmentContext { int list_type; ///< set the list type AVIOContext *list_pb; ///< list file put-byte context int64_t time; ///< segment duration + int64_t min_seg_duration; ///< minimum segment duration int use_strftime; ///< flag to expand filename with strftime int increment_tc; ///< flag to increment timecode if found @@ -710,6 +711,10 @@ static int seg_init(AVFormatContext *s) } seg->clocktime_offset = seg->time - (seg->clocktime_offset % seg->time); } + if (seg->min_seg_duration > seg->time) { + av_log(s, AV_LOG_ERROR, "min_seg_duration cannot be greater than segment_time\n"); + return AVERROR(EINVAL); + } } if (seg->list) { @@ -839,7 +844,7 @@ static int seg_write_packet(AVFormatContext *s, AVPacket *pkt) { SegmentContext *seg = s->priv_data; AVStream *st = s->streams[pkt->stream_index]; - int64_t end_pts = INT64_MAX, offset; + int64_t end_pts = INT64_MAX, offset, pkt_pts_avtb; int start_frame = INT_MAX; int ret; struct tm ti; @@ -890,11 +895,15 @@ calc_times: pkt->flags & AV_PKT_FLAG_KEY, pkt->stream_index == seg->reference_stream_index ? seg->frame_count : -1); + if (pkt->pts != AV_NOPTS_VALUE) + pkt_pts_avtb = av_rescale_q(pkt->pts, st->time_base, AV_TIME_BASE_Q); + if (pkt->stream_index == seg->reference_stream_index && (pkt->flags & AV_PKT_FLAG_KEY || seg->break_non_keyframes) && (seg->segment_frame_count > 0 || seg->write_empty) && (seg->cut_pending || seg->frame_count >= start_frame || (pkt->pts != AV_NOPTS_VALUE && + pkt_pts_avtb - seg->cur_entry.start_pts >= seg->min_seg_duration && av_compare_ts(pkt->pts, st->time_base, end_pts - seg->time_delta, AV_TIME_BASE_Q) >= 0))) { /* sanitize end time in case last packet didn't have a defined duration */ @@ -1031,6 +1040,7 @@ static const AVOption options[] = { { "segment_clocktime_wrap_duration", "set segment clocktime wrapping duration", OFFSET(clocktime_wrap_duration), AV_OPT_TYPE_DURATION, {.i64 = INT64_MAX}, 0, INT64_MAX, E}, { "segment_time", "set segment duration", OFFSET(time),AV_OPT_TYPE_DURATION, {.i64 = 2000000}, INT64_MIN, INT64_MAX, E }, { "segment_time_delta","set approximation value used for the segment times", OFFSET(time_delta), AV_OPT_TYPE_DURATION, {.i64 = 0}, 0, INT64_MAX, E }, + { "min_seg_duration", "set minimum segment duration", OFFSET(min_seg_duration), AV_OPT_TYPE_DURATION, {.i64 = 0}, 0, INT64_MAX, E }, { "segment_times", "set segment split time points", OFFSET(times_str),AV_OPT_TYPE_STRING,{.str = NULL}, 0, 0, E }, { "segment_frames", "set segment split frame numbers", OFFSET(frames_str),AV_OPT_TYPE_STRING,{.str = NULL}, 0, 0, E }, { "segment_wrap", "set number after which the index wraps", OFFSET(segment_idx_wrap), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },