command: add a "cached" mode to sub_add

This avoids reloading a subtitle if it was already added. In all cases,
the subtitle is selected.
This commit is contained in:
wm4 2014-10-23 13:11:41 +02:00
parent 5a3c6563e4
commit 7e27663b7b
3 changed files with 30 additions and 2 deletions

View File

@ -275,6 +275,13 @@ List of Input Commands
Don't select the subtitle. (Or in some special situations, let the
default stream selection mechanism decide.)
<cached>
Select the subtitle. If a subtitle with the same filename was already
added, that one is selected, instead of loading a duplicate entry.
(In this case, title/language are ignored, and if the was changed since
it was loaded, these changes won't be reflected.)
The ``title`` argument sets the track title in the UI.
The ``lang`` argument sets the track language, and can also influence

View File

@ -91,7 +91,7 @@ const struct mp_cmd_def mp_cmds[] = {
.allow_auto_repeat = true},
{ MP_CMD_SHOW_PROGRESS, "show_progress", .allow_auto_repeat = true},
{ MP_CMD_SUB_ADD, "sub_add", { ARG_STRING,
OARG_CHOICE(0, ({"select", 0}, {"auto", 1})),
OARG_CHOICE(0, ({"select", 0}, {"auto", 1}, {"cached", 2})),
OARG_STRING(""), OARG_STRING("") } },
{ MP_CMD_SUB_REMOVE, "sub_remove", { OARG_INT(-1) } },
{ MP_CMD_SUB_RELOAD, "sub_reload", { OARG_INT(-1) } },

View File

@ -3682,6 +3682,18 @@ static int mp_property_multiply(char *property, double f, struct MPContext *mpct
return r;
}
static struct track *find_track_with_url(struct MPContext *mpctx, int type,
const char *url)
{
for (int n = 0; n < mpctx->num_tracks; n++) {
struct track *track = mpctx->tracks[n];
if (track && track->type == type && track->is_external &&
strcmp(track->external_filename, url) == 0)
return track;
}
return NULL;
}
// Whether this property should react to key events generated by auto-repeat.
static bool check_property_autorepeat(char *property, struct MPContext *mpctx)
{
@ -4090,10 +4102,19 @@ int run_command(MPContext *mpctx, mp_cmd_t *cmd)
case MP_CMD_SUB_ADD: {
if (!mpctx->playing)
return -1;
if (cmd->args[1].v.i == 2) {
struct track *sub = find_track_with_url(mpctx, STREAM_SUB,
cmd->args[0].v.s);
if (sub) {
mp_switch_track(mpctx, sub->type, sub);
mp_mark_user_track_selection(mpctx, 0, sub->type);
return 0;
}
}
struct track *sub = mp_add_subtitles(mpctx, cmd->args[0].v.s);
if (!sub)
return -1;
if (cmd->args[1].v.i == 0) {
if (cmd->args[1].v.i != 1) {
mp_switch_track(mpctx, sub->type, sub);
mp_mark_user_track_selection(mpctx, 0, sub->type);
}