[FFmpeg-devel] [PATCH 2/4] fftools/cmdutils: factorize loading a file from the datadir

Marton Balint cus at passwd.hu
Sat May 17 02:08:59 EEST 2025


This changes the order of preset file loading slighly to be in line with the
documentation, because before the patch codec-prefixed presets were probed
before trying the next directory.

Signed-off-by: Marton Balint <cus at passwd.hu>
---
 fftools/cmdutils.c | 46 ++++++++++++++++++++++++++++------------------
 1 file changed, 28 insertions(+), 18 deletions(-)

diff --git a/fftools/cmdutils.c b/fftools/cmdutils.c
index e516ee6ebd..1de670c2e4 100644
--- a/fftools/cmdutils.c
+++ b/fftools/cmdutils.c
@@ -920,9 +920,7 @@ int read_yesno(void)
     return yesno;
 }
 
-FILE *get_preset_file(AVBPrint *filename,
-                      const char *preset_name, int is_path,
-                      const char *codec_name)
+static FILE *get_datadir_file_fmt(AVBPrint *filename, const char *fmt, ...)
 {
     FILE *f = NULL;
     int i;
@@ -935,11 +933,6 @@ FILE *get_preset_file(AVBPrint *filename,
                             env_home,   /* index=1(HOME) is special: search in a .ffmpeg subfolder */
                             FFMPEG_DATADIR, };
 
-    if (is_path) {
-        av_bprintf(filename, "%s", preset_name);
-        if (av_bprint_is_complete(filename))
-            f = fopen_utf8(filename->str, "r");
-    } else {
 #if HAVE_GETMODULEHANDLE && defined(_WIN32)
         wchar_t *datadir_w = get_module_filename(NULL);
         base[2] = NULL;
@@ -969,21 +962,21 @@ FILE *get_preset_file(AVBPrint *filename,
             }
         }
 #endif
+
+    {
+        va_list ap;
+
         for (i = 0; i < 3 && !f; i++) {
             if (!base[i])
                 continue;
             av_bprint_clear(filename);
-            av_bprintf(filename, "%s%s/%s.ffpreset", base[i],
-                     i != 1 ? "" : "/.ffmpeg", preset_name);
+            av_bprintf(filename, "%s%s/", base[i],  i != 1 ? "" : "/.ffmpeg");
+            va_start(ap, fmt);
+            av_vbprintf(filename, fmt, ap);
+            va_end(ap);
+            if (!av_bprint_is_complete(filename))
+                break;
             f = fopen_utf8(filename->str, "r");
-            if (!f && codec_name) {
-                av_bprint_clear(filename);
-                av_bprintf(filename,
-                         "%s%s/%s-%s.ffpreset",
-                         base[i], i != 1 ? "" : "/.ffmpeg", codec_name,
-                         preset_name);
-                f = fopen_utf8(filename->str, "r");
-            }
         }
     }
 
@@ -995,6 +988,23 @@ FILE *get_preset_file(AVBPrint *filename,
     return f;
 }
 
+FILE *get_preset_file(AVBPrint *filename,
+                      const char *preset_name, int is_path,
+                      const char *codec_name)
+{
+    FILE *f = NULL;
+    if (is_path) {
+        av_bprintf(filename, "%s", preset_name);
+        if (av_bprint_is_complete(filename))
+            f = fopen_utf8(filename->str, "r");
+    } else {
+        f = get_datadir_file_fmt(filename, "%s.ffpreset", preset_name);
+        if (!f && codec_name)
+            f = get_datadir_file_fmt(filename, "%s%s/%s-%s.ffpreset", codec_name, preset_name);
+    }
+    return f;
+}
+
 int cmdutils_isalnum(char c)
 {
     return (c >= '0' && c <= '9') ||
-- 
2.43.0



More information about the ffmpeg-devel mailing list