Skip to content

Conversation

@guidocella
Copy link
Contributor

@guidocella guidocella commented 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 #10975.

This also fixes the issue described in #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 #16932 and #17021.

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.
@WhitePeter
Copy link

WhitePeter commented Nov 7, 2025

This does alter the semantics of mp_basename, though. Maybe this warrants renaming to mp_basename_or_url, or something similar? Or wrap mp_basename in said new function, and encourage use of the new one? Who knows, pure GNU basename may have other uses? Proper naming will help to not confuse tourists like me. ;)

@guidocella
Copy link
Contributor Author

Use by who? This is an internal mpv function. And using the same function only simplifies the code already using it by just removing conditions, no calls in the codebase benefitted from returning the basename of URLs.

@WhitePeter
Copy link

WhitePeter commented Nov 8, 2025

Fair enough. I was just musing about people not intimately familiar with the code base. Seeing something that says basename and not looking at its implementation may yield unexpected results. I am thinking about the concept of encapsulation when I talk about not looking at the code of a until function. Ideally, what it does is obvious from the outside, so it might as well be black box.
I am also a bit spoiled by Python, I guess. Lightweight wrappers are commonplace. And having a mp_basename_or_url, or mp_bn_or_url wrapper that only short circuits on URLs and otherwise calls mp_basename, is not much additional code. I think I'll do that in my PR, which you kindly referenced.

@guidocella
Copy link
Contributor Author

It doesn't matter what's expected when everywhere in the codebase benefits from keeping full URLs, including the implementation of mp_basename itself where the URL check can be removed. There would be no user of a separate function.

@WhitePeter
Copy link

But there may be. Imagine some passerby wanting to contribute something related. They can find a monolithic function that does everything perfectly for a narrow set of problem or they can find a wrapper that does exactly the same, but its building block(s), with well defined behavior in case of GNU basename, there for the taking.

char *mp_basename(const char *path)
{
if (mp_is_url(bstr0(path)))
return (char *)path;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as an aside: why does this function not return a const char*?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently it did until 962eec0 with no explanation for the change. Anyway, I can change it after this PR.

Before the previous commit stripext(), used for %F, was passed the
basename of URLs. Now that full URLs are passed, make it use mp_splitext
which handles extensions in URLs correctly, e.g. with
"https://example.com/foo" it returns NULL instead of "com/foo", and it
is even less code than before.

${filename/no-ext} was already using mp_splitext().
@github-actions
Copy link

github-actions bot commented Nov 9, 2025

@guidocella
Copy link
Contributor Author

guidocella commented Nov 9, 2025

The problem with this idea is that mp_basename also affects mp_dirname and using . as dirname for URLs breaks concatenating relative paths in EDL URLs. I assume cue and mkv_timeline/ordered chapters have the same issue if they even work with URLs. It also affects m3u8 URLs with --playlist=http...m3u (else they use the lavf demuxer). Marking as draft in favor of #17021. I can change mp_property_playlist and configfiles.c after that.

@guidocella guidocella marked this pull request as draft November 9, 2025 09:30
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