[PATCH] Add subdirs option
Clément Bœsch
ubitux at gmail.com
Tue Nov 9 23:48:05 CET 2010
---
DOCS/man/en/mplayer.1 | 29 ++++++--
cfg-common.h | 1 +
mencoder.c | 19 +-----
mpcommon.h | 1 +
mplayer.c | 17 +----
path.c | 32 +++++++++
path.h | 1 +
sub/subreader.c | 172 +++++++++++++++++++++++++++++++++++--------------
sub/subreader.h | 2 +-
9 files changed, 184 insertions(+), 90 deletions(-)
diff --git a/DOCS/man/en/mplayer.1 b/DOCS/man/en/mplayer.1
index 9921db4..6b82a42 100644
--- a/DOCS/man/en/mplayer.1
+++ b/DOCS/man/en/mplayer.1
@@ -2625,6 +2625,27 @@ Guess the encoding for Polish, fall back on cp1250.
.PD 1
.
.TP
+.B \-subdirs <dirname1,dirname2,...>
+Specify extra subtitles directories to track in the media directory.
+.sp 1
+.I EXAMPLE:
+Assuming that /path/\:to/\:movie/\:movie.avi is played and \-subdirs
+sub,subtitles,/tmp/subs is specified, MPlayer searches for subtitles files in these
+directories:
+.RSs
+/path/\:to/\:movie/
+.br
+/path/\:to/\:movie/\:sub/
+.br
+/path/\:to/\:movie/\:subtitles/
+.br
+/tmp/\:subs/
+.br
+~/.mplayer/\:sub/
+.RE
+.PD 1
+.
+.TP
.B \-subdelay <sec>
Delays subtitles by <sec> seconds.
Can be negative.
@@ -11714,14 +11735,6 @@ font directory (There must be a font.desc file and files with .RAW extension.)
.TP
~/.mplayer/\:DVDkeys/
cached CSS keys
-.
-.TP
-Assuming that /path/\:to/\:movie.avi is played, MPlayer searches for sub files
-in this order:
-.RS
-/path/\:to/\:movie.sub
-.br
-~/.mplayer/\:sub/\:movie.sub
.RE
.PD 1
.
diff --git a/cfg-common.h b/cfg-common.h
index c6dfef0..d50ebe2 100644
--- a/cfg-common.h
+++ b/cfg-common.h
@@ -562,6 +562,7 @@ const m_option_t common_opts[] = {
// ------------------------- subtitles options --------------------
{"sub", &sub_name, CONF_TYPE_STRING_LIST, 0, 0, 0, NULL},
+ {"subdirs", &sub_dirs, CONF_TYPE_STRING_LIST, 0, 0, 0, NULL},
#ifdef CONFIG_FRIBIDI
{"fribidi-charset", &fribidi_charset, CONF_TYPE_STRING, 0, 0, 0, NULL},
{"flip-hebrew", &flip_hebrew, CONF_TYPE_FLAG, 0, 0, 1, NULL},
diff --git a/mencoder.c b/mencoder.c
index 57afbc2..403fbac 100644
--- a/mencoder.c
+++ b/mencoder.c
@@ -179,6 +179,7 @@ char *font_name=NULL;
char *sub_font_name=NULL;
float font_factor=0.75;
char **sub_name=NULL;
+char **sub_dirs = NULL;
float sub_delay=0;
float sub_fps=0;
int sub_auto = 0;
@@ -1019,23 +1020,7 @@ default: {
// after reading video params we should load subtitles because
// we know fps so now we can adjust subtitles time to ~6 seconds AST
// check .sub
- if(sub_name && sub_name[0]){
- for (i = 0; sub_name[i] != NULL; ++i)
- add_subtitles (sub_name[i], sh_video->fps, 0);
- } else
- if(sub_auto && filename) { // auto load sub file ...
- char **tmp = NULL;
- int i = 0;
- char *psub = get_path( "sub/" );
- tmp = sub_filenames((psub ? psub : ""), filename);
- free(psub);
- while (tmp[i])
- {
- add_subtitles (tmp[i], sh_video->fps, 0);
- free(tmp[i++]);
- }
- free(tmp);
- }
+ load_subtitles(filename, sh_video->fps, add_subtitles);
mp_msg(MSGT_CPLAYER,MSGL_INFO,"==========================================================================\n");
init_best_video_codec(sh_video,video_codec_list,video_fm_list);
diff --git a/mpcommon.h b/mpcommon.h
index 19110d6..4a2147e 100644
--- a/mpcommon.h
+++ b/mpcommon.h
@@ -35,6 +35,7 @@ extern int sub_auto;
extern float sub_delay;
extern float sub_fps;
extern char **sub_name;
+extern char **sub_dirs;
extern char *font_name;
extern char *sub_font_name;
extern char *audio_lang;
diff --git a/mplayer.c b/mplayer.c
index 517448d..7ebc3cd 100644
--- a/mplayer.c
+++ b/mplayer.c
@@ -292,6 +292,7 @@ char *font_name=NULL;
char *sub_font_name=NULL;
float font_factor=0.75;
char **sub_name=NULL;
+char **sub_dirs = NULL;
float sub_delay=0;
float sub_fps=0;
int sub_auto = 1;
@@ -3558,21 +3559,7 @@ if(1 || mpctx->sh_video) {
// check .sub
double fps = mpctx->sh_video ? mpctx->sh_video->fps : 25;
current_module="read_subtitles_file";
- if(sub_name){
- for (i = 0; sub_name[i] != NULL; ++i)
- add_subtitles (sub_name[i], fps, 0);
- }
- if(sub_auto) { // auto load sub file ...
- char *psub = get_path( "sub/" );
- char **tmp = sub_filenames((psub ? psub : ""), filename);
- int i = 0;
- free(psub); // release the buffer created by get_path() above
- while (tmp[i]) {
- add_subtitles (tmp[i], fps, 1);
- free(tmp[i++]);
- }
- free(tmp);
- }
+ load_subtitles(filename, fps, add_subtitles);
if (mpctx->set_of_sub_size > 0)
mpctx->sub_counts[SUB_SOURCE_SUBS] = mpctx->set_of_sub_size;
}
diff --git a/path.c b/path.c
index bb1da4d..daba695 100644
--- a/path.c
+++ b/path.c
@@ -206,3 +206,35 @@ const char *mp_basename(const char *path)
s = strrchr(path, '/');
return s ? s + 1 : path;
}
+
+/**
+ * \brief Allocates a new buffer containing the directory name
+ * \param path Original path. Must be a valid string.
+ *
+ * The path returned always contains a trailing slash '/'.
+ * On systems supporting DOS paths, '\' is also considered as a directory
+ * separator in addition to the '/'.
+ */
+char *mp_dirname(const char *path)
+{
+ char *tmp, *s;
+ size_t len;
+
+ tmp = strrchr(path, '/');
+
+#if HAVE_DOS_PATHS
+ if (!tmp)
+ tmp = strrchr(path, '\\');
+#endif
+
+ if (!tmp)
+ return strdup("./");
+ tmp++;
+ len = tmp - path;
+ s = malloc(len + 1);
+ if (!s)
+ return NULL;
+ strncpy(s, path, len);
+ s[len] = '\0';
+ return s;
+}
diff --git a/path.h b/path.h
index 349f55e..dddbd19 100644
--- a/path.h
+++ b/path.h
@@ -27,5 +27,6 @@ char *get_path(const char *filename);
void set_path_env(void);
void set_codec_path(const char *path);
const char *mp_basename(const char *path);
+char *mp_dirname(const char *path);
#endif /* MPLAYER_PATH_H */
diff --git a/sub/subreader.c b/sub/subreader.c
index 42d523b..65f48a9 100644
--- a/sub/subreader.c
+++ b/sub/subreader.c
@@ -33,6 +33,7 @@
#include "config.h"
#include "mp_msg.h"
#include "mpcommon.h"
+#include "path.h"
#include "subreader.h"
#include "subassconvert.h"
#include "sub.h"
@@ -1876,8 +1877,15 @@ typedef struct subfn
{
int priority;
char *fname;
+ int noerr;
} subfn;
+struct sub_list {
+ struct subfn *subs;
+ size_t size;
+ int sid;
+};
+
static int compare_sub_priority(const void *a, const void *b)
{
if (((const subfn*)a)->priority > ((const subfn*)b)->priority) {
@@ -1889,17 +1897,35 @@ static int compare_sub_priority(const void *a, const void *b)
}
}
-char** sub_filenames(const char* path, char *fname)
+static void append_sub(struct sub_list *dst, struct subfn *src)
+{
+ if (dst->sid >= (int)dst->size - 1) {
+ dst->size += 32;
+ dst->subs = realloc(dst->subs, sizeof(*dst->subs) * dst->size);
+ }
+ memcpy(&dst->subs[dst->sid], src, sizeof(*src));
+ dst->sid++;
+}
+
+static void merge_subs(struct sub_list *dst, struct sub_list *src)
+{
+ if (src->sid == 0)
+ return;
+ dst->size = dst->sid + src->sid;
+ dst->subs = realloc(dst->subs, sizeof(*dst->subs) * dst->size);
+ memcpy(&dst->subs[dst->sid], src->subs, src->sid * sizeof(*src->subs));
+ dst->sid += src->sid;
+}
+
+static struct sub_list get_sub_list(const char* path, char *fname)
{
- char *f_dir, *f_fname, *f_fname_noext, *f_fname_trim, *tmp, *tmp_sub_id;
+ char *f_fname, *f_fname_noext, *f_fname_trim, *tmp, *tmp_sub_id;
char *tmp_fname_noext, *tmp_fname_trim, *tmp_fname_ext, *tmpresult;
- int len, pos, found, i, j;
+ int len, found, i;
char * sub_exts[] = { "utf", "utf8", "utf-8", "sub", "srt", "smi", "rt", "txt", "ssa", "aqt", "jss", "js", "ass", NULL};
- subfn *result;
- char **result2;
- int subcnt;
+ struct sub_list subs = {0};
FILE *f;
@@ -1909,8 +1935,7 @@ char** sub_filenames(const char* path, char *fname)
len = (strlen(fname) > 256 ? strlen(fname) : 256)
+(strlen(path) > 256 ? strlen(path) : 256)+2;
- f_dir = malloc(len);
- f_fname = malloc(len);
+ f_fname = strdup(mp_basename(fname));
f_fname_noext = malloc(len);
f_fname_trim = malloc(len);
@@ -1920,27 +1945,6 @@ char** sub_filenames(const char* path, char *fname)
tmpresult = malloc(len);
- result = calloc(MAX_SUBTITLE_FILES, sizeof(*result));
-
- subcnt = 0;
-
- tmp = strrchr(fname,'/');
-#if HAVE_DOS_PATHS
- if(!tmp)tmp = strrchr(fname,'\\');
- if(!tmp)tmp = strrchr(fname,':');
-#endif
-
- // extract filename & dirname from fname
- if (tmp) {
- strcpy(f_fname, tmp+1);
- pos = tmp - fname;
- strncpy(f_dir, fname, pos+1);
- f_dir[pos+1] = 0;
- } else {
- strcpy(f_fname, fname);
- strcpy(f_dir, "./");
- }
-
strcpy_strip_ext(f_fname_noext, f_fname);
strcpy_trim(f_fname_trim, f_fname_noext);
@@ -1954,9 +1958,9 @@ char** sub_filenames(const char* path, char *fname)
// 1 = any subtitle file
// 2 = any sub file containing movie name
// 3 = sub file containing movie name and the lang extension
- for (j = 0; j <= 1; j++) {
- d = opendir(j == 0 ? f_dir : path);
+ d = opendir(path);
if (d) {
+ mp_msg(MSGT_SUBREADER, MSGL_INFO, "Load subtitles in %s\n", path);
while ((de = readdir(d))) {
// retrieve various parts of the filename
strcpy_strip_ext(tmp_fname_noext, de->d_name);
@@ -2012,8 +2016,7 @@ char** sub_filenames(const char* path, char *fname)
}
if (!prio) {
// doesn't contain the movie name
- // don't try in the mplayer subtitle directory
- if ((j == 0) && (sub_match_fuzziness >= 2)) {
+ if (sub_match_fuzziness >= 2) {
prio = 1;
}
}
@@ -2025,27 +2028,29 @@ char** sub_filenames(const char* path, char *fname)
prio++;
}
#endif
- sprintf(tmpresult, "%s%s", j == 0 ? f_dir : path, de->d_name);
+ sprintf(tmpresult, "%s%s", path, de->d_name);
// fprintf(stderr, "%s priority %d\n", tmpresult, prio);
if ((f = fopen(tmpresult, "rt"))) {
- fclose(f);
- result[subcnt].priority = prio;
- result[subcnt].fname = strdup(tmpresult);
- subcnt++;
+ struct subfn sub = {
+ .fname = strdup(tmpresult),
+ .priority = prio,
+ .noerr = 1
+ };
+
+ fclose(f);
+ append_sub(&subs, &sub);
}
}
}
- if (subcnt >= MAX_SUBTITLE_FILES) break;
+ if (subs.sid >= MAX_SUBTITLE_FILES)
+ break;
}
closedir(d);
}
- }
-
free(tmp_sub_id);
- free(f_dir);
free(f_fname);
free(f_fname_noext);
free(f_fname_trim);
@@ -2056,18 +2061,87 @@ char** sub_filenames(const char* path, char *fname)
free(tmpresult);
- qsort(result, subcnt, sizeof(subfn), compare_sub_priority);
+ return subs;
+}
+
+static void track_sub_directory(char *dir, char *fname, struct sub_list *fslist)
+{
+ struct sub_list tmp_list;
+ char *path;
+ size_t plen;
+
+ // Build path
+ path = mp_dirname(fname);
+ plen = strlen(path);
+ path = realloc(path, plen + strlen(dir) + 1);
+ strcpy(path + plen, dir);
+
+ // Grab and merge subtitles filenames
+ tmp_list = get_sub_list(path, fname);
+ merge_subs(fslist, &tmp_list);
+
+ free(tmp_list.subs);
+ free(path);
+}
+
+static struct sub_list get_full_sub_list(char *fname)
+{
+ struct sub_list fslist = {0};
+ char *mp_subdir;
+
+ // Load subtitles specified by sub option with highest priority
+ if (sub_name) {
+ int i;
+ for (i = 0; sub_name[i]; i++) {
+ struct subfn sub = {
+ .fname = strdup(sub_name[i]),
+ .priority = INT_MAX - i,
+ .noerr = 0
+ };
+ append_sub(&fslist, &sub);
+ }
+ }
+
+ // Stop here if automatic detection disabled
+ if (!sub_auto || !fname)
+ return fslist;
- result2 = calloc(subcnt + 1, sizeof(*result2));
+ // Load subtitles from current media directory
+ track_sub_directory("", fname, &fslist);
- for (i = 0; i < subcnt; i++) {
- result2[i] = result[i].fname;
+ // Load subtitles in dirs specified by subdirs option
+ if (sub_dirs) {
+ int i;
+ for (i = 0; sub_dirs[i]; i++)
+ track_sub_directory(sub_dirs[i], fname, &fslist);
}
- result2[subcnt] = NULL;
- free(result);
+ // Load subtitles in ~/.mplayer/sub
+ mp_subdir = get_path("sub/");
+ if (mp_subdir) {
+ struct sub_list tmp_list = get_sub_list(mp_subdir, fname);
+ merge_subs(&fslist, &tmp_list);
+ free(tmp_list.subs);
+ }
+ free(mp_subdir);
- return result2;
+ return fslist;
+}
+
+void load_subtitles(char *fname, int fps, void add_f(char *, float, int))
+{
+ int i;
+ struct sub_list fslist = get_full_sub_list(fname);
+
+ if (fslist.sid == 0)
+ return;
+ qsort(fslist.subs, fslist.sid, sizeof(*fslist.subs), compare_sub_priority);
+ for (i = 0; i < fslist.sid; i++) {
+ struct subfn *sub = &fslist.subs[i];
+ add_f(sub->fname, fps, sub->noerr);
+ free(sub->fname);
+ }
+ free(fslist.subs);
}
void list_sub_file(sub_data* subd){
diff --git a/sub/subreader.h b/sub/subreader.h
index 323b576..fb67760 100644
--- a/sub/subreader.h
+++ b/sub/subreader.h
@@ -94,7 +94,7 @@ void subcp_close (void); /* for demux_ogg.c */
const char* guess_buffer_cp(unsigned char* buffer, int buflen, const char *preferred_language, const char *fallback);
const char* guess_cp(struct stream *st, const char *preferred_language, const char *fallback);
#endif
-char ** sub_filenames(const char *path, char *fname);
+void load_subtitles(char *fname, int fps, void add_f(char *, float, int));
void list_sub_file(sub_data* subd);
void dump_srt(sub_data* subd, float fps);
void dump_mpsub(sub_data* subd, float fps);
--
1.7.3.2
--6zdv2QT/q3FMhpsV--
More information about the MPlayer-dev-eng
mailing list