[FFmpeg-devel] [PATCH 6/7] avformat/hlsenc: add a flag disabling the filename in segment names

Nicolas Martyanoff khaelin at gmail.com
Fri Jul 18 10:57:46 CEST 2014


The default way to name segment is to use the name of the media file, with an
optional prefix (-hls_base_url).

However in applications where the URL used to fetch segments does not contain
the name of the file, it is useful to have the ability not to include the
filename in segment names.

Of course this only makes sense in single file mode, since without it,
segments are stored in individual files.
---
 doc/muxers.texi      |  6 ++++++
 libavformat/hlsenc.c | 22 +++++++++++++++++++++-
 2 files changed, 27 insertions(+), 1 deletion(-)

diff --git a/doc/muxers.texi b/doc/muxers.texi
index 186619e..b97775b 100644
--- a/doc/muxers.texi
+++ b/doc/muxers.texi
@@ -256,6 +256,12 @@ ffmpeg -i in.nut -hls_flags single_file out.m3u8
 @end example
 Will produce the playlist, @file{out.m3u8}, and a single segment file,
 @file{out.ts}.
+
+ at item hls_flags no_filename
+If this flag is set, the HLS playlist will not include the filename in each
+segment description, only the URL provided with the @option{baseurl} option.
+This option can only be used if the @option{single_file} flag is set, and if
+the @option{baseurl} option is provided.
 @end table
 
 @anchor{ico}
diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
index 8b644a2..0faf53f 100644
--- a/libavformat/hlsenc.c
+++ b/libavformat/hlsenc.c
@@ -43,6 +43,8 @@ typedef struct HLSSegment {
 typedef enum HLSFlags {
     // Generate a single media file and use byte ranges in the playlist.
     HLS_SINGLE_FILE = (1 << 0),
+    // Do not include the media filename in segment name (only the base url)
+    HLS_NO_FILENAME = (1 << 1),
 } HLSFlags;
 
 typedef struct HLSContext {
@@ -87,6 +89,22 @@ static int hls_mux_init(AVFormatContext *ctx)
 
     hls = ctx->priv_data;
 
+    if (hls->flags & HLS_NO_FILENAME) {
+        if (!(hls->flags & HLS_SINGLE_FILE)) {
+            av_log(ctx, AV_LOG_ERROR,
+                   "HLS flag 'no_filename' cannot be used without flag "
+                   "'single_file'\n");
+            return AVERROR(EINVAL);
+        }
+
+        if (!hls->baseurl) {
+            av_log(ctx, AV_LOG_ERROR,
+                   "HLS flag 'no_filename' cannot be used without "
+                   "-hls_base_url\n");
+            return AVERROR(EINVAL);
+        }
+    }
+
     hls->segment_duration = 0.0;
     hls->segment_size = 0;
     hls->segment_pos = 0;
@@ -260,7 +278,8 @@ static int hls_generate_playlist(AVFormatContext *ctx)
         }
 
         avio_printf(hls->pb, "%s%s\n",
-                    (hls->baseurl ? hls->baseurl : ""), segment->filename);
+                    (hls->baseurl ? hls->baseurl : ""),
+                    ((hls->flags & HLS_NO_FILENAME) ? "" : segment->filename));
     }
 
     avio_printf(hls->pb, "#EXT-X-ENDLIST\n");
@@ -486,6 +505,7 @@ static const AVOption options[] = {
 
     {"hls_flags",   "set flags affecting HLS playlist and media file generation", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0 }, 0, UINT_MAX, E, "flags"},
     {"single_file", "generate a single media file indexed with byte ranges", 0, AV_OPT_TYPE_CONST, {.i64 = HLS_SINGLE_FILE }, 0, UINT_MAX,   E, "flags"},
+    {"no_filename", "do not use the name of the media file in segment names", 0, AV_OPT_TYPE_CONST, {.i64 = HLS_NO_FILENAME }, 0, UINT_MAX,   E, "flags"},
 
     { NULL },
 };
-- 
1.8.5.5



More information about the ffmpeg-devel mailing list