From: "softworkz ." <softworkz-at-hotmail.com@ffmpeg.org>
To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org>
Subject: Re: [FFmpeg-devel] [PATCH 05/10] doc/muxers: Add documentation for segment_limit option
Date: Mon, 23 Jun 2025 02:02:55 +0000
Message-ID: <DM8P223MB0365B0ACA383930FBA57E714BA79A@DM8P223MB0365.NAMP223.PROD.OUTLOOK.COM> (raw)
In-Reply-To: <4e2b3b0c-27ea-c803-b2d5-4dc8bc602a18@passwd.hu>
> -----Original Message-----
> From: ffmpeg-devel <ffmpeg-devel-bounces@ffmpeg.org> On Behalf Of Marton
> Balint
> Sent: Samstag, 14. Juni 2025 18:00
> To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org>
> Subject: Re: [FFmpeg-devel] [PATCH 05/10] doc/muxers: Add documentation
> for segment_limit option
>
>
>
> On Fri, 13 Jun 2025, softworkz . wrote:
>
> [...]
>
> >>> (Please note, that the default is 0, which means that nothing is
> >>> dropped and there's no change in behavior when it's 0).
> >>>
> >>> Probably it's best to look at an example. Let's say we have:
> >>>
> >>> - a 300s video
> >>> - that we want to stream via HLS
> >>> - Segment-Duration: 3s - makes 100 segments
> >>> - Now we want to create the segments on-demand only,
> >>> so we deliver a synthetic playlist with 100 3s segments,
> >>> even though we don't have any segment yet
> >>> - Once specific segments are needed, we create them on-the-fly
> >>>
> >>> That's a situation that the commit message is about:
> >>>
> >>> Existing segments 0-30 and 70-99 => we already have them on disk
> >>>
> >>> 31-69 need to be created
> >>>
> >>> This option allows to stop precisely after 69.
> >>> Otherwise, it would start overwriting segment 70 before stopping
> >>> via 'q' or break signal.
> >>>
> >>> So, in order to generate segments 31-69, you will set
> >>> segment_start_number to 31 and the segment_limit to 38.
> >>> This causes the muxer to write and complete segment 69
> >>> in the exact same way like when it would be creating segment
> >>> 70, but without starting to write segment 70 - which would
> >> destroy
> >>> the existing segment 70 (which is good already).
> >>
> >> Buy you have to seek in the input to achieve that, don't you? And
> >> you can
> >> just as easily specify the input duration to not overwrite segment
> >> 70...
> >
> > Here we're getting at the core of the problem: When specifying -to or
> > a duration, you cannot achieve the exact same "cut" like the segment
> > muxer does it. It usually cuts by video key frames. And when it does
> > the cut it also cuts all other streams at that moment. Timestamp
> > offsets between video and audio are common, often the audio is
> > somewhat ahead of the video (specifically in case of TV broadcast
> > streams), but it varies. Also, when transcoding before segmenting,
> > it's not easy to predict the exact video key frame times.
> >
> > For each segment n to segment n+1 the cut must be done in a way
> > that for each stream, there is neither a duplicate nor a missing
> > frame. That is what makes it non-trivial.
> >
> > In the end, it always comes down to the same conclusion: the only
> > way to get the exact same cut point through all streams like the
> > segment muxer does it - is to let the segment muxer do it.
> >
> > At least that was my conclusion from intensive work on this, some
> > years ago.
>
> But this is also a problem for the first segment. You can set
> segment_start_number to 31, but you still have to know where to seek
> exactly, because setting segment_start_number alone won't make the
> segment
> muxer "ignore" data for the first 30 segments...
>
> So what does not make sense to me is that you want to rely on segment
> muxer behaviour for ending the segments, but not for starting them. The
> way I see it you either know exactly where the segments supposed to
> start
> and end, in which case you don't really need this option, because you
> can set -ss and -to as you need. Or you don't, in which case this option
> alone won't help you.
Hi Marton,
you are right to ask about the side of starting at a specific segment.
This is something I wanted to bring up just after the patch for the
ending segment, because that's a topic which is way more difficult.
The core requirement is to achieve the exact same segmentation each time
when running FFmpeg to create a defined subset of HLS segments.
When transcoding is involved (before segmentation), it can even depend
on the decoder WRT the positions at which it is emitting key frames,
and then also the segment muxer with its various parameters to control
the segment cut positions.
Several parameters need to be set up precisely in a way that the transcoding
operation will pick up the same "rhythm" of operation like it would have
when starting from zero.
The recipe is like this:
- Probe the preceding segment of where you want to start
e.g. when you want to generate segments n,..., n+10, you probe existing
segment n-1. But you can only do so when it already exists (otherwise
you can just estimate)
- Get the PTS time of the first video frame (assuming it's a "key-frame"
or similar)
- Compensate for input/output time offsets (or make sure there are none)
- Subtract 1 from this time
- Depending on the operation:
- If video encoding involved: use this as -ss value
- Otherwise, subtract a second (or half) and use this as -ss value
- Set the segment muxer start segment to n-1
- Then I have two more options for the segment muxer: min_frame_time
and skip_first_segment
- min_frame_time gets the pts-minus-1 value
this causes the muxer to remain inactive until it sees a video frame
with a time greater than that - then it starts operation
- skip_first_segment tells it not to write the first segment
Summary:
You can never get the first segment immediately "right", where "right" means
identical to how it would be written as part (in the middle) of a sequence.
So we start one segment earlier, still try to be precise about its start to
ensure that encoder and segment muxer get into the proper "segmentation
rhythm", and discard the first segment which is most likely "wrong"
(=different) WRT its cut at the start.
Obviously, this is not a very nice solution. It only works when all
the supplied parameters are precisely set in the right way.
That made me hesitant to even submit a patch to add those options.
Maybe there's a better way, that I've just been failing to see..?
Best regards,
sw
PS: I want to retain audio-video offsets like in case of tV broadcast
signals
_______________________________________________
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".
next prev parent reply other threads:[~2025-06-23 2:03 UTC|newest]
Thread overview: 34+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-13 5:54 [FFmpeg-devel] [PATCH 00/10] avformat/segment: Various segment muxer improvements ffmpegagent
2025-06-13 5:54 ` [FFmpeg-devel] [PATCH 01/10] avformat/segment: Add segment_write_temp option softworkz
2025-06-13 20:37 ` Marton Balint
2025-06-13 21:43 ` softworkz .
2025-06-13 22:07 ` Marton Balint
2025-06-13 22:26 ` softworkz .
2025-06-13 5:54 ` [FFmpeg-devel] [PATCH 02/10] doc/muxers: Add documentation for " softworkz
2025-06-13 5:54 ` [FFmpeg-devel] [PATCH 03/10] avformat/segment: Log more detailed information about written segments softworkz
2025-06-13 13:42 ` Derek Buitenhuis
2025-06-13 14:03 ` softworkz .
2025-06-13 20:44 ` Marton Balint
2025-06-13 23:53 ` softworkz .
2025-06-13 5:54 ` [FFmpeg-devel] [PATCH 04/10] avformat/segment: Add segment_limit option softworkz
2025-06-13 5:54 ` [FFmpeg-devel] [PATCH 05/10] doc/muxers: Add documentation for " softworkz
2025-06-13 20:25 ` Marton Balint
2025-06-13 21:33 ` softworkz .
2025-06-13 21:59 ` Marton Balint
2025-06-13 22:49 ` softworkz .
2025-06-14 15:59 ` Marton Balint
2025-06-23 2:02 ` softworkz . [this message]
2025-06-13 5:54 ` [FFmpeg-devel] [PATCH 06/10] avformat/segment: Fix invalid codecpar extradata_size after copying softworkz
2025-06-13 6:03 ` softworkz .
2025-06-13 5:54 ` [FFmpeg-devel] [PATCH 07/10] avformat/segment: Remove non-negative constraint from segment_time_delta softworkz
2025-06-13 5:54 ` [FFmpeg-devel] [PATCH 08/10] avformat/segment: Don't allow negative segment duration softworkz
2025-06-13 5:54 ` [FFmpeg-devel] [PATCH 09/10] avformat/segment: Fix typo softworkz
2025-06-13 5:54 ` [FFmpeg-devel] [PATCH 10/10] avformat/segment: Indent and whitespace fixes softworkz
2025-06-14 0:59 ` [FFmpeg-devel] [PATCH v2 0/6] avformat/segment: Various segment muxer improvements ffmpegagent
2025-06-14 0:59 ` [FFmpeg-devel] [PATCH v2 1/6] avformat/segment: Add segment_write_temp option softworkz
2025-06-14 0:59 ` [FFmpeg-devel] [PATCH v2 2/6] avformat/segment: Add segment_limit option softworkz
2025-06-14 0:59 ` [FFmpeg-devel] [PATCH v2 3/6] avformat/segment: Remove non-negative constraint from segment_time_delta softworkz
2025-06-14 0:59 ` [FFmpeg-devel] [PATCH v2 4/6] avformat/segment: Don't allow negative segment duration softworkz
2025-06-14 20:40 ` Michael Niedermayer
2025-06-14 0:59 ` [FFmpeg-devel] [PATCH v2 5/6] avformat/segment: Fix typo softworkz
2025-06-14 0:59 ` [FFmpeg-devel] [PATCH v2 6/6] avformat/segment: Indent and whitespace fixes softworkz
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=DM8P223MB0365B0ACA383930FBA57E714BA79A@DM8P223MB0365.NAMP223.PROD.OUTLOOK.COM \
--to=softworkz-at-hotmail.com@ffmpeg.org \
--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