[FFmpeg-devel] libavformat/segment: add an option to split the rest of stream after the segment_times is set

hectorqin hectorqin at 163.com
Tue Jun 18 10:17:21 EEST 2019


Hi all:
   Add an option to split the rest of stream after the segment_times is set. It's very useful to set the first some segments to a little small, and then split with a clear duration.


   The code is simple, but i'm not good at writing the document. 


From cd90b0b552223c78741eecb6cbdd13a4ae8f3e57 Mon Sep 17 00:00:00 2001
From: hectorqin <hectorqin at 163.com>
Date: Tue, 18 Jun 2019 15:03:21 +0800
Subject: [PATCH] Add libavformat segment_rest_duration option, ignore VSCode
 editor files


---
 .gitignore            |  1 +
 doc/muxers.texi       |  5 +++++
 libavformat/segment.c | 21 +++++++++++++++++++--
 3 files changed, 25 insertions(+), 2 deletions(-)


diff --git a/.gitignore b/.gitignore
index 0e57cb0b4c..33f8693522 100644
--- a/.gitignore
+++ b/.gitignore
@@ -36,3 +36,4 @@
 /lcov/
 /src
 /mapfile
+/.vscode
\ No newline at end of file
diff --git a/doc/muxers.texi b/doc/muxers.texi
index 50147c4d20..38f773c055 100644
--- a/doc/muxers.texi
+++ b/doc/muxers.texi
@@ -1912,6 +1912,11 @@ Specify a list of split points. @var{times} contains a list of comma
 separated duration specifications, in increasing order. See also
 the @option{segment_time} option.
 
+ at item segment_rest_duration @var{rest_duration}
+Set the rest segment duration to @var{rest_duration} when the @var{segment_times} is set, the value must be a duration
+specification. Default value is "0", which means don't split the rest part. See also the
+ at option{segment_time} option.
+
 @item segment_frames @var{frames}
 Specify a list of split video frame numbers. @var{frames} contains a
 list of comma separated integer numbers, in increasing order.
diff --git a/libavformat/segment.c b/libavformat/segment.c
index 6e37707f9f..0d22549296 100644
--- a/libavformat/segment.c
+++ b/libavformat/segment.c
@@ -100,6 +100,9 @@ typedef struct SegmentContext {
     int64_t *times;        ///< list of segment interval specification
     int nb_times;          ///< number of elments in the times array
 
+    char *rest_duration_str; ///< rest segment duration specification string
+    int64_t rest_duration;   ///< rest segment duration
+
     char *frames_str;      ///< segment frame numbers specification string
     int *frames;           ///< list of frame number specification
     int nb_frames;         ///< number of elments in the frames array
@@ -675,6 +678,14 @@ static int seg_init(AVFormatContext *s)
     if (seg->times_str) {
         if ((ret = parse_times(s, &seg->times, &seg->nb_times, seg->times_str)) < 0)
             return ret;
+        if (seg->rest_duration_str) {
+            if ((ret = av_parse_time(&seg->rest_duration, seg->rest_duration_str, 1)) < 0) {
+                av_log(s, AV_LOG_ERROR,
+                        "Invalid time duration specification '%s' for segment_rest_duration option\n",
+                        seg->rest_duration_str);
+                return ret;
+            }
+        }
     } else if (seg->frames_str) {
         if ((ret = parse_frames(s, &seg->frames, &seg->nb_frames, seg->frames_str)) < 0)
             return ret;
@@ -875,8 +886,13 @@ static int seg_write_packet(AVFormatContext *s, AVPacket *pkt)
 
 calc_times:
     if (seg->times) {
-        end_pts = seg->segment_count < seg->nb_times ?
-            seg->times[seg->segment_count] : INT64_MAX;
+        if (seg->segment_count < seg->nb_times) {
+            end_pts = seg->times[seg->segment_count];
+        } else if (seg->rest_duration > 0) {
+            end_pts = seg->times[seg->nb_times - 1] + seg->rest_duration * (seg->segment_count + 1 - seg->nb_times);
+        } else {
+            end_pts = INT64_MAX;
+        }
     } else if (seg->frames) {
         start_frame = seg->segment_count < seg->nb_frames ?
             seg->frames[seg->segment_count] : INT_MAX;
@@ -1061,6 +1077,7 @@ static const AVOption options[] = {
     { "segment_time",      "set segment duration",                       OFFSET(time_str),AV_OPT_TYPE_STRING, {.str = NULL},  0, 0,       E },
     { "segment_time_delta","set approximation value used for the segment times", OFFSET(time_delta), AV_OPT_TYPE_DURATION, {.i64 = 0}, 0, INT64_MAX, E },
     { "segment_times",     "set segment split time points",              OFFSET(times_str),AV_OPT_TYPE_STRING,{.str = NULL},  0, 0,       E },
+    { "segment_rest_duration", "set rest segment duration after out of the segment_times", OFFSET(rest_duration_str), AV_OPT_TYPE_STRING,{.str = NULL},  0, 0,       E },
     { "segment_frames",    "set segment split frame numbers",            OFFSET(frames_str),AV_OPT_TYPE_STRING,{.str = NULL},  0, 0,       E },
     { "segment_wrap",      "set number after which the index wraps",     OFFSET(segment_idx_wrap), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
     { "segment_list_entry_prefix", "set base url prefix for segments", OFFSET(entry_prefix), AV_OPT_TYPE_STRING,  {.str = NULL}, 0, 0, E },
-- 
2.17.2 (Apple Git-113)



More information about the ffmpeg-devel mailing list