1
0
mirror of https://github.com/mpv-player/mpv synced 2025-03-23 03:37:27 +00:00

command: new commands audio_add/audio_remove/audio_reload

These commands are counterparts of sub_add/sub_remove/sub_reload which
work for external audio file.

Signed-off-by: wm4 <wm4@nowhere>
(minor simplification)
This commit is contained in:
xylosper 2015-02-03 17:15:14 +09:00 committed by wm4
parent 2c22fcd350
commit 95fd83a269
6 changed files with 55 additions and 34 deletions

View File

@ -391,6 +391,15 @@ List of Input Commands
<double> <double>
The mouse event represents double-click. The mouse event represents double-click.
``audio_add "<file>" [<flags> [<title> [<lang>]]]``
Load the given audio file. See ``sub_add`` command.
``audio_remove [<id>]``
Remove the given audio track. See ``sub_remove`` command.
``audio_reload [<id>]``
Reload the given audio tracks. See ``sub_reload`` command.
Input Commands that are Possibly Subject to Change Input Commands that are Possibly Subject to Change
-------------------------------------------------- --------------------------------------------------

View File

@ -193,6 +193,12 @@ const struct mp_cmd_def mp_cmds[] = {
{"double", 1})), {"double", 1})),
}}, }},
{ MP_CMD_AUDIO_ADD, "audio_add", { ARG_STRING,
OARG_CHOICE(0, ({"select", 0}, {"auto", 1}, {"cached", 2})),
OARG_STRING(""), OARG_STRING("") } },
{ MP_CMD_AUDIO_REMOVE, "audio_remove", { OARG_INT(-1) } },
{ MP_CMD_AUDIO_RELOAD, "audio_reload", { OARG_INT(-1) } },
{0} {0}
}; };

View File

