command: print track metadata in ${track-list}

Extract track metadata formatting out of loadfile.c to reuse in the
track-list property.
This commit is contained in:
Guido Cella 2024-10-24 11:04:28 +02:00 committed by Kacper Michajłow
parent 1bee15d5da
commit 3ea8d751f5
3 changed files with 62 additions and 53 deletions

View File

@ -76,8 +76,6 @@
#include "osdep/subprocess.h" #include "osdep/subprocess.h"
#include "osdep/terminal.h" #include "osdep/terminal.h"
#include "core.h"
#ifdef _WIN32 #ifdef _WIN32
#include <windows.h> #include <windows.h>
#endif #endif
@ -2104,21 +2102,70 @@ static const char *track_type_name(struct track *t)
return NULL; return NULL;
} }
static char *append_track_info(char *res, struct track *track) static char *append_track_info(char *res, struct track *track)
{ {
res = talloc_strdup_append(res, track->selected ? list_current : list_normal); res = talloc_strdup_append(res, track->selected ? list_current : list_normal);
res = talloc_asprintf_append(res, "(%d) ", track->user_tid); res = talloc_asprintf_append(res, "(%d) ", track->user_tid);
if (track->title) res = talloc_strdup_append(res, mp_format_track_metadata(res, track));
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) ");
return res; 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 : "<unknown>");
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, static int property_list_tracks(void *ctx, struct m_property *prop,
int action, void *arg) int action, void *arg)
{ {

View File

@ -20,6 +20,7 @@
#include <stdbool.h> #include <stdbool.h>
#include "core.h"
#include "libmpv/client.h" #include "libmpv/client.h"
#include "osdep/compiler.h" #include "osdep/compiler.h"
@ -121,4 +122,6 @@ void mark_seek(struct MPContext *mpctx);
void mp_abort_cache_dumping(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 */ #endif /* MPLAYER_COMMAND_H */

View File

@ -238,14 +238,9 @@ static void uninit_demuxer(struct MPContext *mpctx)
#define WHITE_CIRCLE "\xe2\x97\x8b" #define WHITE_CIRCLE "\xe2\x97\x8b"
#define APPEND(s, ...) mp_snprintf_cat(s, sizeof(s), __VA_ARGS__) #define APPEND(s, ...) mp_snprintf_cat(s, sizeof(s), __VA_ARGS__)
#define FILL(s, n) mp_snprintf_cat(s, sizeof(s), "%*s", n, "") #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) static void print_stream(struct MPContext *mpctx, struct track *t, bool indent)
{ {
struct sh_stream *s = t->stream;
const char *tname = "?"; const char *tname = "?";
const char *selopt = "?"; const char *selopt = "?";
const char *langopt = "?"; const char *langopt = "?";
@ -277,46 +272,10 @@ static void print_stream(struct MPContext *mpctx, struct track *t, bool indent)
} else if (max_lang_length) { } else if (max_lang_length) {
FILL(b, (int) strlen(" --alang= ") + 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; void *ctx = talloc_new(NULL);
APPEND(b, " (%s", codec ? codec : "<unknown>"); APPEND(b, " %s", mp_format_track_metadata(ctx, t));
if (s && s->codec->codec_profile) talloc_free(ctx);
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, "]");
MP_INFO(mpctx, "%s\n", b); MP_INFO(mpctx, "%s\n", b);
} }