From 7e27663b7bdd92125dd221bf3dbb8f760681c9d7 Mon Sep 17 00:00:00 2001 From: wm4 Date: Thu, 23 Oct 2014 13:11:41 +0200 Subject: [PATCH] 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. --- DOCS/man/input.rst | 7 +++++++ input/cmd_list.c | 2 +- player/command.c | 23 ++++++++++++++++++++++- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/DOCS/man/input.rst b/DOCS/man/input.rst index 05cc9da562..9dc1cf0ea6 100644 --- a/DOCS/man/input.rst +++ b/DOCS/man/input.rst @@ -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.) + + + 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 diff --git a/input/cmd_list.c b/input/cmd_list.c index 6f9bd17af9..fe014c4983 100644 --- a/input/cmd_list.c +++ b/input/cmd_list.c @@ -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) } }, diff --git a/player/command.c b/player/command.c index 77645d3755..8887338a6e 100644 --- a/player/command.c +++ b/player/command.c @@ -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); }