[FFmpeg-devel] [PATCH] lavf/segment: add segment_list_live option, allow live-friendly M3U8 list format generation

Stefano Sabatini stefasab at gmail.com
Sat Sep 1 18:06:28 CEST 2012


Allow to create M3U8 list which are HLS-friendly. In particular, write a
fake EXT-X-TARGETDURATION duration field based on the specified
segment_time.

For a full discussion read trac ticket #1492:
http://ffmpeg.org/trac/ffmpeg/ticket/1642
---
 libavformat/segment.c |   17 +++++++++++++++--
 1 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/libavformat/segment.c b/libavformat/segment.c
index c695761..b25ee48 100644
--- a/libavformat/segment.c
+++ b/libavformat/segment.c
@@ -55,6 +55,7 @@ typedef struct {
     char *format;          ///< format to use for output segment files
     char *list;            ///< filename for the segment list file
     int   list_count;      ///< list counter
+    int   list_live;       ///< force live-friendly list generation
     int   list_size;       ///< number of entries for the segment list file
     double list_max_segment_time; ///< max segment time in the current list
     ListType list_type;    ///< set the list type
@@ -154,6 +155,9 @@ static int segment_list_open(AVFormatContext *s)
         avio_printf(seg->list_pb, "#EXTM3U\n");
         avio_printf(seg->list_pb, "#EXT-X-VERSION:3\n");
         avio_printf(seg->list_pb, "#EXT-X-MEDIA-SEQUENCE:%d\n", seg->list_count);
+        if (seg->list_live)
+            avio_printf(seg->list_pb,
+                        "#EXT-X-TARGETDURATION:%"PRId64"\n", seg->time / 1000000);
     }
 
     return ret;
@@ -164,8 +168,9 @@ static void segment_list_close(AVFormatContext *s)
     SegmentContext *seg = s->priv_data;
 
     if (seg->list_type == LIST_TYPE_M3U8) {
-        avio_printf(seg->list_pb, "#EXT-X-TARGETDURATION:%d\n",
-                    (int)ceil(seg->list_max_segment_time));
+        if (!seg->list_live)
+            avio_printf(seg->list_pb, "#EXT-X-TARGETDURATION:%d\n",
+                        (int)ceil(seg->list_max_segment_time));
         avio_printf(seg->list_pb, "#EXT-X-ENDLIST\n");
     }
     seg->list_count++;
@@ -282,6 +287,13 @@ static int seg_write_header(AVFormatContext *s)
         return AVERROR(EINVAL);
     }
 
+    if (seg->list_live && seg->times_str) {
+        av_log(s, AV_LOG_ERROR,
+               "segment_live set to 1 and segment_times options are mutually exclusive:"
+               "specify -segment_time if you want a live-friendly list\n");
+        return AVERROR(EINVAL);
+    }
+
     if (seg->times_str) {
         if ((ret = parse_times(s, &seg->times, &seg->nb_times, seg->times_str)) < 0)
             return ret;
@@ -448,6 +460,7 @@ static int seg_write_trailer(struct AVFormatContext *s)
 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_list",      "set the segment list filename",              OFFSET(list),    AV_OPT_TYPE_STRING, {.str = NULL},  0, 0,       E },
+    { "segment_list_live", "enable live-friendly list generation (useful for HLS)", OFFSET(list_live), AV_OPT_TYPE_INT, {.dbl = 0},  0, 1, E },
     { "segment_list_size", "set the maximum number of playlist entries", OFFSET(list_size), AV_OPT_TYPE_INT,  {.dbl = 0},     0, INT_MAX, E },
     { "segment_list_type", "set the segment list type",                  OFFSET(list_type), AV_OPT_TYPE_INT,  {.dbl = LIST_TYPE_UNDEFINED}, -1, LIST_TYPE_NB-1, E, "list_type" },
     { "flat", "flat format",     0, AV_OPT_TYPE_CONST, {.dbl=LIST_TYPE_FLAT }, INT_MIN, INT_MAX, 0, "list_type" },
-- 
1.7.5.4



More information about the ffmpeg-devel mailing list