mirror of
https://github.com/mpv-player/mpv
synced 2024-12-22 06:42:03 +00:00
subs: move vobsub loading logic down to find_subfiles.c
Analogously to the previous commit, move path handling logic for loading external vobsub files from mplayer.c to find_subfiles.c. Based on a commit from Clément Bœsch but fixed and simplified.
This commit is contained in:
parent
7221e28fe3
commit
bdfdece245
8
bstr.c
8
bstr.c
@ -62,6 +62,14 @@ int bstrchr(struct bstr str, int c)
|
||||
return -1;
|
||||
}
|
||||
|
||||
int bstrrchr(struct bstr str, int c)
|
||||
{
|
||||
for (int i = str.len - 1; i >= 0; i--)
|
||||
if (str.start[i] == c)
|
||||
return i;
|
||||
return -1;
|
||||
}
|
||||
|
||||
struct bstr bstr_strip(struct bstr str)
|
||||
{
|
||||
while (str.len && isspace(*str.start)) {
|
||||
|
1
bstr.h
1
bstr.h
@ -37,6 +37,7 @@ struct bstr {
|
||||
int bstrcmp(struct bstr str1, struct bstr str2);
|
||||
int bstrcasecmp(struct bstr str1, struct bstr str2);
|
||||
int bstrchr(struct bstr str, int c);
|
||||
int bstrrchr(struct bstr str, int c);
|
||||
struct bstr *bstr_splitlines(void *talloc_ctx, struct bstr str);
|
||||
struct bstr bstr_strip(struct bstr str);
|
||||
struct bstr bstr_split(struct bstr str, char *sep, struct bstr *rest);
|
||||
|
@ -25,6 +25,11 @@
|
||||
#define ROUND(x) ((int)((x) < 0 ? (x) - 0.5 : (x) + 0.5))
|
||||
|
||||
#define MP_TALLOC_ELEMS(p) (talloc_get_size(p) / sizeof((p)[0]))
|
||||
#define MP_GROW_ARRAY(p, nextidx) do { \
|
||||
if ((nextidx) == MP_TALLOC_ELEMS(p)) \
|
||||
p = talloc_realloc_size(NULL, p, talloc_get_size(p) * 2); } while (0)
|
||||
#define MP_RESIZE_ARRAY(ctx, p, count) do { \
|
||||
p = talloc_realloc_size((ctx), p, (count) * sizeof(p[0])); } while (0)
|
||||
|
||||
extern const char *mplayer_version;
|
||||
|
||||
|
28
mplayer.c
28
mplayer.c
@ -4174,27 +4174,13 @@ if (edl_output_filename) {
|
||||
mp_tmsg(MSGT_CPLAYER,MSGL_ERR,"Cannot load subtitles: %s\n",
|
||||
filename_recode(opts->vobsub_name));
|
||||
} else if (opts->sub_auto && mpctx->filename){
|
||||
/* try to autodetect vobsub from movie filename ::atmos */
|
||||
char *buf = strdup(mpctx->filename), *psub;
|
||||
char *pdot = strrchr(buf, '.');
|
||||
char *pslash = strrchr(buf, '/');
|
||||
#if defined(__MINGW32__) || defined(__CYGWIN__)
|
||||
if (!pslash) pslash = strrchr(buf, '\\');
|
||||
#endif
|
||||
if (pdot && (!pslash || pdot > pslash))
|
||||
*pdot = '\0';
|
||||
vo_vobsub=vobsub_open(buf,spudec_ifo,0,&vo_spudec);
|
||||
/* try from ~/.mplayer/sub */
|
||||
if(!vo_vobsub && (psub = get_path( "sub/" ))) {
|
||||
const char *bname = mp_basename(buf);
|
||||
int l;
|
||||
l = strlen(psub) + strlen(bname) + 1;
|
||||
psub = realloc(psub,l);
|
||||
strcat(psub,bname);
|
||||
vo_vobsub=vobsub_open(psub,spudec_ifo,0,&vo_spudec);
|
||||
free(psub);
|
||||
}
|
||||
free(buf);
|
||||
char **vob = find_vob_subtitles(mpctx->filename);
|
||||
for (int i = 0; i < MP_TALLOC_ELEMS(vob); i++) {
|
||||
vo_vobsub = vobsub_open(vob[i], spudec_ifo, 0, &vo_spudec);
|
||||
if (vo_vobsub)
|
||||
break;
|
||||
}
|
||||
talloc_free(vob);
|
||||
}
|
||||
if(vo_vobsub){
|
||||
mpctx->initialized_flags|=INITIALIZED_VOBSUB;
|
||||
|
@ -5,6 +5,7 @@
|
||||
|
||||
#include "mp_msg.h"
|
||||
#include "path.h"
|
||||
#include "mpcommon.h"
|
||||
#include "sub/find_subfiles.h"
|
||||
#include "sub/sub.h"
|
||||
|
||||
@ -278,3 +279,27 @@ char **find_text_subtitles(const char *fname)
|
||||
free(psub);
|
||||
return tmp;
|
||||
}
|
||||
|
||||
char **find_vob_subtitles(const char *fname)
|
||||
{
|
||||
char **vobs = talloc_array_ptrtype(NULL, vobs, 1);
|
||||
int n = 0;
|
||||
|
||||
// Potential vobsub in the media directory
|
||||
struct bstr bname = BSTR(mp_basename(fname));
|
||||
int pdot = bstrrchr(bname, '.');
|
||||
if (pdot >= 0)
|
||||
bname.len = pdot;
|
||||
vobs[n++] = mp_path_join(vobs, mp_dirname(fname), bname);
|
||||
|
||||
// Potential vobsub in ~/.mplayer/sub
|
||||
char *mp_subdir = get_path("sub/");
|
||||
if (mp_subdir) {
|
||||
MP_GROW_ARRAY(vobs, n);
|
||||
vobs[n++] = mp_path_join(vobs, BSTR(mp_subdir), bname);
|
||||
}
|
||||
|
||||
free(mp_subdir);
|
||||
MP_RESIZE_ARRAY(NULL, vobs, n);
|
||||
return vobs;
|
||||
}
|
||||
|
@ -22,5 +22,6 @@
|
||||
#define MAX_SUBTITLE_FILES 128
|
||||
|
||||
char **find_text_subtitles(const char *fname);
|
||||
char **find_vob_subtitles(const char *fname);
|
||||
|
||||
#endif /* MPLAYER_FINDFILES_H */
|
||||
|
Loading…
Reference in New Issue
Block a user