Browse code

lavf/segment: add segment_list_flags option

Allow to specify options affecting the segment list generation.

In particular: add +live and +cache flags.

For a full discussion read trac ticket #1642:
http://ffmpeg.org/trac/ffmpeg/ticket/1642

Also add live M3U8 generation example.

Stefano Sabatini authored on 2012/09/02 01:01:51
Showing 3 changed files
... ...
@@ -479,6 +479,24 @@ extension.
479 479
 @item segment_list @var{name}
480 480
 Generate also a listfile named @var{name}. If not specified no
481 481
 listfile is generated.
482
+@item segment_list_flags @var{flags}
483
+Set flags affecting the segment list generation.
484
+
485
+It currently supports the following flags:
486
+@table @var
487
+@item cache
488
+Allow caching (only affects M3U8 list files).
489
+
490
+@item live
491
+Allow live-friendly file generation.
492
+
493
+This currently only affects M3U8 lists. In particular, write a fake
494
+EXT-X-TARGETDURATION duration field at the top of the file, based on
495
+the specified @var{segment_time}.
496
+@end table
497
+
498
+Default value is @code{cache}.
499
+
482 500
 @item segment_list_size @var{size}
483 501
 Overwrite the listfile once it reaches @var{size} entries. If 0
484 502
 the listfile is never overwritten. Default value is 0.
... ...
@@ -584,6 +602,14 @@ and @code{libfaac} encoders:
584 584
 @example
585 585
 ffmpeg -i in.mkv -map 0 -codec:v libx264 -codec:a libfaac -f ssegment -segment_list out.list out%03d.ts
586 586
 @end example
587
+
588
+@item
589
+Segment the input file, and create an M3U8 live playlist (can be used
590
+as live HLS source):
591
+@example
592
+ffmpeg -re -i in.mkv -codec copy -map 0 -f segment -segment_list playlist.m3u8 \
593
+-segment_list_flags +live -segment_time 10 out%03d.mkv
594
+@end example
587 595
 @end itemize
588 596
 
589 597
 @section mp3
