1
0
mirror of https://github.com/mpv-player/mpv synced 2025-01-31 04:02:06 +00:00

sub: respect detected language for fuzzy-matched external subtitles

Solve this by passing through the language to the player, which then
uses the generic subtitle selection code to make a choice.

Fixes #367.
This commit is contained in:
wm4 2013-11-25 23:38:51 +01:00
parent 78fa766fcc
commit ce4d1f461a
3 changed files with 26 additions and 21 deletions

View File

@ -697,20 +697,23 @@ static void open_subtitles_from_options(struct MPContext *mpctx)
mp_add_subtitles(mpctx, mpctx->opts->sub_name[i]);
}
if (mpctx->opts->sub_auto) { // auto load sub file ...
char **tmp = find_text_subtitles(mpctx->opts, mpctx->filename);
int nsub = MP_TALLOC_ELEMS(tmp);
for (int i = 0; i < nsub; i++) {
char *filename = tmp[i];
struct subfn *list = find_text_subtitles(mpctx->opts, mpctx->filename);
for (int i = 0; list[i].fname; i++) {
char *filename = list[i].fname;
char *lang = list[i].lang;
for (int n = 0; n < mpctx->num_sources; n++) {
if (strcmp(mpctx->sources[n]->stream->url, filename) == 0)
goto skip;
}
struct track *track = mp_add_subtitles(mpctx, filename);
if (track)
if (track) {
track->auto_loaded = true;
if (!track->lang)
track->lang = talloc_strdup(track, lang);
}
skip:;
}
talloc_free(tmp);
talloc_free(list);
}
}

View File

@ -41,11 +41,6 @@ static struct bstr get_ext(struct bstr s)
return bstr_splice(s, dotpos + 1, s.len);
}
struct subfn {
int priority;
char *fname;
};
static int compare_sub_filename(const void *a, const void *b)
{
const struct subfn *s1 = a;
@ -135,14 +130,15 @@ static void append_dir_subtitles(struct MPOpts *opts,
// we have a (likely) subtitle file
int prio = 0;
char *found_lang = NULL;
if (opts->sub_lang) {
if (bstr_startswith(tmp_fname_trim, f_fname_trim)) {
struct bstr lang = guess_lang_from_filename(tmp_fname_trim);
if (lang.len) {
for (int n = 0; opts->sub_lang[n]; n++) {
if (bstr_startswith(lang,
bstr0(opts->sub_lang[n]))) {
if (bstr_startswith0(lang, opts->sub_lang[n])) {
prio = 4; // matches the movie name + lang extension
found_lang = opts->sub_lang[n];
break;
}
}
@ -177,6 +173,7 @@ static void append_dir_subtitles(struct MPOpts *opts,
sub->priority = prio;
sub->fname = subpath;
sub->lang = found_lang;
} else
talloc_free(subpath);
}
@ -217,9 +214,10 @@ static void filter_subidx(struct subfn **slist, int *nsub)
}
}
char **find_text_subtitles(struct MPOpts *opts, const char *fname)
// Return a list of subtitles found, sorted by priority.
// Last element is terminated with a fname==NULL entry.
struct subfn *find_text_subtitles(struct MPOpts *opts, const char *fname)
{
char **subnames = NULL;
struct subfn *slist = talloc_array_ptrtype(NULL, slist, 1);
int n = 0;
@ -249,10 +247,8 @@ char **find_text_subtitles(struct MPOpts *opts, const char *fname)
// Sort subs by priority and append them
qsort(slist, n, sizeof(*slist), compare_sub_priority);
subnames = talloc_array_ptrtype(NULL, subnames, n);
for (int i = 0; i < n; i++)
subnames[i] = talloc_strdup(subnames, slist[i].fname);
struct subfn z = {0};
MP_TARRAY_APPEND(NULL, slist, n, z);
talloc_free(slist);
return subnames;
return slist;
}

View File

@ -21,6 +21,12 @@
struct MPOpts;
char **find_text_subtitles(struct MPOpts *opts, const char *fname);
struct subfn {
int priority;
char *fname;
char *lang;
};
struct subfn *find_text_subtitles(struct MPOpts *opts, const char *fname);
#endif /* MPLAYER_FINDFILES_H */