From 32be72623b1d43027f2d97c15fcf604d8c328e00 Mon Sep 17 00:00:00 2001 From: Dudemanguy Date: Thu, 10 Aug 2023 17:36:22 -0500 Subject: [PATCH] player: make all autoload extensions configurable --audio-file-auto, --cover-art-auto, and --sub-auto all work by using an internally hardcoded list that determine what file extensions get recognized. This is fine and people periodically update it, but we can actually expose this as a stringlist option instead. This way users can add or remove any file extension for any type. For the most part, this is pretty pretty easy and involves making sub_exts, etc. the defaults for the new options (--audio-file-auto-exts, --cover-art-auto-exts, and --sub-auto-exts). There's actually one slight complication however. The input code uses mp_might_be_subtitle_file which guesses if the file drag and dropped file is a subtitle. The input ctx has no access to mpctx so we have to be clever here. For this, the trick is to recognize that we can leverage the m_option_change_callback. We add a new flag, UPDATE_SUB_EXTS, which fires when the player starts up. Then in the callback, we can set the value of sub_exts in external_files to opts->sub_auto_exts. Whenever the option updates, the callback is fired again and sub_exts updates. That way mp_might_be_subtitle_file can just operate off of this global variable instead of trying to mess with the core mpv state directly. Fixes #12000. --- DOCS/interface-changes.rst | 1 + DOCS/man/options.rst | 17 ++++++++++++ options/m_option.h | 3 ++- options/options.c | 55 ++++++++++++++++++++++++++++++++++++++ options/options.h | 3 +++ player/command.c | 4 +++ player/external_files.c | 39 +++++++++++++-------------- player/external_files.h | 1 + 8 files changed, 101 insertions(+), 22 deletions(-) diff --git a/DOCS/interface-changes.rst b/DOCS/interface-changes.rst index cef7d4d55b..538439ec28 100644 --- a/DOCS/interface-changes.rst +++ b/DOCS/interface-changes.rst @@ -42,6 +42,7 @@ Interface changes - add `--x11-wid-title` option - add `--libplacebo-opts` option - change `--video-pan-x/y` to be relative to the destination rectangle + - add `--audio-file-exts`, `--cover-art-auto-exts`, and `--sub-auto-exts` --- mpv 0.36.0 --- - add `--target-contrast` - Target luminance value is now also applied when ICC profile is used. diff --git a/DOCS/man/options.rst b/DOCS/man/options.rst index bbdcb7cd6e..23692a672f 100644 --- a/DOCS/man/options.rst +++ b/DOCS/man/options.rst @@ -2187,6 +2187,11 @@ Audio :all: Load all audio files in the current and ``--audio-file-paths`` directories. +``--audio-file-auto-exts=ext1,ext2,...`` + Audio file extentions to try and match when using ``audio-file-auto``. + + This is a string list option. See `List Options`_ for details. + ``--audio-file-paths=`` Equivalent to ``--sub-file-paths`` option, but for auto-loaded audio files. @@ -2566,6 +2571,13 @@ Subtitles :fuzzy: Load all subs containing the media filename. :all: Load all subs in the current and ``--sub-file-paths`` directories. +``--sub-auto-exts=ext1,ext2,...`` + Subtitle extentions to try and match when using ``--sub-auto``. Note that + modifying this list will also affect what mpv recognizes as subtitles when + using drag and drop. + + This is a string list option. See `List Options`_ for details. + ``--sub-codepage=`` You can use this option to specify the subtitle codepage. uchardet will be used to guess the charset. (If mpv was not compiled with uchardet, then @@ -7191,6 +7203,11 @@ Miscellaneous See ``--audio-display`` how to control display of cover art (this can be used to disable cover art that is part of the file). +``--cover-art-auto-exts=ext1,ext2,...`` + Cover art extentions to try and match when using ``cover-art-auto``. + + This is a string list option. See `List Options`_ for details. + ``--cover-art-whitelist=`` Whether to load filenames in an internal whitelist, such as ``cover.jpg``, as cover art. If ``cover-art-auto`` is set to ``no``, the whitelisted diff --git a/options/m_option.h b/options/m_option.h index 1adac938e4..c868487b3e 100644 --- a/options/m_option.h +++ b/options/m_option.h @@ -433,7 +433,8 @@ char *format_file_size(int64_t size); #define UPDATE_HWDEC (1 << 20) // --hwdec #define UPDATE_DVB_PROG (1 << 21) // some --dvbin-... #define UPDATE_SUB_HARD (1 << 22) // subtitle opts. that need full reinit -#define UPDATE_OPT_LAST (1 << 22) +#define UPDATE_SUB_EXTS (1 << 23) // update internal list of sub exts +#define UPDATE_OPT_LAST (1 << 23) // All bits between _FIRST and _LAST (inclusive) #define UPDATE_OPTS_MASK \ diff --git a/options/options.c b/options/options.c index 7d00537b28..7b9156a823 100644 --- a/options/options.c +++ b/options/options.c @@ -615,10 +615,13 @@ static const m_option_t mp_opts[] = { {"sub-auto", OPT_CHOICE(sub_auto, {"no", -1}, {"exact", 0}, {"fuzzy", 1}, {"all", 2})}, + {"sub-auto-exts", OPT_STRINGLIST(sub_auto_exts), .flags = UPDATE_SUB_EXTS}, {"audio-file-auto", OPT_CHOICE(audiofile_auto, {"no", -1}, {"exact", 0}, {"fuzzy", 1}, {"all", 2})}, + {"audio-file-auto-exts", OPT_STRINGLIST(audiofile_auto_exts)}, {"cover-art-auto", OPT_CHOICE(coverart_auto, {"no", -1}, {"exact", 0}, {"fuzzy", 1}, {"all", 2})}, + {"cover-art-auto-exts", OPT_STRINGLIST(coverart_auto_exts)}, {"cover-art-whitelist", OPT_BOOL(coverart_whitelist)}, {"", OPT_SUBSTRUCT(subs_rend, mp_subtitle_sub_opts)}, @@ -1056,6 +1059,58 @@ static const struct MPOpts mp_default_opts = { .screenshot_template = "mpv-shot%n", .play_dir = 1, + .audiofile_auto_exts = (char *[]){ + "aac", + "ac3", + "dts", + "eac3", + "flac", + "m4a", + "mka", + "mp3", + "ogg", + "opus", + "thd", + "wav", + "wv", + NULL + }, + + .coverart_auto_exts = (char *[]){ + "avif", + "bmp", + "gif", + "jpeg", + "jpg", + "jxl", + "png", + "tif", + "tiff", + "webp", + NULL + }, + + .sub_auto_exts = (char *[]){ + "ass", + "idx", + "lrc", + "mks", + "pgs", + "rt", + "sbv", + "scc", + "smi", + "srt", + "ssa", + "sub", + "sup", + "utf", + "utf-8", + "utf8", + "vtt", + NULL + }, + .audio_output_channels = { .set = 1, .auto_safe = 1, diff --git a/options/options.h b/options/options.h index fac88dd939..6f8c8a5998 100644 --- a/options/options.h +++ b/options/options.h @@ -309,8 +309,11 @@ typedef struct MPOpts { char **external_files; bool autoload_files; int sub_auto; + char **sub_auto_exts; int audiofile_auto; + char **audiofile_auto_exts; int coverart_auto; + char **coverart_auto_exts; bool coverart_whitelist; bool osd_bar_visible; diff --git a/player/command.c b/player/command.c index a00b42ca8c..0a45469308 100644 --- a/player/command.c +++ b/player/command.c @@ -33,6 +33,7 @@ #include "mpv_talloc.h" #include "client.h" +#include "external_files.h" #include "common/av_common.h" #include "common/codecs.h" #include "common/msg.h" @@ -6946,6 +6947,9 @@ void mp_option_change_callback(void *ctx, struct m_config_option *co, int flags, if (flags & UPDATE_INPUT) mp_input_update_opts(mpctx->input); + if (flags & UPDATE_SUB_EXTS) + mp_update_subtitle_exts(mpctx->opts); + if (init || opt_ptr == &opts->ipc_path || opt_ptr == &opts->ipc_client) { mp_uninit_ipc(mpctx->ipc_ctx); mpctx->ipc_ctx = mp_init_ipc(mpctx->clients, mpctx->global); diff --git a/player/external_files.c b/player/external_files.c index 451f05bcfb..000fafc82c 100644 --- a/player/external_files.c +++ b/player/external_files.c @@ -32,20 +32,6 @@ #include "options/path.h" #include "external_files.h" -static const char *const sub_exts[] = {"ass", "idx", "lrc", "mks", "pgs", "rt", - "sbv", "scc", "smi", "srt", "ssa", "sub", - "sup", "utf", "utf-8", "utf8", "vtt", - NULL}; - -static const char *const audio_exts[] = {"aac", "ac3", "dts", "eac3", "flac", - "m4a", "mka", "mp3", "ogg", "opus", - "thd", "wav", "wv", - NULL}; - -static const char *const image_exts[] = {"avif", "bmp", "gif", "jpeg", "jpg", - "jxl", "png", "tif", "tiff", "webp", - NULL}; - // Stolen from: vlc/-/blob/master/modules/meta_engine/folder.c#L40 // sorted by priority (descending) static const char *const cover_files[] = { @@ -92,22 +78,28 @@ static const char *const cover_files[] = { NULL }; -static bool test_ext_list(bstr ext, const char *const *list) +// Needed for mp_might_be_subtitle_file +char **sub_exts; + +static bool test_ext_list(bstr ext, char **list) { + if (!list) + goto done; for (int n = 0; list[n]; n++) { if (bstrcasecmp(bstr0(list[n]), ext) == 0) return true; } +done: return false; } -static int test_ext(bstr ext) +static int test_ext(MPOpts *opts, bstr ext) { - if (test_ext_list(ext, sub_exts)) + if (test_ext_list(ext, opts->sub_auto_exts)) return STREAM_SUB; - if (test_ext_list(ext, audio_exts)) + if (test_ext_list(ext, opts->audiofile_auto_exts)) return STREAM_AUDIO; - if (test_ext_list(ext, image_exts)) + if (test_ext_list(ext, opts->coverart_auto_exts)) return STREAM_VIDEO; return -1; } @@ -124,7 +116,12 @@ static int test_cover_filename(bstr fname) bool mp_might_be_subtitle_file(const char *filename) { - return test_ext(bstr_get_ext(bstr0(filename))) == STREAM_SUB; + return test_ext_list(bstr_get_ext(bstr0(filename)), sub_exts); +} + +void mp_update_subtitle_exts(struct MPOpts *opts) +{ + sub_exts = opts->sub_auto_exts; } static int compare_sub_filename(const void *a, const void *b) @@ -220,7 +217,7 @@ static void append_dir_subtitles(struct mpv_global *global, struct MPOpts *opts, talloc_steal(tmpmem2, dename.start); // check what it is (most likely) - int type = test_ext(tmp_fname_ext); + int type = test_ext(opts, tmp_fname_ext); char **langs = NULL; int fuzz = -1; switch (type) { diff --git a/player/external_files.h b/player/external_files.h index e64f249c6d..20b37c3705 100644 --- a/player/external_files.h +++ b/player/external_files.h @@ -33,5 +33,6 @@ struct subfn *find_external_files(struct mpv_global *global, const char *fname, struct MPOpts *opts); bool mp_might_be_subtitle_file(const char *filename); +void mp_update_subtitle_exts(struct MPOpts *opts); #endif /* MPLAYER_FINDFILES_H */