Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: Gyan Doshi <ffmpeg@gyani.pro>
To: ffmpeg-devel@ffmpeg.org
Subject: [FFmpeg-devel] [PATCH] avformat/segment: add option min_seg_duration
Date: Wed, 21 Dec 2022 21:08:57 +0530
Message-ID: <20221221153857.2515-1-ffmpeg@gyani.pro> (raw)

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 },
-- 
2.36.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

             reply	other threads:[~2022-12-21 15:39 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-21 15:38 Gyan Doshi [this message]
2022-12-25 18:24 ` Gyan Doshi
2022-12-28  3:56   ` Gyan Doshi
2022-12-29 10:16     ` Gyan Doshi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221221153857.2515-1-ffmpeg@gyani.pro \
    --to=ffmpeg@gyani.pro \
    --cc=ffmpeg-devel@ffmpeg.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git