@ -73,6 +73,9 @@ enum mp_command_type {
MP_CMD_MULTIPLY, MP_CMD_MULTIPLY,
MP_CMD_CYCLE_VALUES, MP_CMD_CYCLE_VALUES,
MP_CMD_STOP, MP_CMD_STOP,
MP_CMD_AUDIO_ADD,
MP_CMD_AUDIO_REMOVE,
MP_CMD_AUDIO_RELOAD,
MP_CMD_ENABLE_INPUT_SECTION, MP_CMD_ENABLE_INPUT_SECTION,
MP_CMD_DISABLE_INPUT_SECTION, MP_CMD_DISABLE_INPUT_SECTION,

View File

@ -4557,51 +4557,58 @@ int run_command(MPContext *mpctx, mp_cmd_t *cmd)
break; break;
} }
case MP_CMD_SUB_ADD: { case MP_CMD_SUB_ADD:
case MP_CMD_AUDIO_ADD: {
if (!mpctx->playing) if (!mpctx->playing)
return -1; return -1;
int type = cmd->id == MP_CMD_SUB_ADD ? STREAM_SUB : STREAM_AUDIO;
if (cmd->args[1].v.i == 2) { if (cmd->args[1].v.i == 2) {
struct track *sub = find_track_with_url(mpctx, STREAM_SUB, struct track *t = find_track_with_url(mpctx, type,
cmd->args[0].v.s); cmd->args[0].v.s);
if (sub) { if (t) {
mp_switch_track(mpctx, sub->type, sub); mp_switch_track(mpctx, t->type, t);
mp_mark_user_track_selection(mpctx, 0, sub->type); mp_mark_user_track_selection(mpctx, 0, t->type);
return 0; return 0;
} }
} }
struct track *sub = mp_add_subtitles(mpctx, cmd->args[0].v.s); struct track *t = mp_add_external_file(mpctx, cmd->args[0].v.s, type);
if (!sub) if (!t)
return -1; return -1;
if (cmd->args[1].v.i == 1) { if (cmd->args[1].v.i == 1) {
sub->no_default = true; t->no_default = true;
} else { } else {
mp_switch_track(mpctx, sub->type, sub); mp_switch_track(mpctx, t->type, t);
mp_mark_user_track_selection(mpctx, 0, sub->type); mp_mark_user_track_selection(mpctx, 0, t->type);
} }
char *title = cmd->args[2].v.s; char *title = cmd->args[2].v.s;
if (title && title[0]) if (title && title[0])
sub->title = talloc_strdup(sub, title); t->title = talloc_strdup(t, title);
char *lang = cmd->args[3].v.s; char *lang = cmd->args[3].v.s;
if (lang && lang[0]) if (lang && lang[0])
sub->lang = talloc_strdup(sub, lang); t->lang = talloc_strdup(t, lang);
break; break;
} }
case MP_CMD_SUB_REMOVE: { case MP_CMD_SUB_REMOVE:
struct track *sub = mp_track_by_tid(mpctx, STREAM_SUB, cmd->args[0].v.i); case MP_CMD_AUDIO_REMOVE: {
if (!sub) int type = cmd->id == MP_CMD_SUB_REMOVE ? STREAM_SUB : STREAM_AUDIO;
struct track *t = mp_track_by_tid(mpctx, type, cmd->args[0].v.i);
if (!t)
return -1; return -1;
mp_remove_track(mpctx, sub); mp_remove_track(mpctx, t);
break; break;
} }
case MP_CMD_SUB_RELOAD: { case MP_CMD_SUB_RELOAD:
struct track *sub = mp_track_by_tid(mpctx, STREAM_SUB, cmd->args[0].v.i); case MP_CMD_AUDIO_RELOAD: {
if (sub && sub->is_external && sub->external_filename) { int type = cmd->id == MP_CMD_SUB_RELOAD ? STREAM_SUB : STREAM_AUDIO;
struct track *nsub = mp_add_subtitles(mpctx, sub->external_filename); struct track *t = mp_track_by_tid(mpctx, type, cmd->args[0].v.i);
if (nsub) { if (t && t->is_external && t->external_filename) {
mp_remove_track(mpctx, sub); struct track *nt = mp_add_external_file(mpctx, t->external_filename,
mp_switch_track(mpctx, nsub->type, nsub); type);
if (nt) {
mp_remove_track(mpctx, t);
mp_switch_track(mpctx, nt->type, nt);
return 0; return 0;
} }
} }

View File

@ -373,7 +373,8 @@ int mp_nav_in_menu(struct MPContext *mpctx);
// loadfile.c // loadfile.c
void uninit_player(struct MPContext *mpctx, unsigned int mask); void uninit_player(struct MPContext *mpctx, unsigned int mask);
struct track *mp_add_subtitles(struct MPContext *mpctx, char *filename); struct track *mp_add_external_file(struct MPContext *mpctx, char *filename,
enum stream_type filter);
void mp_switch_track(struct MPContext *mpctx, enum stream_type type, void mp_switch_track(struct MPContext *mpctx, enum stream_type type,
struct track *track); struct track *track);
void mp_switch_track_n(struct MPContext *mpctx, int order, void mp_switch_track_n(struct MPContext *mpctx, int order,

View File

@ -646,8 +646,8 @@ bool mp_remove_track(struct MPContext *mpctx, struct track *track)
return true; return true;
} }
static struct track *open_external_file(struct MPContext *mpctx, char *filename, struct track *mp_add_external_file(struct MPContext *mpctx, char *filename,
enum stream_type filter) enum stream_type filter)
{ {
struct MPOpts *opts = mpctx->opts; struct MPOpts *opts = mpctx->opts;
if (!filename) if (!filename)
@ -714,19 +714,14 @@ static void open_audiofiles_from_options(struct MPContext *mpctx)
{ {
struct MPOpts *opts = mpctx->opts; struct MPOpts *opts = mpctx->opts;
for (int n = 0; opts->audio_files && opts->audio_files[n]; n++) for (int n = 0; opts->audio_files && opts->audio_files[n]; n++)
open_external_file(mpctx, opts->audio_files[n], STREAM_AUDIO); mp_add_external_file(mpctx, opts->audio_files[n], STREAM_AUDIO);
}
struct track *mp_add_subtitles(struct MPContext *mpctx, char *filename)
{
return open_external_file(mpctx, filename, STREAM_SUB);
} }
static void open_subtitles_from_options(struct MPContext *mpctx) static void open_subtitles_from_options(struct MPContext *mpctx)
{ {
struct MPOpts *opts = mpctx->opts; struct MPOpts *opts = mpctx->opts;
for (int i = 0; opts->sub_name && opts->sub_name[i] != NULL; i++) for (int i = 0; opts->sub_name && opts->sub_name[i] != NULL; i++)
mp_add_subtitles(mpctx, opts->sub_name[i]); mp_add_external_file(mpctx, opts->sub_name[i], STREAM_SUB);
} }
static void autoload_external_files(struct MPContext *mpctx) static void autoload_external_files(struct MPContext *mpctx)
@ -751,7 +746,7 @@ static void autoload_external_files(struct MPContext *mpctx)
if (strcmp(mpctx->sources[n]->stream->url, filename) == 0) if (strcmp(mpctx->sources[n]->stream->url, filename) == 0)
goto skip; goto skip;
} }
struct track *track = open_external_file(mpctx, filename, list[i].type); struct track *track = mp_add_external_file(mpctx, filename, list[i].type);
if (track) { if (track) {
track->auto_loaded = true; track->auto_loaded = true;
if (!track->lang) if (!track->lang)