From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTPS id B322C4A12F for ; Wed, 29 Jan 2025 09:54:50 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 7C0F868B5CE; Wed, 29 Jan 2025 11:54:48 +0200 (EET) Received: from mail-wm1-f52.google.com (mail-wm1-f52.google.com [209.85.128.52]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 80ED168A4FA for ; Wed, 29 Jan 2025 11:54:42 +0200 (EET) Received: by mail-wm1-f52.google.com with SMTP id 5b1f17b1804b1-436a03197b2so44852975e9.2 for ; Wed, 29 Jan 2025 01:54:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=datarhei.com; s=google; t=1738144481; x=1738749281; darn=ffmpeg.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=Epcm3m0Is0iAnm79xaoTca4M2z9ddtVzY+FxEpIuqQQ=; b=EObqPGxhm1qhMv5EytY7tU9pJeUVWlF0OKpO2By60FnBUi1y16OT5eZnQu6qeowFgB vFJubJeOpD3iZKjZEJaP5U68SCSJtzew6hUYtIX3cAK9S9ZCRIuYUXBomG8kHFffwwxQ TUjtndlH/1tGHD2ipjnIV32QURccfuO0/zGWwMCd7NjCu9UPigSiz1JYjW31KR2pOVyo I59/KBNw2umMT7vOfUknn09rjQtr4h+Pb/xCtC2M6KPPnMTR97tCy2OxKvwREbsICz+1 NAcCjeGJ59awCL7Lylh/vDsmu2jPnwTFomeo+FuSeEz4veCATCF56NpE7hA6QqO2h6eF S34g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738144481; x=1738749281; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Epcm3m0Is0iAnm79xaoTca4M2z9ddtVzY+FxEpIuqQQ=; b=hhlOsChju38iiMO1z7odS4FLTVX71nEny99zrbJ0yyCq5pqkPhmjsWq2q7J+5BK8L5 MyKd2HfqHJxVcqxhC5Ew6r27tzEc/PA2SrcuwfA1IVaYb5mfgLWc/a0wr779kHcFcq+W aKdAIqb8dY5xSnU0f8oaSpvDkf8Ve8XXRLVifTLSrg1jrCyviDsVgOuWqAm8ozw6t2Cg AY9XjZe4gL/yRa8a/rqgR0iBAXDiMQk+cgYPTKVWtN3Ar9QZJ4+MDiaeCseRWLWxoxi4 kL8pQnR/kD4SzDG2xPAaLp0Ztb8clpvlNh2ja/S2w9/tAHy/EGdzPtL4rEa/+cHmW6uf E7Sw== X-Gm-Message-State: AOJu0YwvhnmAyK4xUyN4hxCTEJwTffNFcaCUjqJ8A2fL3ZaCYMXrG0K6 50iKQVFn0OzvO/e5s9i8Ukkm3GK2EmVHgc/7Hn/nckOWW4IIwlfXqJy6swUL/82VQBxuDKtwRHA = X-Gm-Gg: ASbGnctNs5lW+nReuTKCI/Q0ic1uxKSu5OrZWkrdYmj3SkTf/iW7a7qz3V32wuvEh6D LNlUFUG7LzHW7yKFrhkX2ASotGI4QLJUCfOxkGaC12PHTgu6+9Da7Eaj59c0Op2VTjcSXf9CRXS wWiv0nD0Kev0OsSTDrE1AKMEFMuQfp5VQAHvoWmqWucawJjo94/2RxwnySrx61oz6oRIbN6u0ho F3fc9TnQjZgwtpQq5lfNDzDiY4VuqS4d47Y4Oucbayxna7jAbey9Oprg7lREseJUuTJtLEX0caQ DA+Izp+Y0ncM+lP6C0yZUd87VLqqLN+VFBi7VGCt X-Google-Smtp-Source: AGHT+IHTm6xLRpVt4u/dD19VTuHDQpRfff6pebbNZHNO+ok6RyBuFbQ/xs7Q2MHVKVo6ydu7wXSSYw== X-Received: by 2002:a05:600c:3ca1:b0:434:fafe:edb with SMTP id 5b1f17b1804b1-438dc41002cmr18052755e9.24.1738144480314; Wed, 29 Jan 2025 01:54:40 -0800 (PST) Received: from localhost.localdomain ([2a04:ee41:4:b3b0:dc5a:a860:8bca:819]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-438dcc33bb8sm16526035e9.28.2025.01.29.01.54.39 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 29 Jan 2025 01:54:39 -0800 (PST) From: Ingo Oppermann To: ffmpeg-devel@ffmpeg.org Date: Wed, 29 Jan 2025 10:54:36 +0100 Message-Id: <20250129095436.9059-1-ingo@datarhei.com> X-Mailer: git-send-email 2.39.5 (Apple Git-154) MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v1] avformat/hlsenc: add hls_min_time to enforce minimal segment 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 Cc: Ingo Oppermann Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: The current implementation of the hls_time might produce segments with shorter duration in some cases. With the hls_min_time option a minimal segment duration will be enforced. Instead of changing the current behaviour of hls_time which might break existing workflows, the new option hls_min_time will override hls_time and enforce a minimal segment duration. hls_time is supposed to define the minimum length of a segment, however this is not respected in all cases when a stream has variable GOP sizes. Imagine a stream starts with a key frame every 10 seconds for e.g. 30 seconds. After that, key frames will come every second. This will result in segments that are first 10 seconds (as expected), then 1 second for some time (not as expected) and later 2 seconds (as expected). How to reproduce: ffmpeg -t 30 -f lavfi -i testsrc2 -codec:v libx264 -g 250 part1.mp4 ffmpeg -t 30 -f lavfi -i testsrc2 -codec:v libx264 -g 25 part2.mp4 echo "file part1.mp4\nfile part2.mp4" > list.txt ffmpeg -f concat -i list.txt -codec copy \ -f hls -hls_time 2 -hls_list_size 0 parts.m3u8 cat parts.m3u8 Signed-off-by: Ingo Oppermann --- doc/muxers.texi | 7 +++++++ libavformat/hlsenc.c | 27 ++++++++++++++++++++------- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/doc/muxers.texi b/doc/muxers.texi index 04b7f20b7e..e058a8f8c0 100644 --- a/doc/muxers.texi +++ b/doc/muxers.texi @@ -1929,6 +1929,13 @@ Set the target segment length. Default value is 2. see @ref{time duration syntax,,the Time duration section in the ffmpeg-utils(1) manual,ffmpeg-utils}. Segment will be cut on the next key frame after this time has passed. +@item hls_min_time @var{duration} +Set the minimum target segment length. Default value is 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 at least this long and will be cut at the following key frame. If set, it will override @option{hls_time}. + @item hls_list_size @var{size} Set the maximum number of playlist entries. If set to 0 the list file will contain all the segments. Default value is 5. diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c index 6148685f40..5fc215fdce 100644 --- a/libavformat/hlsenc.c +++ b/libavformat/hlsenc.c @@ -204,6 +204,7 @@ typedef struct HLSContext { uint32_t start_sequence_source_type; // enum StartSequenceSourceType int64_t time; // Set by a private option. + int64_t min_time; // Set by a private option. int64_t init_time; // Set by a private option. int max_nb_segments; // Set by a private option. int hls_delete_threshold; // Set by a private option. @@ -2474,7 +2475,7 @@ static int hls_write_packet(AVFormatContext *s, AVPacket *pkt) HLSContext *hls = s->priv_data; AVFormatContext *oc = NULL; AVStream *st = s->streams[pkt->stream_index]; - int64_t end_pts = 0; + int64_t end_pts = 0, current_pts; int is_ref_pkt = 1; int ret = 0, can_split = 1, i, j; int stream_index = 0; @@ -2515,11 +2516,16 @@ static int hls_write_packet(AVFormatContext *s, AVPacket *pkt) end_pts = hls->recording_time * vs->number; if (vs->sequence - vs->nb_entries > hls->start_sequence && hls->init_time > 0) { - /* reset end_pts, hls->recording_time at end of the init hls list */ - int64_t init_list_dur = hls->init_time * vs->nb_entries; - int64_t after_init_list_dur = (vs->sequence - hls->start_sequence - vs->nb_entries) * hls->time; - hls->recording_time = hls->time; - end_pts = init_list_dur + after_init_list_dur ; + if (hls->min_time > 0) { + hls->recording_time = hls->min_time; + hls->init_time = 0; + } else { + /* reset end_pts, hls->recording_time at end of the init hls list */ + int64_t init_list_dur = hls->init_time * vs->nb_entries; + int64_t after_init_list_dur = (vs->sequence - hls->start_sequence - vs->nb_entries) * hls->time; + hls->recording_time = hls->time; + end_pts = init_list_dur + after_init_list_dur; + } } if (vs->start_pts == AV_NOPTS_VALUE) { @@ -2559,8 +2565,14 @@ static int hls_write_packet(AVFormatContext *s, AVPacket *pkt) } } + current_pts = pkt->pts - vs->start_pts; + if (hls->min_time > 0 && hls->init_time == 0) { + current_pts = pkt->pts - vs->end_pts; + end_pts = hls->min_time; + } + 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, + if (vs->packets_written && can_split && av_compare_ts(current_pts, st->time_base, end_pts, AV_TIME_BASE_Q) >= 0) { int64_t new_start_pos; int byterange_mode = (hls->flags & HLS_SINGLE_FILE) || (hls->max_seg_size > 0); @@ -3182,6 +3194,7 @@ static int hls_init(AVFormatContext *s) 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_min_time", "set minimum segment length", OFFSET(min_time), AV_OPT_TYPE_DURATION, {.i64 = 0}, 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_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}, base-commit: 3380c0d27df1e07f009e114da98fb8033c2ce33d -- 2.39.5 (Apple Git-154) _______________________________________________ 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".