[FFmpeg-devel] [PATCH] avformat/hlsenc: add hls_time_delta to cut segment at before hls_time

Steven Liu lq at chinaffmpeg.org
Fri Mar 18 14:20:50 EET 2022


From: Steven Liu <liuqi05 at kuaishou.com>

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 <liuqi05 at kuaishou.com>
---
 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}
 
+ at item hls_time_delta @var{duration}
+Set approximation value used for the segment duration. Default value is @var{0}.
+
+ at 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
-- 
2.25.0



More information about the ffmpeg-devel mailing list