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"
|
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
|
|
|
|
2014-06-10 21:56:05 +00:00
|
|
|
static const char *const sub_exts[] = {"utf", "utf8", "utf-8", "idx", "sub", "srt",
|
|
|
|
"smi", "rt", "txt", "ssa", "aqt", "jss",
|
2015-05-18 08:28:01 +00:00
|
|
|
"js", "ass", "mks", "vtt", "sup", 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",
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-12-21 18:02:05 +00:00
|
|
|
static void append_dir_subtitles(struct mpv_global *global,
|
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);
|
2013-12-21 18:02:05 +00:00
|
|
|
struct MPOpts *opts = global->opts;
|
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
|
|
|
|
2013-09-07 18:22:40 +00:00
|
|
|
if (mp_is_url(bstr0(fname)))
|
|
|
|
goto out;
|
|
|
|
|
2012-07-28 21:47:42 +00:00
|
|
|
struct bstr f_fname = bstr0(mp_basename(fname));
|
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
|
|
|
|
|
|
|
// 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
|
|
|
char *path0 = bstrdup0(tmpmem, path);
|
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))) {
|
2012-07-28 21:47:42 +00:00
|
|
|
struct bstr dename = bstr0(de->d_name);
|
2011-04-19 03:53:56 +00:00
|
|
|
void *tmpmem2 = talloc_new(tmpmem);
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
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
|
|
|
|
int prio = 0;
|
2013-11-25 22:38:51 +00:00
|
|
|
char *found_lang = NULL;
|
2015-02-02 20:23:12 +00:00
|
|
|
if (langs) {
|
2011-04-19 03:53:56 +00:00
|
|
|
if (bstr_startswith(tmp_fname_trim, f_fname_trim)) {
|
|
|
|
struct bstr lang = guess_lang_from_filename(tmp_fname_trim);
|
|
|
|
if (lang.len) {
|
2015-02-02 20:23:12 +00:00
|
|
|
for (int n = 0; langs[n]; n++) {
|
|
|
|
if (bstr_startswith0(lang, langs[n])) {
|
2011-04-19 03:53:56 +00:00
|
|
|
prio = 4; // matches the movie name + lang extension
|
2015-02-02 20:23:12 +00:00
|
|
|
found_lang = langs[n];
|
2011-04-19 03:53:56 +00:00
|
|
|
break;
|
2011-03-03 18:59:15 +00:00
|
|
|
}
|
2011-02-26 19:47:49 +00:00
|
|
|
}
|
2011-03-01 21:42:22 +00:00
|
|
|
}
|
2011-04-19 03:53:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!prio && bstrcmp(tmp_fname_trim, f_fname_trim) == 0)
|
|
|
|
prio = 3; // matches the movie name
|
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)) {
|
2011-04-19 03:53:56 +00:00
|
|
|
MP_GROW_ARRAY(*slist, *nsub);
|
|
|
|
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;
|
2013-11-25 22:38:51 +00:00
|
|
|
sub->lang = found_lang;
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-25 12:17:11 +00:00
|
|
|
static void load_paths(struct mpv_global *global, struct subfn **slist,
|
|
|
|
int *nsubs, const char *fname, char **paths,
|
2015-12-25 12:40:06 +00:00
|
|
|
char *cfg_path, int type)
|
2015-12-25 12:17:11 +00:00
|
|
|
{
|
|
|
|
for (int i = 0; paths && paths[i]; i++) {
|
|
|
|
char *path = mp_path_join_bstr(*slist, mp_dirname(fname),
|
|
|
|
bstr0(paths[i]));
|
2015-12-25 12:40:06 +00:00
|
|
|
append_dir_subtitles(global, slist, nsubs, bstr0(path), fname, 0, type);
|
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) {
|
|
|
|
append_dir_subtitles(global, slist, nsubs, bstr0(mp_subdir), fname, 1,
|
|
|
|
type);
|
|
|
|
}
|
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.
|
2015-02-02 20:23:12 +00:00
|
|
|
struct subfn *find_external_files(struct mpv_global *global, const char *fname)
|
2011-03-01 21:42:22 +00:00
|
|
|
{
|
2013-12-21 18:02:05 +00:00
|
|
|
struct MPOpts *opts = global->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
|
2015-12-25 12:40:06 +00:00
|
|
|
append_dir_subtitles(global, &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) {
|
|
|
|
load_paths(global, &slist, &n, fname, opts->sub_paths, "sub/",
|
|
|
|
STREAM_SUB);
|
|
|
|
}
|
2011-03-03 10:31:12 +00:00
|
|
|
|
2015-12-25 12:40:06 +00:00
|
|
|
if (opts->audiofile_auto >= 0) {
|
|
|
|
load_paths(global, &slist, &n, fname, opts->audiofile_paths, "audio/",
|
|
|
|
STREAM_AUDIO);
|
|
|
|
}
|
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
|
|
|
}
|