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"
|
|
|
|
|
2011-02-26 19:47:49 +00:00
|
|
|
#include "mp_msg.h"
|
2011-03-03 10:31:12 +00:00
|
|
|
#include "options.h"
|
2011-02-26 19:59:16 +00:00
|
|
|
#include "path.h"
|
2011-04-09 02:14:55 +00:00
|
|
|
#include "mpcommon.h"
|
2011-02-26 19:47:49 +00:00
|
|
|
#include "sub/find_subfiles.h"
|
|
|
|
#include "sub/sub.h"
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-03-03 19:05:17 +00:00
|
|
|
struct subfn {
|
2011-02-26 19:47:49 +00:00
|
|
|
int priority;
|
|
|
|
char *fname;
|
2011-03-03 19:05:17 +00:00
|
|
|
};
|
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
|
|
|
}
|
|
|
|
|
|
|
|
struct sub_list {
|
|
|
|
struct subfn subs[MAX_SUBTITLE_FILES];
|
|
|
|
int sid;
|
|
|
|
void *ctx;
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
*/
|
2011-03-03 18:59:15 +00:00
|
|
|
static void append_dir_subtitles(struct MPOpts *opts,
|
|
|
|
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
|
|
|
{
|
|
|
|
char *sub_exts[] = {"utf", "utf8", "utf-8", "sub", "srt", "smi", "rt", "txt", "ssa", "aqt", "jss", "js", "ass", NULL};
|
2011-04-19 03:53:56 +00:00
|
|
|
void *tmpmem = talloc_new(NULL);
|
2011-02-26 19:47:49 +00:00
|
|
|
FILE *f;
|
2011-04-19 03:53:56 +00:00
|
|
|
assert(strlen(fname) < 1e6);
|
2011-02-26 19:47:49 +00:00
|
|
|
|
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;
|
2012-07-29 19:19:36 +00:00
|
|
|
mp_msg(MSGT_SUBREADER, MSGL_V, "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);
|
|
|
|
|
|
|
|
// If it's a .sub, check if there is a .idx with the same name. If
|
|
|
|
// there is one, it's certainly a vobsub so we skip it.
|
2012-07-28 21:47:42 +00:00
|
|
|
if (bstrcasecmp(tmp_fname_ext, bstr0("sub")) == 0) {
|
2011-04-19 03:53:56 +00:00
|
|
|
char *idxname = talloc_asprintf(tmpmem2, "%.*s.idx",
|
|
|
|
(int)tmp_fname_noext.len,
|
|
|
|
de->d_name);
|
2012-07-28 21:47:42 +00:00
|
|
|
char *idx = mp_path_join(tmpmem2, path, bstr0(idxname));
|
2011-04-19 03:53:56 +00:00
|
|
|
f = fopen(idx, "rt");
|
|
|
|
if (f) {
|
|
|
|
fclose(f);
|
|
|
|
goto next_sub;
|
2011-03-03 10:39:18 +00:00
|
|
|
}
|
2011-04-19 03:53:56 +00:00
|
|
|
}
|
2011-03-03 10:39:18 +00:00
|
|
|
|
2011-04-19 03:53:56 +00:00
|
|
|
// does it end with a subtitle extension?
|
2011-02-26 19:47:49 +00:00
|
|
|
#ifdef CONFIG_ICONV
|
|
|
|
#ifdef CONFIG_ENCA
|
2011-04-19 03:53:56 +00:00
|
|
|
int i = (sub_cp && strncasecmp(sub_cp, "enca", 4) != 0) ? 3 : 0;
|
2011-02-26 19:47:49 +00:00
|
|
|
#else
|
2011-04-19 03:53:56 +00:00
|
|
|
int i = sub_cp ? 3 : 0;
|
2011-02-26 19:47:49 +00:00
|
|
|
#endif
|
|
|
|
#else
|
2011-04-19 03:53:56 +00:00
|
|
|
int i = 0;
|
2011-02-26 19:47:49 +00:00
|
|
|
#endif
|
2011-04-19 03:53:56 +00:00
|
|
|
while (1) {
|
|
|
|
if (!sub_exts[i])
|
|
|
|
goto next_sub;
|
2012-07-28 21:47:42 +00:00
|
|
|
if (bstrcasecmp(bstr0(sub_exts[i]), tmp_fname_ext) == 0)
|
2011-04-19 03:53:56 +00:00
|
|
|
break;
|
|
|
|
i++;
|
|
|
|
}
|
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;
|
|
|
|
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,
|
2012-07-28 21:47:42 +00:00
|
|
|
bstr0(opts->sub_lang[n]))) {
|
2011-04-19 03:53:56 +00:00
|
|
|
prio = 4; // matches the movie name + lang extension
|
|
|
|
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
|
|
|
|
&& sub_match_fuzziness >= 1)
|
|
|
|
prio = 2; // contains the movie name
|
|
|
|
if (!prio) {
|
|
|
|
// doesn't contain the movie name
|
|
|
|
// don't try in the mplayer subtitle directory
|
|
|
|
if (!limit_fuzziness && sub_match_fuzziness >= 2) {
|
|
|
|
prio = 1;
|
|
|
|
}
|
|
|
|
}
|
2011-02-26 19:47:49 +00:00
|
|
|
|
2011-04-19 03:53:56 +00:00
|
|
|
mp_msg(MSGT_SUBREADER, MSGL_DBG2, "Potential sub file: "
|
|
|
|
"\"%s\" Priority: %d\n", de->d_name, prio);
|
|
|
|
if (prio) {
|
|
|
|
prio += prio;
|
2011-02-26 19:47:49 +00:00
|
|
|
#ifdef CONFIG_ICONV
|
2011-04-19 03:53:56 +00:00
|
|
|
if (i < 3) // prefer UTF-8 coded
|
|
|
|
prio++;
|
2011-02-26 19:47:49 +00:00
|
|
|
#endif
|
2011-04-19 03:53:56 +00:00
|
|
|
char *subpath = mp_path_join(*slist, path, dename);
|
|
|
|
if ((f = fopen(subpath, "rt"))) {
|
|
|
|
MP_GROW_ARRAY(*slist, *nsub);
|
|
|
|
struct subfn *sub = *slist + (*nsub)++;
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
sub->priority = prio;
|
|
|
|
sub->fname = subpath;
|
|
|
|
} 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
|
|
|
|
2011-03-03 10:31:12 +00:00
|
|
|
char **find_text_subtitles(struct MPOpts *opts, const char *fname)
|
2011-03-01 21:42:22 +00:00
|
|
|
{
|
2011-03-03 10:31:12 +00:00
|
|
|
char **subnames = NULL;
|
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
|
2011-03-03 18:59:15 +00:00
|
|
|
append_dir_subtitles(opts, &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]));
|
|
|
|
append_dir_subtitles(opts, &slist, &n, bstr0(path), fname, 0);
|
2011-03-03 10:31:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-01 21:42:22 +00:00
|
|
|
// Load subtitles in ~/.mplayer/sub limiting sub fuzziness
|
|
|
|
char *mp_subdir = get_path("sub/");
|
|
|
|
if (mp_subdir)
|
2012-07-28 21:47:42 +00:00
|
|
|
append_dir_subtitles(opts, &slist, &n, bstr0(mp_subdir), fname, 1);
|
2011-03-01 21:42:22 +00:00
|
|
|
free(mp_subdir);
|
2011-02-26 19:47:49 +00:00
|
|
|
|
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
|
|
|
|
2011-03-03 10:31:12 +00:00
|
|
|
subnames = talloc_array_ptrtype(NULL, subnames, n);
|
2011-03-01 21:42:22 +00:00
|
|
|
for (int i = 0; i < n; i++)
|
|
|
|
subnames[i] = talloc_strdup(subnames, slist[i].fname);
|
2011-02-26 19:59:16 +00:00
|
|
|
|
2011-03-01 21:42:22 +00:00
|
|
|
talloc_free(slist);
|
|
|
|
return subnames;
|
2011-02-26 19:59:16 +00:00
|
|
|
}
|
2011-04-09 02:14:55 +00:00
|
|
|
|
2011-03-03 10:31:12 +00:00
|
|
|
char **find_vob_subtitles(struct MPOpts *opts, const char *fname)
|
2011-04-09 02:14:55 +00:00
|
|
|
{
|
|
|
|
char **vobs = talloc_array_ptrtype(NULL, vobs, 1);
|
|
|
|
int n = 0;
|
|
|
|
|
|
|
|
// Potential vobsub in the media directory
|
2012-07-28 21:47:42 +00:00
|
|
|
struct bstr bname = bstr0(mp_basename(fname));
|
2011-04-09 02:14:55 +00:00
|
|
|
int pdot = bstrrchr(bname, '.');
|
|
|
|
if (pdot >= 0)
|
|
|
|
bname.len = pdot;
|
|
|
|
vobs[n++] = mp_path_join(vobs, mp_dirname(fname), bname);
|
|
|
|
|
2011-03-03 10:31:12 +00:00
|
|
|
// Potential vobsubs in directories specified by sub-paths option
|
|
|
|
if (opts->sub_paths) {
|
|
|
|
for (int i = 0; opts->sub_paths[i]; i++) {
|
|
|
|
char *path = mp_path_join(NULL, mp_dirname(fname),
|
2012-07-28 21:47:42 +00:00
|
|
|
bstr0(opts->sub_paths[i]));
|
2011-03-03 10:31:12 +00:00
|
|
|
MP_GROW_ARRAY(vobs, n);
|
2012-07-28 21:47:42 +00:00
|
|
|
vobs[n++] = mp_path_join(vobs, bstr0(path), bname);
|
2011-03-03 10:31:12 +00:00
|
|
|
talloc_free(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-09 02:14:55 +00:00
|
|
|
// Potential vobsub in ~/.mplayer/sub
|
|
|
|
char *mp_subdir = get_path("sub/");
|
|
|
|
if (mp_subdir) {
|
|
|
|
MP_GROW_ARRAY(vobs, n);
|
2012-07-28 21:47:42 +00:00
|
|
|
vobs[n++] = mp_path_join(vobs, bstr0(mp_subdir), bname);
|
2011-04-09 02:14:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
free(mp_subdir);
|
|
|
|
MP_RESIZE_ARRAY(NULL, vobs, n);
|
|
|
|
return vobs;
|
|
|
|
}
|