summaryrefslogtreecommitdiffstats
path: root/player/loadfile.c
diff options
context:
space:
mode:
authorDudemanguy <random342@airmail.cc>2023-08-06 23:49:37 -0500
committerDudemanguy <random342@airmail.cc>2023-08-28 18:43:46 +0000
commit58ec0630f545705f0300805a43ee24aa0dcb59d6 (patch)
tree515e7c57a4e68e5efdfedbe0b7e8fc7e635da52e /player/loadfile.c
parentfbe8f9919428a7ed24a61899bfd85bbb7680e389 (diff)
downloadmpv-58ec0630f545705f0300805a43ee24aa0dcb59d6.tar.bz2
mpv-58ec0630f545705f0300805a43ee24aa0dcb59d6.tar.xz
player: add --subs-match-os-language option
This is the replacement for the previous auto option for slang. It behaves similar however it never overrides slang if that is set and will instead try to pick the subtitle that matches the user's language if appropriately flagged by the file.
Diffstat (limited to 'player/loadfile.c')
-rw-r--r--player/loadfile.c32
1 files changed, 21 insertions, 11 deletions
diff --git a/player/loadfile.c b/player/loadfile.c
index 3b0dcb1a39..2a83f57241 100644
--- a/player/loadfile.c
+++ b/player/loadfile.c
@@ -472,10 +472,11 @@ static int match_lang(char **langs, const char *lang)
* 1) track is external (no_default cancels this)
* 1b) track was passed explicitly (is not an auto-loaded subtitle)
* 1c) track matches the program ID of the video
- * 2) earlier match in lang list
+ * 2) earlier match in lang list but not if we're using os_langs
* 3a) track is marked forced and we're preferring forced tracks
* 3b) track is marked non-forced and we're preferring non-forced tracks
* 3c) track is marked default
+ * 3d) match in lang list with os_langs
* 4) attached picture, HLS bitrate
* 5) lower track number
* If select_fallback is not set, 5) is only used to determine whether a
@@ -485,7 +486,7 @@ static int match_lang(char **langs, const char *lang)
*/
// Return whether t1 is preferred over t2
static bool compare_track(struct track *t1, struct track *t2, char **langs,
- struct MPOpts *opts, int preferred_program)
+ bool os_langs, struct MPOpts *opts, int preferred_program)
{
if (!opts->autoload_files && t1->is_external != t2->is_external)
return !t1->is_external;
@@ -505,10 +506,12 @@ static bool compare_track(struct track *t1, struct track *t2, char **langs,
return t1->program_id == preferred_program;
}
int l1 = match_lang(langs, t1->lang), l2 = match_lang(langs, t2->lang);
- if (l1 != l2)
+ if (!os_langs && l1 != l2)
return l1 > l2;
if (t1->default_track != t2->default_track)
return t1->default_track;
+ if (os_langs && l1 != l2)
+ return l1 > l2;
if (t1->attached_picture != t2->attached_picture)
return !t1->attached_picture;
if (t1->stream && t2->stream && opts->hls_bitrate >= 0 &&
@@ -546,19 +549,19 @@ static bool append_lang(size_t *nb, char ***out, char *in)
return true;
}
-static bool add_auto_langs(size_t *nb, char ***out)
+static char **add_os_langs(void)
{
- bool ret = false;
+ size_t nb = 0;
+ char **out = NULL;
char **autos = mp_get_user_langs();
for (int i = 0; autos && autos[i]; i++) {
- if (!append_lang(nb, out, autos[i]))
+ if (!append_lang(&nb, &out, autos[i]))
goto cleanup;
}
- ret = true;
cleanup:
talloc_free(autos);
- return ret;
+ return out;
}
static char **process_langs(char **in)
@@ -611,6 +614,13 @@ struct track *select_default_track(struct MPContext *mpctx, int order,
if (tid == -2)
return NULL;
char **langs = process_langs(opts->stream_lang[type]);
+ bool os_langs = false;
+ // Try to add OS languages if enabled by the user and we don't already have a lang from slang.
+ if (type == STREAM_SUB && (!langs || !strcmp(langs[0], "")) && opts->subs_match_os_language) {
+ talloc_free(langs);
+ langs = add_os_langs();
+ os_langs = true;
+ }
const char *audio_lang = get_audio_lang(mpctx);
bool sub = type == STREAM_SUB;
bool fallback_forced = sub && opts->subs_fallback_forced;
@@ -633,12 +643,12 @@ struct track *select_default_track(struct MPContext *mpctx, int order,
continue;
if (duplicate_track(mpctx, order, type, track))
continue;
- if (!pick || compare_track(track, pick, langs, mpctx->opts, preferred_program))
+ if (!pick || compare_track(track, pick, langs, os_langs, mpctx->opts, preferred_program))
pick = track;
// We only try to autoselect forced tracks if they match the audio language and are subs
if (fallback_forced && track->forced_track && mp_match_lang_single(audio_lang, track->lang) &&
- (!forced_pick || compare_track(track, forced_pick, langs, mpctx->opts, preferred_program)))
+ (!forced_pick || compare_track(track, forced_pick, langs, os_langs, mpctx->opts, preferred_program)))
forced_pick = track;
}
@@ -652,7 +662,7 @@ struct track *select_default_track(struct MPContext *mpctx, int order,
sub_fallback = (pick->is_external && !pick->no_default) || opts->subs_fallback == 2 ||
(opts->subs_fallback == 1 && pick->default_track);
}
- if (pick && !forced_pick && sub && !match_lang(langs, pick->lang) &&
+ if (pick && !forced_pick && sub && (!match_lang(langs, pick->lang) || os_langs) &&
((!opts->subs_with_matching_audio && audio_matches) || !sub_fallback))
pick = NULL;