[FFmpeg-devel] [PATCH v2 2/6] avformat/segment: Add segment_limit option
softworkz
ffmpegagent at gmail.com
Sat Jun 14 03:59:08 EEST 2025
From: softworkz <softworkz at hotmail.com>
Example use case:
Existing segments 0-30 and 70-99, 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.
Signed-off-by: softworkz <softworkz at hotmail.com>
---
doc/muxers.texi | 7 +++++++
libavformat/segment.c | 12 ++++++++++++
2 files changed, 19 insertions(+)
diff --git a/doc/muxers.texi b/doc/muxers.texi
index 6d5c17b4cc..1cca8da1fb 100644
--- a/doc/muxers.texi
+++ b/doc/muxers.texi
@@ -3510,6 +3510,13 @@ packet written. Defaults to @code{0}.
Write segments to files with a .tmp extension. Each file is renamed to its
actual name on completion. This can help to prevent segment files from
being accessed before they are complete. Disabled by default (@code{0}).
+
+ at item segment_limit @var{number}
+Stops after the specified number of segments has been generated.
+This can be helpful to fill gaps in a range of already generated segments,
+which is difficult to achieve otherwise as it would either cause the last
+segment to be incomplete or to overwrite an existing segment with a partial
+data. Default is @code{0} - no limit.
@end table
Make sure to require a closed GOP when encoding and to set the GOP
diff --git a/libavformat/segment.c b/libavformat/segment.c
index 14eba90292..e8b1582e7d 100644
--- a/libavformat/segment.c
+++ b/libavformat/segment.c
@@ -121,6 +121,8 @@ typedef struct SegmentContext {
int break_non_keyframes;
int write_empty;
+ int segment_limit; ///< max number of segments to create
+
int segment_write_temp; ///< write segments as temp files and rename on completion
int use_rename;
char temp_list_filename[1024];
@@ -365,6 +367,9 @@ static int segment_end(AVFormatContext *s, int write_trailer, int is_last)
int i;
int err;
+ if (seg->segment_limit && seg->segment_count >= seg->segment_limit)
+ return 0;
+
if (!oc || !oc->pb)
return AVERROR(EINVAL);
@@ -879,6 +884,9 @@ static int seg_write_packet(AVFormatContext *s, AVPacket *pkt)
int64_t usecs;
int64_t wrapped_val;
+ if (seg->segment_limit && seg->segment_count >= seg->segment_limit)
+ return 0;
+
if (!seg->avf || !seg->avf->pb)
return AVERROR(EINVAL);
@@ -953,6 +961,9 @@ calc_times:
if ((ret = segment_end(s, seg->individual_header_trailer, 0)) < 0)
goto fail;
+ if (seg->segment_limit && seg->segment_count >= seg->segment_limit)
+ return 0;
+
if ((ret = segment_start(s, seg->individual_header_trailer)) < 0)
goto fail;
@@ -1098,6 +1109,7 @@ static const AVOption options[] = {
{ "initial_offset", "set initial timestamp offset", OFFSET(initial_offset), AV_OPT_TYPE_DURATION, {.i64 = 0}, -INT64_MAX, INT64_MAX, E },
{ "write_empty_segments", "allow writing empty 'filler' segments", OFFSET(write_empty), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, E },
{ "segment_write_temp", "write segments as temp files (.tmp) and rename on completion", OFFSET(segment_write_temp), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, E },
+ { "segment_limit", "stop output once the specified number of segments has been written", OFFSET(segment_limit), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
{ NULL },
};
--
ffmpeg-codebot
More information about the ffmpeg-devel
mailing list