[FFmpeg-devel] [PATCH v3] avformat/hlsenc: Fix path handling for Windows

softworkz ffmpegagent at gmail.com
Mon Jun 23 03:32:44 EEST 2025


From: softworkz <softworkz at hotmail.com>

..to allow playlist paths like:
c:\hls\video1\master.m3u8

When base_output_dirname was determined, only '/' was searched for as
path separator. get_relative_url() on the other hand searched for both
forward and backward slash regardless of OS.

Fix these issues by factorizing the separator finder function, only
search for backslash for Windows/DOS and use that in both places.

Reviewed-by: Marton Balint <cus at passwd.hu>
Signed-off-by: softworkz <softworkz at hotmail.com>
---
    avformat/hlsenc: Some HLS improvements
    
    Includes only a single patch now:
    
     * Fix path handling on Windows
    
    
    Versions
    ========
    
    
    V2
    ==
    
     * Fix path handling for Windows;
       * Keep using av_asprintf() with "%*s" to limit the length (as per
         review by Marton - thanks!)
       * Use int64:_t instead of int
     * Removed logging patches
    
    
    V3
    ==
    
     * Updated commit message
     * Don't compare null pointers
     * use ptrdiff_t (all as per review by Marton Balint - thanks!)
    
    .

Published-As: https://github.com/ffstaging/FFmpeg/releases/tag/pr-ffstaging-97%2Fsoftworkz%2Fsubmit_hls_logging-v3
Fetch-It-Via: git fetch https://github.com/ffstaging/FFmpeg pr-ffstaging-97/softworkz/submit_hls_logging-v3
Pull-Request: https://github.com/ffstaging/FFmpeg/pull/97

