Skip to content

Commit 3e6bc19

Browse files
qwist1233-cpuBreakthrough
authored andcommitted
Address review: terser warning, restructured get_file_name docstring
1 parent 6d75bef commit 3e6bc19

3 files changed

Lines changed: 26 additions & 8 deletions

File tree

‎scenedetect/_cli/context.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -540,8 +540,7 @@ def _open_video_stream(
540540
if not self.video_stream.name:
541541
logger.warning(
542542
"Could not determine a video name from the input path, so $VIDEO_NAME will be"
543-
" empty in output filenames. Set an explicit filename on the output command"
544-
" (e.g. `list-scenes -f scenes.csv`) to avoid this."
543+
" empty in output filenames."
545544
)
546545
duration = self.video_stream.duration
547546
duration_str = f"{duration} ({duration.frame_num} frames)" if duration else "unknown"

‎scenedetect/platform.py‎

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,27 @@ def get_file_name(file_path: StrPath, include_extension: bool = True) -> str:
140140
141141
E.g. /tmp/foo.bar -> foo
142142
143-
URLs use the last non-empty segment of the path component, ignoring any query string or
144-
fragment, so https://example.com/videos/clip.mp4?token=1.2 -> clip. If the URL has no path
145-
(e.g. rtsp://192.168.0.1:554), the host is used instead, with the port separator replaced
146-
so the result is a valid file name on all platforms. Returns an empty string only if no
147-
usable name can be derived."""
143+
URL and URI-like inputs:
144+
Inputs containing "://" are treated as URLs (see :func:`is_url`) and handled by their
145+
URL components rather than as paths, since a path separator inside a query string or
146+
fragment is not part of the name. The name is the last non-empty segment of the path
147+
component, percent-decoded, e.g. https://example.com/videos/clip.mp4?token=1.2 -> clip.
148+
149+
When the path component is empty there is no segment to use, so the host becomes the
150+
name, e.g. rtsp://192.168.0.1:554 -> 192.168.0.1_554. Userinfo (credentials) is never
151+
part of the result, and the port is joined with "_" because ":" is not valid in a file
152+
name on Windows. Hosts are returned in full even when `include_extension` is False, as
153+
the last "." in a host is a domain separator and not an extension.
154+
155+
Arguments:
156+
file_path: Path, or URL, to get the name of.
157+
include_extension: If False, remove the extension (the last "." and everything after
158+
it) from the name. Has no effect on names derived from a URL host.
159+
160+
Returns:
161+
The name, or an empty string if none can be derived (e.g. a path that is only
162+
separators, or a URL with neither a path nor a host).
163+
"""
148164
file_path = os.fspath(file_path)
149165
if is_url(file_path):
150166
parsed = urllib.parse.urlsplit(file_path)
@@ -154,7 +170,7 @@ def get_file_name(file_path: StrPath, include_extension: bool = True) -> str:
154170
# host has no extension to remove: stripping after the last "." would turn
155171
# example.com into example.
156172
host = parsed.hostname or ""
157-
return f"{host}_{parsed.port}" if parsed.port else host
173+
return f"{host}_{parsed.port}" if host and parsed.port else host
158174
file_name = segments[-1]
159175
else:
160176
file_name = os.path.basename(file_path)

‎tests/test_platform.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ def test_long_command():
5858
("https://example.com/", "example.com"),
5959
("rtsp://192.168.0.1:554", "192.168.0.1_554"),
6060
("rtsp://user:pass@192.168.0.1:554", "192.168.0.1_554"),
61+
# Neither a path nor a host to derive a name from.
62+
("rtsp://", ""),
63+
("rtsp://:554", ""),
6164
],
6265
)
6366
def test_get_file_name_without_extension(path: str, expected: str):

0 commit comments

Comments
 (0)