[FFmpeg-devel] [PATCH 7/7] avformat/hlsenc: add an option to set the media filename

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


Works with or without the 'single_file' HLS flag.
---
 doc/muxers.texi      | 11 +++++++++++
 libavformat/hlsenc.c | 31 ++++++++++++++++++-------------
 2 files changed, 29 insertions(+), 13 deletions(-)

diff --git a/doc/muxers.texi b/doc/muxers.texi
index b97775b..63a0fdf 100644
--- a/doc/muxers.texi
+++ b/doc/muxers.texi
@@ -246,6 +246,17 @@ and it is not to be confused with the segment filename sequence number
 which can be cyclic, for example if the @option{wrap} option is
 specified.
 
+ at item hls_media_filename @var{filename}
+Set the name used for generated MPEG-TS files. If the @option{single_file}
+flag is set, this is the name of the single file. If this flag was not set,
+this is the pattern used for generated files.
+For example:
+ at example
+ffmpeg -i in.nut -hls_media_filename test-%d.ts out.m3u8
+ at end example
+Will produce the playlist, @file{out.m3u8}, and segment files:
+ at file{test-0.ts}, @file{test-1.ts}, @file{test-2.ts}, etc.
+
 @item hls_flags single_file
 If this flag is set, the muxer will store all segments in a single MPEG-TS
 file, and will use byte ranges in the playlist. HLS playlists generated with
diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
index 0faf53f..79736a1 100644
--- a/libavformat/hlsenc.c
+++ b/libavformat/hlsenc.c
@@ -76,6 +76,7 @@ typedef struct HLSContext {
     HLSSegment *segments;
     HLSSegment *last_segment;
 
+    char *media_filename_opt;
     char *media_filename;
     char *baseurl;
 
@@ -333,8 +334,6 @@ static int hls_write_header(AVFormatContext *ctx)
     HLSContext *hls;
     int ret;
     char *dot;
-    size_t basename_size;
-    char filename[1024];
 
     hls = ctx->priv_data;
 
@@ -349,23 +348,28 @@ static int hls_write_header(AVFormatContext *ctx)
     }
 
     /* Generate the name of the media file(s) */
-    av_strlcpy(filename, ctx->filename, sizeof(filename));
-    dot = strrchr(filename, '.');
-    if (dot)
-        *dot = '\0';
+    if (hls->media_filename_opt) {
+        hls->media_filename = av_strdup(hls->media_filename_opt);
+    } else {
+        char filename[1024];
+
+        av_strlcpy(filename, ctx->filename, sizeof(filename));
+        dot = strrchr(filename, '.');
+        if (dot)
+            *dot = '\0';
+
+        if (!(hls->flags & HLS_SINGLE_FILE))
+            av_strlcat(filename, "%d", sizeof(filename));
+        av_strlcat(filename, ".ts", sizeof(filename));
+
+        hls->media_filename = av_strdup(filename);
+    }
 
-    basename_size = sizeof(filename);
-    hls->media_filename = av_malloc(basename_size);
     if (!hls->media_filename) {
         ret = AVERROR(ENOMEM);
         goto fail;
     }
 
-    av_strlcpy(hls->media_filename, filename, basename_size);
-    if (!(hls->flags & HLS_SINGLE_FILE))
-        av_strlcat(hls->media_filename, "%d", basename_size);
-    av_strlcat(hls->media_filename, ".ts", basename_size);
-
     /* Initialize the muxer and create the first file */
     ret = hls_mux_init(ctx);
     if (ret < 0)
@@ -502,6 +506,7 @@ static const AVOption options[] = {
     {"hls_list_size", "set maximum number of playlist entries",  OFFSET(max_nb_segments), AV_OPT_TYPE_INT,    {.i64 = 5},     0, INT_MAX, E},
     {"hls_wrap",      "set number after which the index wraps",  OFFSET(wrap),    AV_OPT_TYPE_INT,    {.i64 = 0},     0, INT_MAX, E},
     {"hls_base_url",  "url to prepend to each playlist entry",   OFFSET(baseurl), AV_OPT_TYPE_STRING, {.str = NULL},  0, 0,       E},
+    {"hls_media_filename",  "the name of generated media files", OFFSET(media_filename_opt), AV_OPT_TYPE_STRING, {.str = NULL},  0, 0, E},
 
     {"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"},
-- 
1.8.5.5



More information about the ffmpeg-devel mailing list