* [FFmpeg-devel] [PATCH] avformat/hlsenc: add hls_time_delta to cut segment at before hls_time
@ 2022-03-18 12:20 Steven Liu
2022-03-21 3:55 ` "zhilizhao(赵志立)"
0 siblings, 1 reply; 3+ messages in thread
From: Steven Liu @ 2022-03-18 12:20 UTC (permalink / raw)
To: ffmpeg-devel; +Cc: Steven Liu
From: Steven Liu <liuqi05@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@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}
+@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
--
2.25.0
_______________________________________________
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".
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [FFmpeg-devel] [PATCH] avformat/hlsenc: add hls_time_delta to cut segment at before hls_time
2022-03-18 12:20 [FFmpeg-devel] [PATCH] avformat/hlsenc: add hls_time_delta to cut segment at before hls_time Steven Liu
@ 2022-03-21 3:55 ` "zhilizhao(赵志立)"
2022-03-21 6:40 ` Steven Liu
0 siblings, 1 reply; 3+ messages in thread
From: "zhilizhao(赵志立)" @ 2022-03-21 3:55 UTC (permalink / raw)
To: FFmpeg development discussions and patches
> On Mar 18, 2022, at 8:20 PM, Steven Liu <lq@chinaffmpeg.org> wrote:
>
> From: Steven Liu <liuqi05@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,
It doesn’t looks like an issue.
Firstly, user can specify a smaller ‘hls_time'. '-hls_time 4.9'
is the same as '-hls_time 5 -hls_time_delta 0.1', right?
Secondly, if user want to have precise segment durations, they
need to control the timestamp of input packets, with fixed FPS.
> 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@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}
>
> +@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
> --
> 2.25.0
>
> _______________________________________________
> 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".
_______________________________________________
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".
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [FFmpeg-devel] [PATCH] avformat/hlsenc: add hls_time_delta to cut segment at before hls_time
2022-03-21 3:55 ` "zhilizhao(赵志立)"
@ 2022-03-21 6:40 ` Steven Liu
0 siblings, 0 replies; 3+ messages in thread
From: Steven Liu @ 2022-03-21 6:40 UTC (permalink / raw)
To: FFmpeg development discussions and patches
"zhilizhao(赵志立)" <quinkblack@foxmail.com> 于2022年3月21日周一 11:55写道:
>
>
>
> > On Mar 18, 2022, at 8:20 PM, Steven Liu <lq@chinaffmpeg.org> wrote:
> >
> > From: Steven Liu <liuqi05@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,
>
> It doesn’t looks like an issue.
>
> Firstly, user can specify a smaller ‘hls_time'. '-hls_time 4.9'
> is the same as '-hls_time 5 -hls_time_delta 0.1', right?
4.9 is set by user, and 5.0 is set by user to, but user can set a
time_delta to control the approximation.
the file number will get more then 5.0 if they set 4.9 than 5.0
eg.
50/4.9
10
50%4.9
1.0
50/5.0
10
50%5.0
0
>
> Secondly, if user want to have precise segment durations, they
> need to control the timestamp of input packets, with fixed FPS.
That's not fixed FPS, just input files precise problem from one format
to the other format:
----test case----
(base) liuqi05:ufbuild liuqi$ ffmpeg -v quiet -i input.mp4 -c:v copy
-an -y out.ts
ffmpeg(47154,0x1090e7600) malloc: nano zone abandoned due to inability
to preallocate reserved vm space.
(base) liuqi05:ufbuild liuqi$ ffprobe -hide_banner -select_streams v
-show_packets -show_entries packet=pts,dts,flags -of xml out.ts |grep
"flags=\"K" | head -n 5
ffprobe(47254,0x10f8e3600) malloc: nano zone abandoned due to
inability to preallocate reserved vm space.
Input #0, mpegts, from 'out.ts':
Duration: 00:04:00.70, start: 1.533333, bitrate: 1595 kb/s
Program 1
Metadata:
service_name : Service01
service_provider: FFmpeg
Stream #0:0[0x100]: Video: hevc (Main) (HEVC / 0x43564548),
yuvj420p(pc, bt470bg/unknown/unknown), 1600x670 [SAR 1:1 DAR 160:67],
30 fps, 30 tbr, 90k tbn, 30 tbc
<packet pts="138000" dts="126000" flags="K_">
<packet pts="588000" dts="576000" flags="K_">
<packet pts="1037999" dts="1025999" flags="K_">
<packet pts="1487999" dts="1475999" flags="K_">
<packet pts="1496999" dts="1484999" flags="K_">
(base) liuqi05:ufbuild liuqi$ ffprobe -hide_banner -select_streams v
-show_packets -show_entries packet=pts,dts,flags -of xml input.mp4
|grep "flags=\"K" | head -n 5
ffprobe(47477,0x1149d1600) malloc: nano zone abandoned due to
inability to preallocate reserved vm space.
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'input.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2mp41
encoder : Lavf58.29.100
Duration: 00:04:00.70, start: 0.000000, bitrate: 1504 kb/s
Stream #0:0(und): Video: hevc (Main) (hvc1 / 0x31637668),
yuvj420p(pc, bt470bg/unknown/unknown, progressive), 1600x670 [SAR 1:1
DAR 160:67], 1500 kb/s, 30 fps, 30 tbr, 1000k tbn, 30 tbc (default)
Metadata:
handler_name : VideoHandler
vendor_id : [0][0][0][0]
<packet pts="0" dts="-133333" flags="K_"/>
<packet pts="4999995" dts="4866662" flags="K_"/>
<packet pts="9999990" dts="9866657" flags="K_"/>
<packet pts="14999985" dts="14866652" flags="K_"/>
<packet pts="15099984" dts="14966651" flags="K_"/>
(base) liuqi05:ufbuild liuqi$
--- end ---
>
> > 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@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}
> >
> > +@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
> > --
> > 2.25.0
> >
> > _______________________________________________
> > 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".
>
> _______________________________________________
> 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".
Thanks
Steven
_______________________________________________
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".
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2022-03-21 6:41 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-18 12:20 [FFmpeg-devel] [PATCH] avformat/hlsenc: add hls_time_delta to cut segment at before hls_time Steven Liu
2022-03-21 3:55 ` "zhilizhao(赵志立)"
2022-03-21 6:40 ` Steven Liu
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