Skip to content

Conversation

@WhitePeter
Copy link

@WhitePeter WhitePeter commented Nov 7, 2025

URLs can end with trailing slashes (/) which in turn results in GNU basename[1] returning the empty string. Catch that case and fallback on the raw value of mpctx->filename. Subsequent path sanitation takes care of translating invalid path component chars.

Issue reproduction steps:

mpv \
    --screenshot-dir=$HOME/mpv-shots \
    --screenshot-template='%f/%P' \
    https://example.org/video/

This would result in %f expanding to '' and thus render screenshot-template an absolute path which, for some reason, would in turn take precedence of screenshot-dir and hence result in a non-writeable path, i.e. /timestamp.ext.

With this new approach the resulting path looks like this:
/home/user/mpv-shots/http:__example.org_video_/timestamp.ext

Not particularly pretty but less ambiguous and more consistent over a diverse range of input paths. For transparency and reference also see [2] which sadly could not be revived.

[1] #14635 (comment)
[2] #17015

Read this before you submit this pull request:
https://github.com/mpv-player/mpv/blob/master/DOCS/contribute.md

Check!

@WhitePeter
Copy link
Author

WhitePeter commented Nov 7, 2025

I also changed the mp_basename to a (GNU) basename call, just for kicks. Which begs the question: is there a particular reason to re-implement it? Because from my POV, all the prerequisites for GNU base-/dirname seem to be met; source is _GNU_SOURCE and string.h is included virtually everywhere.

This just an aside, though. If you want mp_basename, I will comply. But this looks like an opportunity to trim the code base. If there is interest I might be willing to replace all the mp_(basename|dirname) calls; in a separate PR, of course.

@guidocella
Copy link
Contributor

According to DOCS/contribute.md GNU features should not be used and it may not work on Windows.

@WhitePeter WhitePeter force-pushed the fix-screenshot-template-f-expansion branch from 7fd0aa5 to 98e477a Compare November 7, 2025 17:21
@WhitePeter
Copy link
Author

I am a little embarassed for missing that. I grepped for _GNU_SOURCE and saw it as a meson build default. Sorry!

@WhitePeter WhitePeter force-pushed the fix-screenshot-template-f-expansion branch from 98e477a to 896d9d2 Compare November 7, 2025 17:33
URLs can end with trailing slashes (/) which in turn results in GNU
basename[1] returning the empty string. Catch that case and fallback on
the raw value of mpctx->filename. Subsequent path sanitation takes care
of translating invalid path component chars.

Issue reproduction steps:
```
mpv \
    --screenshot-dir=$HOME/mpv-shots \
    --screenshot-template='%f/%P' \
    https://example.org/video/
```
This would result in %f expanding to '' and thus render
screenshot-template an absolute path which, for some reason, would in
turn take precedence of screenshot-dir and hence result in a
non-writeable path, i.e. `/timestamp.ext`.

With this new approach the resulting path looks like this:
    `/home/user/mpv-shots/http:__example.org_video_/timestamp.ext`

Not particularly pretty but less ambiguous and more consistent over a
diverse range of input paths.

[1] mpv-player#14635 (comment)
There is no reason to create the screenshot dir separately, because the
final path starts with it. So defer creating directories to the last
possible moment with all the information necessary.
@WhitePeter WhitePeter force-pushed the fix-screenshot-template-f-expansion branch from 896d9d2 to a5f29e5 Compare November 7, 2025 17:51
@WhitePeter
Copy link
Author

Sorry for the last force push, had to rewrite history.

@WhitePeter WhitePeter marked this pull request as ready for review November 7, 2025 18:14
guidocella added a commit to guidocella/mpv that referenced this pull request Nov 7, 2025
When the path is a URL, use the whole path as basename. The dirname will
be "." in this case.

This simplifies various checks throughout the codebase. In particular,
it avoids returning an empty string for URLs ending with /.

The filename property will return full URLs, which is desirable because
the domain and path segments before the last are useful information.
This fixes half of mpv-player#10975. The empty string check in
mp_property_filename() can be removed because it only happened with the
basename of URLs ending with /. mp_property_filename() will not be
called with directories with trailing slashes because mpv expands them
immediately when they are the current playlist entry; I tested it by
observing filename. Also mpv-player#16896 will prevent directories with trailing
slashes anyway.

This also fixes the issue described in mpv-player#17015 of %f in
--screenshot-template being evaluated to an empty string for URLs ending
with /, which can inadvertently make it use an absolute path.

Alternative to mpv-player#16932 and mpv-player#17021.
guidocella added a commit to guidocella/mpv that referenced this pull request Nov 7, 2025
When the path is a URL, use the whole path as basename. The dirname will
be "." in this case.

This simplifies various checks throughout the codebase. In particular,
it avoids returning an empty string for URLs ending with /.

The filename property will return full URLs, which is desirable because
the domain and path segments before the last are useful information.
This fixes half of mpv-player#10975.

This also fixes the issue described in mpv-player#17015 of %f in
--screenshot-template being evaluated to an empty string for URLs ending
with /, which can inadvertently make it use an absolute path.

Alternative to mpv-player#16932 and mpv-player#17021.
This introduces a simple wrapper around `mp_basename`. It returns URLs
unchanged. Especially with filename representations of streaming service
URLs the last path segment, which usually points to a file in most other
contexts, is becoming ever less meaningful. So provide this to
conveniently use URLs as-is where usually a file name would be expected.

Some caution is advised, because components that expect no '/' may get
tripped up.

Credit goes to @guidocella for the idea.
@github-actions
Copy link

github-actions bot commented Nov 8, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants