From dc8684805bdab433e93fdaad8d214c2b26332944 Mon Sep 17 00:00:00 2001 From: Andrey Morozov Date: Mon, 5 May 2014 18:52:50 -0300 Subject: command: rename stream-length to file-size, format file size Signed-off-by: wm4 --- DOCS/man/en/input.rst | 6 +++--- player/command.c | 57 +++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 47 insertions(+), 16 deletions(-) diff --git a/DOCS/man/en/input.rst b/DOCS/man/en/input.rst index 80655ee369..e895623b3d 100644 --- a/DOCS/man/en/input.rst +++ b/DOCS/man/en/input.rst @@ -569,6 +569,9 @@ Property list looks better for display purposes. Use the ``path`` property to get an unmodified filename.) +``file-size`` + Length in bytes of the source file/stream. + ``path`` Full path of the currently played file. @@ -599,9 +602,6 @@ Property list ``stream-end`` Raw end position in bytes in source stream. -``stream-length`` - Length in bytes of the source stream (``${stream-end} - ${stream-start}``). - ``stream-time-pos`` (RW) Time position in source stream. This only works for DVD and Bluray. This is probably never different from ``time-pos``, because ``time-pos`` is diff --git a/player/command.c b/player/command.c index 868291c043..b0f74219f7 100644 --- a/player/command.c +++ b/player/command.c @@ -109,6 +109,24 @@ static char *format_bitrate(int rate) return talloc_asprintf(NULL, "%d kbps", rate * 8 / 1000); } +static char *format_file_size(int64_t size) +{ + double s = size; + if (size < 1024) + return talloc_asprintf(NULL, "%.0f", s); + + if (size < (1024 * 1024)) + return talloc_asprintf(NULL, "%.3f Kb", s / (1024.0)); + + if (size < (1024 * 1024 * 1024)) + return talloc_asprintf(NULL, "%.3f Mb", s / (1024.0 * 1024.0)); + + if (size < (1024LL * 1024LL * 1024LL * 1024LL)) + return talloc_asprintf(NULL, "%.3f Gb", s / (1024.0 * 1024.0 * 1024.0)); + + return talloc_asprintf(NULL, "%.3f Tb", s / (1024.0 * 1024.0 * 1024.0 * 1024.0)); +} + static char *format_delay(double time) { return talloc_asprintf(NULL, "%d ms", ROUND(time * 1000)); @@ -182,6 +200,30 @@ static int mp_property_filename(m_option_t *prop, int action, void *arg, return r; } +static int mp_property_file_size(m_option_t *prop, int action, void *arg, + void *ctx) +{ + MPContext *mpctx = ctx; + if (!mpctx->stream) + return M_PROPERTY_UNAVAILABLE; + + int64_t size = mpctx->stream->end_pos - mpctx->stream->start_pos; + + switch (action) { + case M_PROPERTY_GET: { + if (size <= 0) + break; + *(int64_t *)arg = size; + return M_PROPERTY_OK; + } + case M_PROPERTY_PRINT: { + *(char **)arg = format_file_size(size); + return M_PROPERTY_OK; + } + } + return M_PROPERTY_NOT_IMPLEMENTED; +} + static int mp_property_media_title(m_option_t *prop, int action, void *arg, MPContext *mpctx) { @@ -276,17 +318,6 @@ static int mp_property_stream_end(m_option_t *prop, int action, void *arg, return m_property_int64_ro(prop, action, arg, stream->end_pos); } -/// Stream length (RO) -static int mp_property_stream_length(m_option_t *prop, int action, - void *arg, MPContext *mpctx) -{ - struct stream *stream = mpctx->stream; - if (!stream) - return M_PROPERTY_UNAVAILABLE; - return m_property_int64_ro(prop, action, arg, - stream->end_pos - stream->start_pos); -} - // Does some magic to handle "/full" as time formatted with milliseconds. // Assumes prop is the type of the actual property. static int property_time(m_option_t *prop, int action, void *arg, double time) @@ -2268,6 +2299,8 @@ static const m_option_t mp_properties[] = { M_OPTION_PROPERTY_CUSTOM("speed", mp_property_playback_speed), { "filename", mp_property_filename, CONF_TYPE_STRING, 0, 0, 0, NULL }, + { "file-size", mp_property_file_size, CONF_TYPE_INT64, + M_OPT_MIN, 0, 0, NULL }, { "path", mp_property_path, CONF_TYPE_STRING, 0, 0, 0, NULL }, { "media-title", mp_property_media_title, CONF_TYPE_STRING, @@ -2283,8 +2316,6 @@ static const m_option_t mp_properties[] = { M_OPT_MIN, 0, 0, NULL }, { "stream-end", mp_property_stream_end, CONF_TYPE_INT64, M_OPT_MIN, 0, 0, NULL }, - { "stream-length", mp_property_stream_length, CONF_TYPE_INT64, - M_OPT_MIN, 0, 0, NULL }, { "stream-time-pos", mp_property_stream_time_pos, CONF_TYPE_TIME, M_OPT_MIN, 0, 0, NULL }, { "length", mp_property_length, CONF_TYPE_TIME, -- cgit v1.2.3