From 34a04d056764f4f1447d39b60be56f02253831f8 Mon Sep 17 00:00:00 2001 From: Christoph Heinrich Date: Mon, 6 Mar 2023 20:22:03 +0100 Subject: [PATCH] player: set playlist title to media title if not set already The playlist title only got set when it was specified in the playlist file. If there is a title after opening a file, that should also be reflected in the playlist. --- DOCS/interface-changes.rst | 2 ++ player/command.c | 49 +++++++++++++++++++++++++------------- 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/DOCS/interface-changes.rst b/DOCS/interface-changes.rst index 5908367e90..919e23c412 100644 --- a/DOCS/interface-changes.rst +++ b/DOCS/interface-changes.rst @@ -27,6 +27,8 @@ Interface changes :: --- mpv 0.36.0 --- + - `playlist/N/title` gets set upon opening the file if it wasn't already set + and a title is available. - add the `--vo=kitty` video output driver, as well as the options `--vo-kitty-cols`, `--vo-kitty-rows`, `--vo-kitty-width`, `--vo-kitty-height`, `--vo-kitty-left`, `--vo-kitty-top`, diff --git a/player/command.c b/player/command.c index f115db6159..92e41e3bca 100644 --- a/player/command.c +++ b/player/command.c @@ -515,28 +515,34 @@ static int mp_property_file_size(void *ctx, struct m_property *prop, return m_property_int64_ro(action, arg, size); } +static const char *find_non_filename_media_title(MPContext *mpctx) +{ + const char *name = mpctx->opts->media_title; + if (name && name[0]) + return name; + if (mpctx->demuxer) { + name = mp_tags_get_str(mpctx->demuxer->metadata, "service_name"); + if (name && name[0]) + return name; + name = mp_tags_get_str(mpctx->demuxer->metadata, "title"); + if (name && name[0]) + return name; + name = mp_tags_get_str(mpctx->demuxer->metadata, "icy-title"); + if (name && name[0]) + return name; + } + if (mpctx->playing && mpctx->playing->title) + return mpctx->playing->title; + return NULL; +} + static int mp_property_media_title(void *ctx, struct m_property *prop, int action, void *arg) { MPContext *mpctx = ctx; - char *name = NULL; - if (mpctx->opts->media_title) - name = mpctx->opts->media_title; + const char *name = find_non_filename_media_title(mpctx); if (name && name[0]) return m_property_strdup_ro(action, arg, name); - if (mpctx->demuxer) { - name = mp_tags_get_str(mpctx->demuxer->metadata, "service_name"); - if (name && name[0]) - return m_property_strdup_ro(action, arg, name); - name = mp_tags_get_str(mpctx->demuxer->metadata, "title"); - if (name && name[0]) - return m_property_strdup_ro(action, arg, name); - name = mp_tags_get_str(mpctx->demuxer->metadata, "icy-title"); - if (name && name[0]) - return m_property_strdup_ro(action, arg, name); - } - if (mpctx->playing && mpctx->playing->title) - return m_property_strdup_ro(action, arg, mpctx->playing->title); return mp_property_filename(ctx, prop, action, arg); } @@ -6761,6 +6767,17 @@ static void command_event(struct MPContext *mpctx, int event, void *arg) if (event == MPV_EVENT_FILE_LOADED) audio_update_media_role(mpctx); + + if (event == MP_EVENT_METADATA_UPDATE) { + struct playlist_entry *const pe = mpctx->playing; + if (!pe->title) { + const char *const name = find_non_filename_media_title(mpctx); + if (name && name[0]) { + pe->title = talloc_strdup(pe, name); + mp_notify_property(mpctx, "playlist"); + } + } + } } void handle_command_updates(struct MPContext *mpctx)