2011-02-26 19:47:49 +00:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <ctype.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"
|
|
|
|
|
2013-12-21 18:02:05 +00:00
|
|
|
#include "common/global.h"
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/msg.h"
|
2013-12-17 01:02:25 +00:00
|
|
|
#include "options/options.h"
|
|
|
|
#include "options/path.h"
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/common.h"
|
2011-02-26 19:47:49 +00:00
|
|
|
#include "sub/find_subfiles.h"
|
|
|
|
|
2013-09-07 18:31:00 +00:00
|
|
|
static const char *sub_exts[] = {"utf", "utf8", "utf-8", "idx", "sub", "srt",
|
|
|
|
"smi", "rt", "txt", "ssa", "aqt", "jss",
|
|
|
|
"js", "ass", NULL};
|
|
|
|
|
|
|
|
static bool is_sub_ext(bstr ext)
|
|
|
|
{
|
|
|
|
for (int n = 0; sub_exts[n]; n++) {
|
|
|
|
if (bstrcasecmp(bstr0(sub_exts[n]), ext) == 0)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-04-19 03:53:56 +00:00
|
|
|
static struct bstr strip_ext(struct bstr str)
|
2011-02-26 19:47:49 +00:00
|
|
|
{
|
2011-04-19 03:53:56 +00:00
|
|
|
int dotpos = bstrrchr(str, '.');
|
|
|
|
if (dotpos < 0)
|
|
|
|
return str;
|
|
|
|
return (struct bstr){str.start, dotpos};
|
2011-02-26 19:47:49 +00:00
|
|
|
}
|
|
|
|
|
2011-04-19 03:53:56 +00:00
|
|
|
static struct bstr get_ext(struct bstr s)
|
2011-02-26 19:47:49 +00:00
|
|
|
{
|
2011-04-19 03:53:56 +00:00
|
|
|
int dotpos = bstrrchr(s, '.');
|
|
|
|
if (dotpos < 0)
|
|
|
|
return (struct bstr){NULL, 0};
|
|
|
|
return bstr_splice(s, dotpos + 1, s.len);
|
2011-02-26 19:47:49 +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--;
|
2011-04-19 03:53:56 +00:00
|
|
|
while (i >= 0 && 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
|
|
|
}
|
|
|
|
|
2011-03-01 21:42:22 +00:00
|
|
|
/**
|
|
|
|
* @brief Append all the subtitles in the given path matching fname
|
2011-03-03 18:59:15 +00:00
|
|
|
* @param opts MPlayer options
|
2011-03-01 21:42:22 +00:00
|
|
|
* @param slist pointer to the subtitles list tallocated
|
|
|
|
* @param nsub pointer to the number of subtitles
|
|
|
|
* @param path Look for subtitles in this directory
|
|
|
|
* @param fname Subtitle filename (pattern)
|
|
|
|
* @param limit_fuzziness Ignore flag when sub_fuziness == 2
|
|
|
|
*/
|
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,
|
|
|
|
int limit_fuzziness)
|
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;
|
|
|
|
struct mp_log *log = mp_log_new(tmpmem, global->log, "find_subfiles");
|
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));
|
2011-04-27 22:06:16 +00:00
|
|
|
struct bstr f_fname_noext = bstrdup(tmpmem, 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;
|
2013-12-21 18:02:05 +00:00
|
|
|
mp_verbose(log, "Load subtitles 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
|
|
|
|
struct bstr tmp_fname_noext = bstrdup(tmpmem2, strip_ext(dename));
|
|
|
|
bstr_lower(tmp_fname_noext);
|
|
|
|
struct bstr tmp_fname_ext = get_ext(dename);
|
|
|
|
struct bstr tmp_fname_trim = bstr_strip(tmp_fname_noext);
|
|
|
|
|
|
|
|
// does it end with a subtitle extension?
|
2013-09-07 18:31:00 +00:00
|
|
|
if (!is_sub_ext(tmp_fname_ext))
|
|
|
|
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;
|
2011-04-19 03:53:56 +00:00
|
|
|
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++) {
|
2013-11-25 22:38:51 +00:00
|
|
|
if (bstr_startswith0(lang, opts->sub_lang[n])) {
|
2011-04-19 03:53:56 +00:00
|
|
|
prio = 4; // matches the movie name + lang extension
|
2013-11-25 22:38:51 +00:00
|
|
|
found_lang = opts->sub_lang[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
|
|
|
|
if (!prio && bstr_find(tmp_fname_trim, f_fname_trim) >= 0
|
2013-04-28 23:49:20 +00:00
|
|
|
&& opts->sub_match_fuzziness >= 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
|
2013-04-28 23:49:20 +00:00
|
|
|
if (!limit_fuzziness && opts->sub_match_fuzziness >= 2) {
|
2011-04-19 03:53:56 +00:00
|
|
|
prio = 1;
|
|
|
|
}
|
|
|
|
}
|
2011-02-26 19:47:49 +00:00
|
|
|
|
2013-12-21 18:02:05 +00:00
|
|
|
mp_dbg(log, "Potential sub file: \"%s\" Priority: %d\n", de->d_name, prio);
|
2011-04-19 03:53:56 +00:00
|
|
|
if (prio) {
|
|
|
|
prio += prio;
|
|
|
|
char *subpath = mp_path_join(*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;
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-25 22:38:51 +00:00
|
|
|
// Return a list of subtitles found, sorted by priority.
|
|
|
|
// Last element is terminated with a fname==NULL entry.
|
2013-12-21 18:02:05 +00:00
|
|
|
struct subfn *find_text_subtitles(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
|
2013-12-21 18:02:05 +00:00
|
|
|
append_dir_subtitles(global, &slist, &n, mp_dirname(fname), fname, 0);
|
2011-02-26 19:47:49 +00:00
|
|
|
|
2011-03-03 10:31:12 +00:00
|
|
|
// Load subtitles in dirs specified by sub-paths option
|
|
|
|
if (opts->sub_paths) {
|
|
|
|
for (int i = 0; opts->sub_paths[i]; i++) {
|
|
|
|
char *path = mp_path_join(slist, mp_dirname(fname),
|
2012-07-28 21:47:42 +00:00
|
|
|
bstr0(opts->sub_paths[i]));
|
2013-12-21 18:02:05 +00:00
|
|
|
append_dir_subtitles(global, &slist, &n, bstr0(path), fname, 0);
|
2011-03-03 10:31:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-07 18:31:00 +00:00
|
|
|
// Load subtitles in ~/.mpv/sub limiting sub fuzziness
|
2013-12-21 19:45:19 +00:00
|
|
|
char *mp_subdir = mp_find_user_config_file(NULL, global, "sub/");
|
2011-03-01 21:42:22 +00:00
|
|
|
if (mp_subdir)
|
2013-12-21 18:02:05 +00:00
|
|
|
append_dir_subtitles(global, &slist, &n, bstr0(mp_subdir), fname, 1);
|
2012-12-09 14:05:21 +00:00
|
|
|
talloc_free(mp_subdir);
|
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
|
|
|
}
|