... ...
@@ -46,6 +46,9 @@ typedef enum {
46 46
 
47 47
 #define LIST_TYPE_EXT LIST_TYPE_CSV
48 48
 
49
+#define SEGMENT_LIST_FLAG_CACHE 1
50
+#define SEGMENT_LIST_FLAG_LIVE  2
51
+
49 52
 typedef struct {
50 53
     const AVClass *class;  /**< Class for private options. */
51 54
     int segment_idx;       ///< index of the segment file to write, starting from 0
... ...
@@ -55,6 +58,7 @@ typedef struct {
55 55
     char *format;          ///< format to use for output segment files
56 56
     char *list;            ///< filename for the segment list file
57 57
     int   list_count;      ///< list counter
58
+    int   list_flags;      ///< flags affecting list generation
58 59
     int   list_size;       ///< number of entries for the segment list file
59 60
     double list_max_segment_time; ///< max segment time in the current list
60 61
     ListType list_type;    ///< set the list type
... ...
@@ -156,6 +160,11 @@ static int segment_list_open(AVFormatContext *s)
156 156
         avio_printf(seg->list_pb, "#EXTM3U\n");
157 157
         avio_printf(seg->list_pb, "#EXT-X-VERSION:3\n");
158 158
         avio_printf(seg->list_pb, "#EXT-X-MEDIA-SEQUENCE:%d\n", seg->list_count);
159
+        avio_printf(seg->list_pb, "#EXT-X-ALLOWCACHE:%d\n",
160
+                    !!(seg->list_flags & SEGMENT_LIST_FLAG_CACHE));
161
+        if (seg->list_flags & SEGMENT_LIST_FLAG_LIVE)
162
+            avio_printf(seg->list_pb,
163
+                        "#EXT-X-TARGETDURATION:%"PRId64"\n", seg->time / 1000000);
159 164
     }
160 165
 
161 166
     return ret;
... ...
@@ -166,8 +175,9 @@ static void segment_list_close(AVFormatContext *s)
166 166
     SegmentContext *seg = s->priv_data;
167 167
 
168 168
     if (seg->list_type == LIST_TYPE_M3U8) {
169
-        avio_printf(seg->list_pb, "#EXT-X-TARGETDURATION:%d\n",
170
-                    (int)ceil(seg->list_max_segment_time));
169
+        if (!(seg->list_flags & SEGMENT_LIST_FLAG_LIVE))
170
+            avio_printf(seg->list_pb, "#EXT-X-TARGETDURATION:%d\n",
171
+                        (int)ceil(seg->list_max_segment_time));
171 172
         avio_printf(seg->list_pb, "#EXT-X-ENDLIST\n");
172 173
     }
173 174
     seg->list_count++;
... ...
@@ -284,6 +294,13 @@ static int seg_write_header(AVFormatContext *s)
284 284
         return AVERROR(EINVAL);
285 285
     }
286 286
 
287
+    if ((seg->list_flags & SEGMENT_LIST_FLAG_LIVE) && seg->times_str) {
288
+        av_log(s, AV_LOG_ERROR,
289
+               "segment_flags +live and segment_times options are mutually exclusive:"
290
+               "specify -segment_time if you want a live-friendly list\n");
291
+        return AVERROR(EINVAL);
292
+    }
293
+
287 294
     if (seg->times_str) {
288 295
         if ((ret = parse_times(s, &seg->times, &seg->nb_times, seg->times_str)) < 0)
289 296
             return ret;
... ...
@@ -450,6 +467,11 @@ static int seg_write_trailer(struct AVFormatContext *s)
450 450
 static const AVOption options[] = {
451 451
     { "segment_format",    "set container format used for the segments", OFFSET(format),  AV_OPT_TYPE_STRING, {.str = NULL},  0, 0,       E },
452 452
     { "segment_list",      "set the segment list filename",              OFFSET(list),    AV_OPT_TYPE_STRING, {.str = NULL},  0, 0,       E },
453
+
454
+    { "segment_list_flags","set flags affecting segment list generation", OFFSET(list_flags), AV_OPT_TYPE_FLAGS, {.i64 = SEGMENT_LIST_FLAG_CACHE }, 0, UINT_MAX, E, "list_flags"},
455
+    { "cache",             "allow list caching",                                    0, AV_OPT_TYPE_CONST, {.i64 = SEGMENT_LIST_FLAG_CACHE }, INT_MIN, INT_MAX,   E, "list_flags"},
456
+    { "live",              "enable live-friendly list generation (useful for HLS)", 0, AV_OPT_TYPE_CONST, {.i64 = SEGMENT_LIST_FLAG_LIVE }, INT_MIN, INT_MAX,    E, "list_flags"},
457
+
453 458
     { "segment_list_size", "set the maximum number of playlist entries", OFFSET(list_size), AV_OPT_TYPE_INT,  {.i64 = 0},     0, INT_MAX, E },
454 459
     { "segment_list_type", "set the segment list type",                  OFFSET(list_type), AV_OPT_TYPE_INT,  {.i64 = LIST_TYPE_UNDEFINED}, -1, LIST_TYPE_NB-1, E, "list_type" },
455 460
     { "flat", "flat format",     0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_FLAT }, INT_MIN, INT_MAX, 0, "list_type" },
... ...
@@ -31,7 +31,7 @@
31 31
 
32 32
 #define LIBAVFORMAT_VERSION_MAJOR 54
33 33
 #define LIBAVFORMAT_VERSION_MINOR 26
34
-#define LIBAVFORMAT_VERSION_MICRO 100
34
+#define LIBAVFORMAT_VERSION_MICRO 101
35 35
 
36 36
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
37 37
                                                LIBAVFORMAT_VERSION_MINOR, \