[FFmpeg-devel] [PATCH] lavf/segment: add -segment_times option
Stefano Sabatini
stefasab at gmail.com
Wed Jul 4 01:21:58 CEST 2012
This option allows to specify the split times for the resulting segments.
FIXME: bump micro
---
doc/muxers.texi | 11 +++++++
libavformat/segment.c | 76 +++++++++++++++++++++++++++++++++++++++++++++----
2 files changed, 81 insertions(+), 6 deletions(-)
diff --git a/doc/muxers.texi b/doc/muxers.texi
index 7531a52..c63356a 100644
--- a/doc/muxers.texi
+++ b/doc/muxers.texi
@@ -463,6 +463,10 @@ listfile is generated.
@item segment_list_size @var{size}
Overwrite the listfile once it reaches @var{size} entries. If 0
the listfile is never overwritten. Default value is 5.
+ at item segment_times @var{times}
+Specify a list of split points. @var{times} contains a list of comma
+separated duration specifications, which are supposed to be in
+increasing order.
@item segment_wrap @var{limit}
Wrap around segment index once it reaches @var{limit}.
@end table
@@ -479,6 +483,13 @@ ffmpeg -i in.mkv -codec copy -map 0 -f segment -segment_list out.list out%03d.nu
@end example
@item
+As the example above, but segment the input file according to the split
+points specified by the @var{segment_times} option:
+ at example
+ffmpeg -i in.mkv -codec copy -map 0 -f segment -segment_list out.list -segment_times 10,20,40,50,90,120,180 out%03d.nut
+ at end example
+
+ at item
To convert the @file{in.mkv} to TS segments using the @code{libx264}
and @code{libfaac} encoders:
@example
diff --git a/libavformat/segment.c b/libavformat/segment.c
index 429ca1e..54b9f9a 100644
--- a/libavformat/segment.c
+++ b/libavformat/segment.c
@@ -41,6 +41,9 @@ typedef struct {
int wrap; ///< number after which the index wraps
char *time_str; ///< segment duration specification string
int64_t time; ///< segment duration
+ char *times_str; ///< segment times specification string
+ int64_t *times; ///< list of segment interval specification
+ int nb_times; ///< number of elments in the times array
int has_video;
double start_time, end_time;
} SegmentContext;
@@ -124,6 +127,45 @@ end:
return ret;
}
+static int parse_times(void *log_ctx, int64_t **times, int *nb_times,
+ const char *times_str)
+{
+ const char *p;
+ int i, ret;
+ *nb_times = 1;
+
+ for (p = times_str; *p; p++)
+ if (*p == ',')
+ (*nb_times)++;
+
+ *times = av_realloc_f(NULL, sizeof(**times), *nb_times);
+ if (!*times) {
+ av_log(log_ctx, AV_LOG_ERROR, "Could not allocate forced times array.\n");
+ return AVERROR(ENOMEM);
+ }
+
+ for (i = 0; i < *nb_times; i++) {
+ int64_t t;
+ p = i ? strchr(p, ',') + 1 : times_str;
+ ret = av_parse_time(&t, p, 1);
+ if (ret < 0) {
+ av_log(log_ctx, AV_LOG_ERROR,
+ "Invalid time duration specification in %s.\n", p);
+ return AVERROR(EINVAL);
+ }
+ (*times)[i] = t;
+
+ /* check on monotonicity */
+ if (i && (*times)[i-1] > (*times)[i]) {
+ av_log(log_ctx, AV_LOG_ERROR,
+ "Specified time %f is greater than the following time %f\n",
+ (float)((*times)[i])/1000000, (float)((*times)[i-1])/1000000);
+ return AVERROR(EINVAL);
+ }
+ }
+ return 0;
+}
+
static int seg_write_header(AVFormatContext *s)
{
SegmentContext *seg = s->priv_data;
@@ -132,11 +174,25 @@ static int seg_write_header(AVFormatContext *s)
seg->number = 0;
- if ((ret = av_parse_time(&seg->time, seg->time_str, 1)) < 0) {
+ if (seg->time_str && seg->times_str) {
av_log(s, AV_LOG_ERROR,
- "Invalid time duration specification '%s' for segment_time option\n",
- seg->time_str);
- return ret;
+ "segment_time and segment_times options are mutually exclusive, select just one of them\n");
+ return AVERROR(EINVAL);
+ }
+
+ if (seg->times_str) {
+ if ((ret = parse_times(s, &seg->times, &seg->nb_times, seg->times_str)) < 0)
+ return ret;
+ } else {
+ /* set default value if not specified */
+ if (!seg->time_str)
+ seg->time_str = av_strdup("2");
+ if ((ret = av_parse_time(&seg->time, seg->time_str, 1)) < 0) {
+ av_log(s, AV_LOG_ERROR,
+ "Invalid time duration specification '%s' for segment_time option\n",
+ seg->time_str);
+ return ret;
+ }
}
oc = avformat_alloc_context();
@@ -209,9 +265,15 @@ static int seg_write_packet(AVFormatContext *s, AVPacket *pkt)
SegmentContext *seg = s->priv_data;
AVFormatContext *oc = seg->avf;
AVStream *st = oc->streams[pkt->stream_index];
- int64_t end_pts = seg->time * seg->number;
+ int64_t end_pts;
int ret;
+ if (seg->times) {
+ end_pts = seg->number <= seg->nb_times ? seg->times[seg->number-1] : INT64_MAX;
+ } else {
+ end_pts = seg->time * seg->number;
+ }
+
/* if the segment has video, start a new segment *only* with a key video frame */
if ((st->codec->codec_type == AVMEDIA_TYPE_VIDEO || !seg->has_video) &&
av_compare_ts(pkt->pts, st->time_base,
@@ -252,6 +314,7 @@ static int seg_write_trailer(struct AVFormatContext *s)
avio_close(seg->list_pb);
av_opt_free(seg);
+ av_freep(&seg->times);
oc->streams = NULL;
oc->nb_streams = 0;
@@ -263,7 +326,8 @@ static int seg_write_trailer(struct AVFormatContext *s)
#define E AV_OPT_FLAG_ENCODING_PARAM
static const AVOption options[] = {
{ "segment_format", "set container format used for the segments", OFFSET(format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
- { "segment_time", "set segment duration", OFFSET(time_str), AV_OPT_TYPE_STRING,{.str = "2"}, 0, 0, E },
+ { "segment_time", "set segment duration", OFFSET(time_str), AV_OPT_TYPE_STRING,{.str = NULL}, 0, 0, E },
+ { "segment_times", "set segment split time points", OFFSET(times_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
{ "segment_list", "set the segment list filename", OFFSET(list), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
{ "segment_list_size", "set the maximum number of playlist entries", OFFSET(list_size), AV_OPT_TYPE_INT, {.dbl = 5}, 0, INT_MAX, E },
{ "segment_wrap", "set number after which the index wraps", OFFSET(wrap), AV_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, E },
--
1.7.5.4
More information about the ffmpeg-devel
mailing list