From 3ea8d751f5b06c5c894660074a96e63edfb7316b Mon Sep 17 00:00:00 2001 From: Guido Cella Date: Thu, 24 Oct 2024 11:04:28 +0200 Subject: [PATCH] command: print track metadata in ${track-list} Extract track metadata formatting out of loadfile.c to reuse in the track-list property. --- player/command.c | 65 ++++++++++++++++++++++++++++++++++++++++------- player/command.h | 3 +++ player/loadfile.c | 47 +++------------------------------- 3 files changed, 62 insertions(+), 53 deletions(-) diff --git a/player/command.c b/player/command.c index 12dc0b7b72..3f0870798b 100644 --- a/player/command.c +++ b/player/command.c @@ -76,8 +76,6 @@ #include "osdep/subprocess.h" #include "osdep/terminal.h" -#include "core.h" - #ifdef _WIN32 #include #endif @@ -2104,21 +2102,70 @@ static const char *track_type_name(struct track *t) return NULL; } - static char *append_track_info(char *res, struct track *track) { res = talloc_strdup_append(res, track->selected ? list_current : list_normal); res = talloc_asprintf_append(res, "(%d) ", track->user_tid); - if (track->title) - res = talloc_asprintf_append(res, "'%s' ", track->title); - if (track->lang) - res = talloc_asprintf_append(res, "(%s) ", track->lang); - if (track->is_external) - res = talloc_asprintf_append(res, "(external) "); + res = talloc_strdup_append(res, mp_format_track_metadata(res, track)); return res; } +#define bstr_xappend0(ctx, dst, s) bstr_xappend(ctx, dst, bstr0(s)) +#define ADD_FLAG(ctx, dst, flag, first) do { \ + bstr_xappend_asprintf(ctx, &dst, " %s%s", first ? "[" : "", flag); \ + first = false; \ +} while(0) + +char *mp_format_track_metadata(void *ctx, struct track *t) +{ + struct sh_stream *s = t->stream; + bstr dst = {0}; + + if (t->title) + bstr_xappend_asprintf(ctx, &dst, " '%s'", t->title); + + const char *codec = s ? s->codec->codec : NULL; + bstr_xappend_asprintf(ctx, &dst, " (%s", codec ? codec : ""); + + if (s && s->codec->codec_profile) + bstr_xappend_asprintf(ctx, &dst, " [%s]", s->codec->codec_profile); + if (s && s->codec->disp_w) + bstr_xappend_asprintf(ctx, &dst, " %dx%d", s->codec->disp_w, s->codec->disp_h); + if (s && s->codec->fps && !t->image) { + char *fps = mp_format_double(ctx, s->codec->fps, 4, false, false, true); + bstr_xappend_asprintf(ctx, &dst, " %s fps", fps); + } + if (s && s->codec->channels.num) + bstr_xappend_asprintf(ctx, &dst, " %dch", s->codec->channels.num); + if (s && s->codec->samplerate) + bstr_xappend_asprintf(ctx, &dst, " %d Hz", s->codec->samplerate); + if (s && s->codec->bitrate) { + bstr_xappend_asprintf(ctx, &dst, " %d kbps", (s->codec->bitrate + 500) / 1000); + } else if (s && s->hls_bitrate) { + bstr_xappend_asprintf(ctx, &dst, " %d kbps", (s->hls_bitrate + 500) / 1000); + } + bstr_xappend0(ctx, &dst, ")"); + + bool first = true; + if (t->default_track) + ADD_FLAG(ctx, dst, "default", first); + if (t->forced_track) + ADD_FLAG(ctx, dst, "forced", first); + if (t->dependent_track) + ADD_FLAG(ctx, dst, "dependent", first); + if (t->visual_impaired_track) + ADD_FLAG(ctx, dst, "visual-impaired", first); + if (t->hearing_impaired_track) + ADD_FLAG(ctx, dst, "hearing-impaired", first); + if (t->is_external) + ADD_FLAG(ctx, dst, "external", first); + if (!first) + bstr_xappend0(ctx, &dst, "]"); + + return bstrto0(ctx, dst); +} + static int property_list_tracks(void *ctx, struct m_property *prop, int action, void *arg) { diff --git a/player/command.h b/player/command.h index 31e3b32a78..6022e0dcd0 100644 --- a/player/command.h +++ b/player/command.h @@ -20,6 +20,7 @@ #include +#include "core.h" #include "libmpv/client.h" #include "osdep/compiler.h" @@ -121,4 +122,6 @@ void mark_seek(struct MPContext *mpctx); void mp_abort_cache_dumping(struct MPContext *mpctx); +char *mp_format_track_metadata(void *ctx, struct track *t); + #endif /* MPLAYER_COMMAND_H */ diff --git a/player/loadfile.c b/player/loadfile.c index 01d4f866ed..d1fac68231 100644 --- a/player/loadfile.c +++ b/player/loadfile.c @@ -238,14 +238,9 @@ static void uninit_demuxer(struct MPContext *mpctx) #define WHITE_CIRCLE "\xe2\x97\x8b" #define APPEND(s, ...) mp_snprintf_cat(s, sizeof(s), __VA_ARGS__) #define FILL(s, n) mp_snprintf_cat(s, sizeof(s), "%*s", n, "") -#define ADD_FLAG(b, flag, first) do { \ - APPEND(b, " %s%s", first ? "[" : "", flag); \ - first = false; \ -} while(0) static void print_stream(struct MPContext *mpctx, struct track *t, bool indent) { - struct sh_stream *s = t->stream; const char *tname = "?"; const char *selopt = "?"; const char *langopt = "?"; @@ -277,46 +272,10 @@ static void print_stream(struct MPContext *mpctx, struct track *t, bool indent) } else if (max_lang_length) { FILL(b, (int) strlen(" --alang= ") + max_lang_length); } - if (t->title) - APPEND(b, " '%s'", t->title); - const char *codec = s ? s->codec->codec : NULL; - APPEND(b, " (%s", codec ? codec : ""); - if (s && s->codec->codec_profile) - APPEND(b, " [%s]", s->codec->codec_profile); - if (s && s->codec->disp_w) - APPEND(b, " %dx%d", s->codec->disp_w, s->codec->disp_h); - if (s && s->codec->fps && !t->image) { - char *fps = mp_format_double(NULL, s->codec->fps, 4, false, false, true); - APPEND(b, " %s fps", fps); - talloc_free(fps); - } - if (s && s->codec->channels.num) - APPEND(b, " %dch", s->codec->channels.num); - if (s && s->codec->samplerate) - APPEND(b, " %d Hz", s->codec->samplerate); - if (s && s->codec->bitrate) { - APPEND(b, " %d kbps", (s->codec->bitrate + 500) / 1000); - } else if (s && s->hls_bitrate) { - APPEND(b, " %d kbps", (s->hls_bitrate + 500) / 1000); - } - APPEND(b, ")"); - - bool first = true; - if (t->default_track) - ADD_FLAG(b, "default", first); - if (t->forced_track) - ADD_FLAG(b, "forced", first); - if (t->dependent_track) - ADD_FLAG(b, "dependent", first); - if (t->visual_impaired_track) - ADD_FLAG(b, "visual-impaired", first); - if (t->hearing_impaired_track) - ADD_FLAG(b, "hearing-impaired", first); - if (t->is_external) - ADD_FLAG(b, "external", first); - if (!first) - APPEND(b, "]"); + void *ctx = talloc_new(NULL); + APPEND(b, " %s", mp_format_track_metadata(ctx, t)); + talloc_free(ctx); MP_INFO(mpctx, "%s\n", b); }