Range-diff vs v2:

 1:  93e1a502be ! 1:  485ffe85e9 avformat/hlsenc: Fix path handling for Windows
     @@ Metadata
       ## Commit message ##
          avformat/hlsenc: Fix path handling for Windows
      
     +    ..to allow playlist paths like:
     +    c:\hls\video1\master.m3u8
     +
     +    When base_output_dirname was determined, only '/' was searched for as
     +    path separator. get_relative_url() on the other hand searched for both
     +    forward and backward slash regardless of OS.
     +
     +    Fix these issues by factorizing the separator finder function, only
     +    search for backslash for Windows/DOS and use that in both places.
     +
     +    Reviewed-by: Marton Balint <cus at passwd.hu>
          Signed-off-by: softworkz <softworkz at hotmail.com>
      
       ## libavformat/hlsenc.c ##
     @@ libavformat/hlsenc.c: static int hlsenc_io_close(AVFormatContext *s, AVIOContext
           return ret;
       }
       
     -+static int64_t get_last_separator_pos(const char *path)
     ++static ptrdiff_t get_last_separator_pos(const char *path)
      +{
      +    if (!path || *path == '\0')
      +        return -1;
     @@ libavformat/hlsenc.c: static int hlsenc_io_close(AVFormatContext *s, AVIOContext
      +    char *p = strrchr(path, '/');
      +#if HAVE_DOS_PATHS
      +    char *q = strrchr(path, '\\');
     -+    p = FFMAX(p, q);
     ++    if (q) {
     ++        if (p) {
     ++            p = FFMAX(p, q);
     ++        } else {
     ++            p = q;
     ++        }
     ++    }
      +#endif
      +
      +    if (!p)
     @@ libavformat/hlsenc.c: static int hlsenc_io_close(AVFormatContext *s, AVIOContext
       {
           int http_base_proto = ff_is_http_proto(s->url);
      @@ libavformat/hlsenc.c: static int hls_rename_temp_file(AVFormatContext *s, AVFormatContext *oc)
     +     return ret;
     + }
       
     - static const char* get_relative_url(const char *master_url, const char *media_url)
     +-static const char* get_relative_url(const char *master_url, const char *media_url)
     ++static const char *get_relative_url(const char *master_url, const char *media_url)
       {
      -    const char *p = strrchr(master_url, '/');
      -    size_t base_len = 0;
      -
      -    if (!p) p = strrchr(master_url, '\\');
     -+    int64_t pos = get_last_separator_pos(master_url);
     ++    ptrdiff_t pos = get_last_separator_pos(master_url);
       
      -    if (p) {
      -        base_len = p - master_url;
     @@ libavformat/hlsenc.c: static int hls_init(AVFormatContext *s)
      -                    char tmp = *(++p);
      -                    *p = '\0';
      -                    vs->base_output_dirname = av_asprintf("%s%s", vs->m3u8_name,
     -+                int64_t pos = get_last_separator_pos(vs->m3u8_name);
     ++                ptrdiff_t pos = get_last_separator_pos(vs->m3u8_name);
      +                if (pos >= 0) {
      +                    vs->base_output_dirname = av_asprintf("%.*s%s", pos + 1, vs->m3u8_name,
                                                                 vs->fmp4_init_filename);


 libavformat/hlsenc.c | 46 +++++++++++++++++++++++++++++---------------
 1 file changed, 31 insertions(+), 15 deletions(-)

diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
index a93d35ab75..7de6820ef7 100644
--- a/libavformat/hlsenc.c
+++ b/libavformat/hlsenc.c
@@ -329,6 +329,29 @@ static int hlsenc_io_close(AVFormatContext *s, AVIOContext **pb, char *filename)
     return ret;
 }
 
+static ptrdiff_t get_last_separator_pos(const char *path)
+{
+    if (!path || *path == '\0')
+        return -1;
+
+    char *p = strrchr(path, '/');
+#if HAVE_DOS_PATHS
+    char *q = strrchr(path, '\\');
+    if (q) {
+        if (p) {
+            p = FFMAX(p, q);
+        } else {
+            p = q;
+        }
+    }
+#endif
+
+    if (!p)
+        return -1;
+
+    return p - path;
+}
+
 static void set_http_options(AVFormatContext *s, AVDictionary **options, HLSContext *c)
 {
     int http_base_proto = ff_is_http_proto(s->url);
@@ -1406,16 +1429,12 @@ static int hls_rename_temp_file(AVFormatContext *s, AVFormatContext *oc)
     return ret;
 }
 
-static const char* get_relative_url(const char *master_url, const char *media_url)
+static const char *get_relative_url(const char *master_url, const char *media_url)
 {
-    const char *p = strrchr(master_url, '/');
-    size_t base_len = 0;
-
-    if (!p) p = strrchr(master_url, '\\');
+    ptrdiff_t pos = get_last_separator_pos(master_url);
 
-    if (p) {
-        base_len = p - master_url;
-        if (av_strncasecmp(master_url, media_url, base_len)) {
+    if (pos >= 0) {
+        if (av_strncasecmp(master_url, media_url, pos)) {
             av_log(NULL, AV_LOG_WARNING, "Unable to find relative url\n");
             return NULL;
         }
@@ -1423,7 +1442,7 @@ static const char* get_relative_url(const char *master_url, const char *media_ur
         return media_url;
     }
 
-    return media_url + base_len + 1;
+    return media_url + pos + 1;
 }
 
 static int64_t get_stream_bit_rate(AVStream *stream)
@@ -3142,13 +3161,10 @@ static int hls_init(AVFormatContext *s)
                     vs->fmp4_init_filename = expanded;
                 }
 
-                p = strrchr(vs->m3u8_name, '/');
-                if (p) {
-                    char tmp = *(++p);
-                    *p = '\0';
-                    vs->base_output_dirname = av_asprintf("%s%s", vs->m3u8_name,
+                ptrdiff_t pos = get_last_separator_pos(vs->m3u8_name);
+                if (pos >= 0) {
+                    vs->base_output_dirname = av_asprintf("%.*s%s", pos + 1, vs->m3u8_name,
                                                           vs->fmp4_init_filename);
-                    *p = tmp;
                 } else {
                     vs->base_output_dirname = av_strdup(vs->fmp4_init_filename);
                 }

base-commit: f3bf5356966b4ad9334034e720fa224c2d9464a6
-- 
ffmpeg-codebot


More information about the ffmpeg-devel mailing list