Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions DOCS/man/input.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2130,10 +2130,10 @@ Property list
Whether ``--video-sync=display`` is actually active.

``filename``
Currently played file, with path stripped. If this is an URL, try to undo
percent encoding as well. (The result is not necessarily correct, but
looks better for display purposes. Use the ``path`` property to get an
unmodified filename.)
Currently played file, with path stripped, except URLs. The latter will be
returned in full with percent encoding undone. (The result is not
necessarily correct, but looks better for display purposes. Use the
``path`` property to get an unmodified filename.)

This has a sub-property:

Expand Down
18 changes: 15 additions & 3 deletions misc/path_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,27 @@ void mp_path_strip_trailing_separator(char *path)
char *mp_splitext(const char *path, bstr *root)
{
mp_assert(path);
int skip = (*path == '.'); // skip leading dot for "hidden" unix files
const char *split = strrchr(path + skip, '.');
if (!split || !split[1] || strchr(split, '/'))
char *bn = mp_basename(path);

// Skip all leading dots, not just for "hidden" unix files, otherwise we
// end up splitting a part of the filename sans leading dot.
while (*bn == '.')
bn++;

const char *split = strrchr(bn, '.');
if (!split || !split[1])
return NULL;
if (root)
*root = (bstr){(char *)path, split - path};
return (char *)split + 1;
}

char *mp_stripext(void *talloc_ctx, const char *s)
{
bstr root;
return mp_splitext(s, &root) ? bstrto0(talloc_ctx, root) : (char *)s;
}

bool mp_path_is_absolute(struct bstr path)
{
if (path.len && strchr(mp_path_separators, path.start[0]))
Expand Down
7 changes: 7 additions & 0 deletions misc/path_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
#include <stdbool.h>
#include "misc/bstr.h"

/* Return pointer to last path segment or the original argument if it is a URL
*/
#define mp_basename_or_url(p) (mp_is_url(bstr0(p)) ? (char *)p : mp_basename(p))

// Return pointer to filename part of path

char *mp_basename(const char *path);
Expand All @@ -33,6 +37,9 @@ char *mp_basename(const char *path);
*/
char *mp_splitext(const char *path, bstr *root);

// This is a shorthand to remove the extension
char *mp_stripext(void *talloc_ctx, const char *s);

/* Return struct bstr referencing directory part of path, or if that
* would be empty, ".".
*/
Expand Down
6 changes: 2 additions & 4 deletions player/command.c
Original file line number Diff line number Diff line change
Expand Up @@ -526,17 +526,15 @@ static int mp_property_filename(void *ctx, struct m_property *prop,
char *filename = talloc_strdup(NULL, mpctx->filename);
if (mp_is_url(bstr0(filename)))
mp_url_unescape_inplace(filename);
char *f = (char *)mp_basename(filename);
char *f = mp_basename_or_url(filename);
if (!f[0])
f = filename;
if (action == M_PROPERTY_KEY_ACTION) {
struct m_property_action_arg *ka = arg;
if (strcmp(ka->key, "no-ext") == 0) {
action = ka->action;
arg = ka->arg;
bstr root;
if (mp_splitext(f, &root))
f = bstrto0(filename, root);
f = mp_stripext(filename, f);
}
}
int r = m_property_strdup_ro(action, arg, f);
Expand Down
22 changes: 3 additions & 19 deletions player/screenshot.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,6 @@ void screenshot_init(struct MPContext *mpctx)
};
}

static char *stripext(void *talloc_ctx, const char *s)
{
const char *end = strrchr(s, '.');
if (!end)
end = s + strlen(s);
return talloc_asprintf(talloc_ctx, "%.*s", (int)(end - s), s);
}

static bool write_screenshot(struct mp_cmd_ctx *cmd, struct mp_image *img,
const char *filename, struct image_writer_opts *opts,
bool overwrite)
Expand Down Expand Up @@ -175,16 +167,11 @@ static char *create_fname(struct MPContext *mpctx, char *template,
}
case 'f':
case 'F': {
char *video_file = NULL;
if (mpctx->filename)
video_file = mp_basename(mpctx->filename);
char *video_file = mpctx->filename;
char *name = video_file ? mp_basename_or_url(video_file) : "NO_FILE";

if (!video_file)
video_file = "NO_FILE";

char *name = video_file;
if (fmt == 'F')
name = stripext(res, video_file);
name = mp_stripext(res, name);
append_filename(&res, name);
break;
}
Expand Down Expand Up @@ -300,9 +287,6 @@ static char *gen_fname(struct mp_cmd_ctx *cmd, const char *file_ext)
void *t = fname;
dir = mp_get_user_path(t, ctx->mpctx->global, dir);
fname = mp_path_join(NULL, dir, fname);

mp_mkdirp(dir);

talloc_free(t);
}

Expand Down
Loading