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

Submitted by hectorqin on June 28, 2019, 3:12 a.m.

Details

Message ID 228be254.122d.16b9c11c48e.Coremail.hectorqin@163.com
State New
Headers show

Commit Message

hectorqin June 28, 2019, 3:12 a.m.
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.




Signed-off-by: hectorqin <hectorqin@163.com>
---
 doc/muxers.texi       |  5 +++++
 libavformat/segment.c | 21 +++++++++++++++++++--
 2 files changed, 24 insertions(+), 2 deletions(-)

Patch hide | download patch | download mbox

diff --git a/doc/muxers.texi b/doc/muxers.texi
index 59c93bc687..7a0f7d2a52 100644
--- a/doc/muxers.texi
+++ b/doc/muxers.texi
@@ -1930,6 +1930,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.
 
+@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
+@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 e3082063d8..8a325cd5e9 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
@@ -698,6 +701,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;
@@ -898,8 +909,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;
@@ -1084,6 +1100,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 },