2017-04-21 10:34:41 +00:00
|
|
|
/*
|
|
|
|
* This file is part of mpv.
|
|
|
|
*
|
2017-06-20 11:09:28 +00:00
|
|
|
* mpv is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2017-04-21 10:34:41 +00:00
|
|
|
*
|
|
|
|
* mpv is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2017-06-20 11:09:28 +00:00
|
|
|
* GNU Lesser General Public License for more details.
|
2017-04-21 10:34:41 +00:00
|
|
|
*
|
2017-06-20 11:09:28 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
|
2017-04-21 10:34:41 +00:00
|
|
|
*/
|
|
|
|
|
2011-02-26 19:47:49 +00:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <string.h>
|
2014-07-10 06:28:03 +00:00
|
|
|
#include <strings.h>
|
2011-02-26 19:47:49 +00:00
|
|
|
#include <stdlib.h>
|
2011-04-19 03:53:56 +00:00
|
|
|
#include <assert.h>
|
2011-02-26 19:47:49 +00:00
|
|
|
|
2012-02-03 07:05:11 +00:00
|
|
|
#include "osdep/io.h"
|
|
|
|
|
2014-07-01 21:10:38 +00:00
|
|
|
#include "common/common.h"
|
2013-12-21 18:02:05 +00:00
|
|
|
#include "common/global.h"
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/msg.h"
|
2014-07-01 21:10:38 +00:00
|
|
|
#include "misc/ctype.h"
|
2017-01-24 18:48:02 +00:00
|
|
|
#include "misc/charset_conv.h"
|
2013-12-17 01:02:25 +00:00
|
|
|
#include "options/options.h"
|
|
|
|
#include "options/path.h"
|
2015-09-20 16:05:06 +00:00
|
|
|
#include "external_files.h"
|
2011-02-26 19:47:49 +00:00
|
|
|
|
2017-06-20 11:09:28 +00:00
|
|
|
static const char *const sub_exts[] = {"utf", "utf8", "utf-8", "idx", "sub",
|
|
|
|
"srt", "rt", "ssa", "ass", "mks", "vtt",
|
2019-08-29 20:46:09 +00:00
|
|
|
"sup", "scc", "smi", "lrc",
|
2017-01-31 13:50:58 +00:00
|
|
|
NULL};
|
2013-09-07 18:31:00 +00:00
|
|
|
|
2015-02-02 20:23:12 +00:00
|
|
|
static const char *const audio_exts[] = {"mp3", "aac", "mka", "dts", "flac",
|
2015-10-04 17:48:58 +00:00
|
|
|
"ogg", "m4a", "ac3", "opus", "wav",
|
2016-03-26 17:24:07 +00:00
|
|
|
"wv",
|
2015-10-04 17:48:58 +00:00
|
|
|
NULL};
|
2015-02-02 20:23:12 +00:00
|
|
|
|
|
|
|
static bool test_ext_list(bstr ext, const char *const *list)
|
2013-09-07 18:31:00 +00:00
|
|
|
{
|
2015-02-02 20:23:12 +00:00
|
|
|
for (int n = 0; list[n]; n++) {
|
|
|
|
if (bstrcasecmp(bstr0(list[n]), ext) == 0)
|
2013-09-07 18:31:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-02-02 20:23:12 +00:00
|
|
|
static int test_ext(bstr ext)
|
|
|
|
{
|
|
|
|
if (test_ext_list(ext, sub_exts))
|
|
|
|
return STREAM_SUB;
|
|
|
|
if (test_ext_list(ext, audio_exts))
|
|
|
|
return STREAM_AUDIO;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-01-04 00:27:06 +00:00
|
|
|
bool mp_might_be_subtitle_file(const char *filename)
|
|
|
|
{
|
2015-11-07 07:40:34 +00:00
|
|
|
return test_ext(bstr_get_ext(bstr0(filename))) == STREAM_SUB;
|
2014-01-04 00:27:06 +00:00
|
|
|
}
|
|
|
|
|
2013-09-07 18:31:00 +00:00
|
|
|
static int compare_sub_filename(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
const struct subfn *s1 = a;
|
|
|
|
const struct subfn *s2 = b;
|
|
|
|
return strcoll(s1->fname, s2->fname);
|
|
|
|
}
|
|
|
|
|
2011-02-26 19:47:49 +00:00
|
|
|
static int compare_sub_priority(const void *a, const void *b)
|
|
|
|
{
|
2011-03-03 19:05:17 +00:00
|
|
|
const struct subfn *s1 = a;
|
|
|
|
const struct subfn *s2 = b;
|
|
|
|
if (s1->priority > s2->priority)
|
2011-02-26 19:47:49 +00:00
|
|
|
return -1;
|
2011-03-03 19:05:17 +00:00
|
|
|
if (s1->priority < s2->priority)
|
2011-02-26 19:47:49 +00:00
|
|
|
return 1;
|
2011-03-03 19:05:17 +00:00
|
|
|
return strcoll(s1->fname, s2->fname);
|
2011-02-26 19:47:49 +00:00
|
|
|
}
|
|
|
|
|
2011-04-19 03:53:56 +00:00
|
|
|
static struct bstr guess_lang_from_filename(struct bstr name)
|
2011-03-03 18:59:15 +00:00
|
|
|
{
|
2011-04-19 03:53:56 +00:00
|
|
|
if (name.len < 2)
|
|
|
|
return (struct bstr){NULL, 0};
|
|
|
|
|
|
|
|
int n = 0;
|
|
|
|
int i = name.len - 1;
|
2011-03-03 18:59:15 +00:00
|
|
|
|
2011-04-19 03:53:56 +00:00
|
|
|
if (name.start[i] == ')' || name.start[i] == ']')
|
2011-03-03 18:59:15 +00:00
|
|
|
i--;
|
2014-07-01 21:10:38 +00:00
|
|
|
while (i >= 0 && mp_isalpha(name.start[i])) {
|
2011-03-03 18:59:15 +00:00
|
|
|
n++;
|
|
|
|
if (n > 3)
|
2011-04-19 03:53:56 +00:00
|
|
|
return (struct bstr){NULL, 0};
|
2011-03-03 18:59:15 +00:00
|
|
|
i--;
|
|
|
|
}
|
|
|
|
if (n < 2)
|
2011-04-19 03:53:56 +00:00
|
|
|
return (struct bstr){NULL, 0};
|
|
|
|
return (struct bstr){name.start + i + 1, n};
|
2011-03-03 18:59:15 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 14:25:52 +00:00
|
|
|
static void append_dir_subtitles(struct mpv_global *global, struct MPOpts *opts,
|
2011-03-03 18:59:15 +00:00
|
|
|
struct subfn **slist, int *nsub,
|
2011-03-01 21:42:22 +00:00
|
|
|
struct bstr path, const char *fname,
|
2015-12-25 12:40:06 +00:00
|
|
|
int limit_fuzziness, int limit_type)
|
2011-02-26 19:47:49 +00:00
|
|
|
{
|
2011-04-19 03:53:56 +00:00
|
|
|
void *tmpmem = talloc_new(NULL);
|
2015-02-02 20:23:12 +00:00
|
|
|
struct mp_log *log = mp_log_new(tmpmem, global->log, "find_files");
|
2011-02-26 19:47:49 +00:00
|
|
|
|
2017-01-24 18:48:02 +00:00
|
|
|
struct bstr f_fbname = bstr0(mp_basename(fname));
|
|
|
|
struct bstr f_fname = mp_iconv_to_utf8(log, f_fbname,
|
|
|
|
"UTF-8-MAC", MP_NO_LATIN1_FALLBACK);
|
2015-11-07 07:40:34 +00:00
|
|
|
struct bstr f_fname_noext = bstrdup(tmpmem, bstr_strip_ext(f_fname));
|
2011-04-19 03:53:56 +00:00
|
|
|
bstr_lower(f_fname_noext);
|
|
|
|
struct bstr f_fname_trim = bstr_strip(f_fname_noext);
|
2011-02-26 19:47:49 +00:00
|
|
|
|
2017-01-24 18:48:02 +00:00
|
|
|
if (f_fbname.start != f_fname.start)
|
|
|
|
talloc_steal(tmpmem, f_fname.start);
|
|
|
|
|
2011-04-19 03:53:56 +00:00
|
|
|
char *path0 = bstrdup0(tmpmem, path);
|
2017-03-31 18:48:06 +00:00
|
|
|
|
|
|
|
if (mp_is_url(bstr0(path0)))
|
|
|
|
goto out;
|
|
|
|
|
2011-03-03 19:05:17 +00:00
|
|
|
DIR *d = opendir(path0);
|
2011-04-19 03:53:56 +00:00
|
|
|
if (!d)
|
|
|
|
goto out;
|
2015-02-02 20:23:12 +00:00
|
|
|
mp_verbose(log, "Loading external files in %.*s\n", BSTR_P(path));
|
2011-04-19 03:53:56 +00:00
|
|
|
struct dirent *de;
|
|
|
|
while ((de = readdir(d))) {
|
|
|
|
void *tmpmem2 = talloc_new(tmpmem);
|
2017-01-24 18:48:02 +00:00
|
|
|
struct bstr den = bstr0(de->d_name);
|
|
|
|
struct bstr dename = mp_iconv_to_utf8(log, den,
|
|
|
|
"UTF-8-MAC", MP_NO_LATIN1_FALLBACK);
|
2011-04-19 03:53:56 +00:00
|
|
|
// retrieve various parts of the filename
|
2015-11-07 07:40:34 +00:00
|
|
|
struct bstr tmp_fname_noext = bstrdup(tmpmem2, bstr_strip_ext(dename));
|
2011-04-19 03:53:56 +00:00
|
|
|
bstr_lower(tmp_fname_noext);
|
2015-11-07 07:40:34 +00:00
|
|
|
struct bstr tmp_fname_ext = bstr_get_ext(dename);
|
2011-04-19 03:53:56 +00:00
|
|
|
struct bstr tmp_fname_trim = bstr_strip(tmp_fname_noext);
|
|
|
|
|
2017-01-24 18:48:02 +00:00
|
|
|
if (den.start != dename.start)
|
|
|
|
talloc_steal(tmpmem2, dename.start);
|
|
|
|
|
2015-02-02 20:23:12 +00:00
|
|
|
// check what it is (most likely)
|
|
|
|
int type = test_ext(tmp_fname_ext);
|
|
|
|
char **langs = NULL;
|
|
|
|
int fuzz = -1;
|
|
|
|
switch (type) {
|
|
|
|
case STREAM_SUB:
|
2015-05-22 19:00:24 +00:00
|
|
|
langs = opts->stream_lang[type];
|
2015-02-02 20:23:12 +00:00
|
|
|
fuzz = opts->sub_auto;
|
|
|
|
break;
|
|
|
|
case STREAM_AUDIO:
|
2015-05-22 19:00:24 +00:00
|
|
|
langs = opts->stream_lang[type];
|
2015-02-02 20:23:12 +00:00
|
|
|
fuzz = opts->audiofile_auto;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-12-25 12:40:06 +00:00
|
|
|
if (fuzz < 0 || (limit_type >= 0 && limit_type != type))
|
2013-09-07 18:31:00 +00:00
|
|
|
goto next_sub;
|
2011-03-03 18:59:15 +00:00
|
|
|
|
2011-04-19 03:53:56 +00:00
|
|
|
// we have a (likely) subtitle file
|
2017-11-27 18:45:13 +00:00
|
|
|
// 0 = nothing
|
|
|
|
// 1 = any subtitle file
|
|
|
|
// 2 = any sub file containing movie name
|
|
|
|
// 3 = sub file containing movie name and the lang extension
|
2011-04-19 03:53:56 +00:00
|
|
|
int prio = 0;
|
2017-11-27 18:45:13 +00:00
|
|
|
|
|
|
|
bstr lang = {0};
|
|
|
|
if (bstr_startswith(tmp_fname_trim, f_fname_trim))
|
|
|
|
lang = guess_lang_from_filename(tmp_fname_trim);
|
|
|
|
for (int n = 0; langs && langs[n]; n++) {
|
2017-12-23 20:18:13 +00:00
|
|
|
if (lang.len && bstr_case_startswith(lang, bstr0(langs[n]))) {
|
2017-11-27 18:45:13 +00:00
|
|
|
prio = 4; // matches the movie name + lang extension
|
|
|
|
break;
|
2011-04-19 03:53:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!prio && bstrcmp(tmp_fname_trim, f_fname_trim) == 0)
|
|
|
|
prio = 3; // matches the movie name
|
2017-11-27 18:45:13 +00:00
|
|
|
if (!prio && lang.len)
|
|
|
|
prio = 3; // matches the movie name + a language was matched
|
2015-02-02 20:23:12 +00:00
|
|
|
if (!prio && bstr_find(tmp_fname_trim, f_fname_trim) >= 0 && fuzz >= 1)
|
2011-04-19 03:53:56 +00:00
|
|
|
prio = 2; // contains the movie name
|
|
|
|
if (!prio) {
|
|
|
|
// doesn't contain the movie name
|
|
|
|
// don't try in the mplayer subtitle directory
|
2015-02-02 20:23:12 +00:00
|
|
|
if (!limit_fuzziness && fuzz >= 2) {
|
2011-04-19 03:53:56 +00:00
|
|
|
prio = 1;
|
|
|
|
}
|
|
|
|
}
|
2011-02-26 19:47:49 +00:00
|
|
|
|
2015-02-02 20:23:12 +00:00
|
|
|
mp_dbg(log, "Potential external file: \"%s\" Priority: %d\n",
|
|
|
|
de->d_name, prio);
|
|
|
|
|
2011-04-19 03:53:56 +00:00
|
|
|
if (prio) {
|
|
|
|
prio += prio;
|
2015-05-09 13:26:47 +00:00
|
|
|
char *subpath = mp_path_join_bstr(*slist, path, dename);
|
2013-09-07 18:24:02 +00:00
|
|
|
if (mp_path_exists(subpath)) {
|
2016-05-17 08:53:14 +00:00
|
|
|
MP_TARRAY_GROW(NULL, *slist, *nsub);
|
2011-04-19 03:53:56 +00:00
|
|
|
struct subfn *sub = *slist + (*nsub)++;
|
|
|
|
|
2013-09-07 18:33:47 +00:00
|
|
|
// annoying and redundant
|
|
|
|
if (strncmp(subpath, "./", 2) == 0)
|
|
|
|
subpath += 2;
|
|
|
|
|
2015-02-02 20:23:12 +00:00
|
|
|
sub->type = type;
|
2011-04-19 03:53:56 +00:00
|
|
|
sub->priority = prio;
|
|
|
|
sub->fname = subpath;
|
2017-11-27 18:45:13 +00:00
|
|
|
sub->lang = lang.len ? bstrdup0(*slist, lang) : NULL;
|
2011-04-19 03:53:56 +00:00
|
|
|
} else
|
|
|
|
talloc_free(subpath);
|
2011-02-26 19:47:49 +00:00
|
|
|
}
|
2011-04-19 03:53:56 +00:00
|
|
|
|
|
|
|
next_sub:
|
|
|
|
talloc_free(tmpmem2);
|
2011-02-26 19:47:49 +00:00
|
|
|
}
|
2011-04-19 03:53:56 +00:00
|
|
|
closedir(d);
|
2011-02-26 19:47:49 +00:00
|
|
|
|
2011-04-19 03:53:56 +00:00
|
|
|
out:
|
|
|
|
talloc_free(tmpmem);
|
2011-03-01 21:42:22 +00:00
|
|
|
}
|
2011-02-26 19:47:49 +00:00
|
|
|
|
2013-09-07 18:31:00 +00:00
|
|
|
static bool case_endswith(const char *s, const char *end)
|
|
|
|
{
|
|
|
|
size_t len = strlen(s);
|
|
|
|
size_t elen = strlen(end);
|
|
|
|
return len >= elen && strcasecmp(s + len - elen, end) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Drop .sub file if .idx file exists.
|
|
|
|
// Assumes slist is sorted by compare_sub_filename.
|
|
|
|
static void filter_subidx(struct subfn **slist, int *nsub)
|
|
|
|
{
|
|
|
|
const char *prev = NULL;
|
|
|
|
for (int n = 0; n < *nsub; n++) {
|
|
|
|
const char *fname = (*slist)[n].fname;
|
|
|
|
if (case_endswith(fname, ".idx")) {
|
|
|
|
prev = fname;
|
|
|
|
} else if (case_endswith(fname, ".sub")) {
|
2013-09-17 13:33:12 +00:00
|
|
|
if (prev && strncmp(prev, fname, strlen(fname) - 4) == 0)
|
2013-09-07 18:31:00 +00:00
|
|
|
(*slist)[n].priority = -1;
|
|
|
|
}
|
|
|
|
}
|
2013-09-08 05:24:30 +00:00
|
|
|
for (int n = *nsub - 1; n >= 0; n--) {
|
2013-09-07 18:31:00 +00:00
|
|
|
if ((*slist)[n].priority < 0)
|
|
|
|
MP_TARRAY_REMOVE_AT(*slist, *nsub, n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-21 14:25:52 +00:00
|
|
|
static void load_paths(struct mpv_global *global, struct MPOpts *opts,
|
|
|
|
struct subfn **slist, int *nsubs, const char *fname,
|
|
|
|
char **paths, char *cfg_path, int type)
|
2015-12-25 12:17:11 +00:00
|
|
|
{
|
|
|
|
for (int i = 0; paths && paths[i]; i++) {
|
2017-05-30 15:07:59 +00:00
|
|
|
char *expanded_path = mp_get_user_path(NULL, global, paths[i]);
|
|
|
|
char *path = mp_path_join_bstr(
|
|
|
|
*slist, mp_dirname(fname),
|
|
|
|
bstr0(expanded_path ? expanded_path : paths[i]));
|
2018-05-21 14:25:52 +00:00
|
|
|
append_dir_subtitles(global, opts, slist, nsubs, bstr0(path),
|
2017-05-30 15:07:59 +00:00
|
|
|
fname, 0, type);
|
|
|
|
talloc_free(expanded_path);
|
2015-12-25 12:17:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Load subtitles in ~/.mpv/sub (or similar) limiting sub fuzziness
|
|
|
|
char *mp_subdir = mp_find_config_file(NULL, global, cfg_path);
|
2015-12-25 12:40:06 +00:00
|
|
|
if (mp_subdir) {
|
2018-05-21 14:25:52 +00:00
|
|
|
append_dir_subtitles(global, opts, slist, nsubs, bstr0(mp_subdir),
|
|
|
|
fname, 1, type);
|
2015-12-25 12:40:06 +00:00
|
|
|
}
|
2015-12-25 12:17:11 +00:00
|
|
|
talloc_free(mp_subdir);
|
|
|
|
}
|
|
|
|
|
2015-02-02 20:23:12 +00:00
|
|
|
// Return a list of subtitles and audio files found, sorted by priority.
|
2013-11-25 22:38:51 +00:00
|
|
|
// Last element is terminated with a fname==NULL entry.
|
2018-05-21 14:25:52 +00:00
|
|
|
struct subfn *find_external_files(struct mpv_global *global, const char *fname,
|
|
|
|
struct MPOpts *opts)
|
2011-03-01 21:42:22 +00:00
|
|
|
{
|
|
|
|
struct subfn *slist = talloc_array_ptrtype(NULL, slist, 1);
|
|
|
|
int n = 0;
|
2011-02-26 19:47:49 +00:00
|
|
|
|
2011-03-01 21:42:22 +00:00
|
|
|
// Load subtitles from current media directory
|
2018-05-21 14:25:52 +00:00
|
|
|
append_dir_subtitles(global, opts, &slist, &n, mp_dirname(fname), fname, 0, -1);
|
2011-02-26 19:47:49 +00:00
|
|
|
|
2015-12-25 12:17:11 +00:00
|
|
|
// Load subtitles in dirs specified by sub-paths option
|
2015-12-25 12:40:06 +00:00
|
|
|
if (opts->sub_auto >= 0) {
|
2018-05-21 14:25:52 +00:00
|
|
|
load_paths(global, opts, &slist, &n, fname, opts->sub_paths, "sub",
|
2015-12-25 12:40:06 +00:00
|
|
|
STREAM_SUB);
|
|
|
|
}
|
2011-03-03 10:31:12 +00:00
|
|
|
|
2015-12-25 12:40:06 +00:00
|
|
|
if (opts->audiofile_auto >= 0) {
|
2018-05-21 14:25:52 +00:00
|
|
|
load_paths(global, opts, &slist, &n, fname, opts->audiofile_paths,
|
|
|
|
"audio", STREAM_AUDIO);
|
2015-12-25 12:40:06 +00:00
|
|
|
}
|
2011-02-26 19:47:49 +00:00
|
|
|
|
2013-09-07 18:31:00 +00:00
|
|
|
// Sort by name for filter_subidx()
|
|
|
|
qsort(slist, n, sizeof(*slist), compare_sub_filename);
|
|
|
|
|
|
|
|
filter_subidx(&slist, &n);
|
|
|
|
|
2011-03-01 21:42:22 +00:00
|
|
|
// Sort subs by priority and append them
|
|
|
|
qsort(slist, n, sizeof(*slist), compare_sub_priority);
|
2011-02-26 19:47:49 +00:00
|
|
|
|
2013-11-25 22:38:51 +00:00
|
|
|
struct subfn z = {0};
|
|
|
|
MP_TARRAY_APPEND(NULL, slist, n, z);
|
2011-02-26 19:59:16 +00:00
|
|
|
|
2013-11-25 22:38:51 +00:00
|
|
|
return slist;
|
2011-02-26 19:59:16 +00:00
|
|
|
}
|