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
6 changes: 6 additions & 0 deletions scenedetect/_cli/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,12 @@ def _open_video_stream(
frame_rate=frame_rate,
backend=backend,
)
if not self.video_stream.name:
logger.warning(
"Could not determine a video name from the input path, so $VIDEO_NAME will be"
" empty in output filenames. Set an explicit filename on the output command"
" (e.g. `list-scenes -f scenes.csv`) to avoid this."
)
duration = self.video_stream.duration
duration_str = f"{duration} ({duration.frame_num} frames)" if duration else "unknown"
rate = self.video_stream.frame_rate
Expand Down
28 changes: 26 additions & 2 deletions scenedetect/platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import string
import subprocess
import sys
import urllib.parse

import cv2

Expand Down Expand Up @@ -129,11 +130,34 @@ def _get_cv2_param(param_name: str) -> int | None:
##


def is_url(path: StrPath) -> bool:
"""True if `path` looks like a URL or network stream (e.g. https://..., rtsp://...)."""
return "://" in os.fspath(path)


def get_file_name(file_path: StrPath, include_extension: bool = True) -> str:
"""Return the file name that `file_path` refers to, optionally removing the extension.

E.g. /tmp/foo.bar -> foo"""
file_name = os.path.basename(os.fspath(file_path))
E.g. /tmp/foo.bar -> foo

URLs use the last non-empty segment of the path component, ignoring any query string or
fragment, so https://example.com/videos/clip.mp4?token=1.2 -> clip. If the URL has no path
(e.g. rtsp://192.168.0.1:554), the host is used instead, with the port separator replaced
so the result is a valid file name on all platforms. Returns an empty string only if no
usable name can be derived."""
file_path = os.fspath(file_path)
if is_url(file_path):
parsed = urllib.parse.urlsplit(file_path)
segments = [urllib.parse.unquote(seg) for seg in parsed.path.split("/") if seg]
if not segments:
# No path to take a name from, so fall back to the host (never the credentials). A
# host has no extension to remove: stripping after the last "." would turn
# example.com into example.
host = parsed.hostname or ""
return f"{host}_{parsed.port}" if parsed.port else host
file_name = segments[-1]
else:
file_name = os.path.basename(file_path)
if not include_extension:
last_dot_pos = file_name.rfind(".")
if last_dot_pos >= 0:
Expand Down
46 changes: 45 additions & 1 deletion tests/test_platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import pytest

from scenedetect.platform import CommandTooLong, invoke_command
from scenedetect.platform import CommandTooLong, get_file_name, invoke_command, is_url


def test_invoke_command():
Expand All @@ -38,3 +38,47 @@ def test_long_command():
if platform.system() == "Windows":
with pytest.raises(CommandTooLong):
invoke_command(["x" * 2**15])


@pytest.mark.parametrize(
("path", "expected"),
[
("/tmp/foo.bar", "foo"),
("foo.bar", "foo"),
("foo", "foo"),
("https://example.com/videos/clip.mp4", "clip"),
# Query strings and fragments are not part of the name, even if they contain a "." or "/".
("https://example.com/videos/clip.mp4?token=1.2&dir=a/b", "clip"),
("https://example.com/videos/clip.mp4#t=10", "clip"),
("https://example.com/videos/clip.mp4/", "clip"),
("https://example.com/videos/my%20clip.mp4", "my clip"),
("rtsp://user:pass@192.168.0.1:554/live/stream1", "stream1"),
# No path component: fall back to the host, which must stay a valid file name.
("https://example.com", "example.com"),
("https://example.com/", "example.com"),
("rtsp://192.168.0.1:554", "192.168.0.1_554"),
("rtsp://user:pass@192.168.0.1:554", "192.168.0.1_554"),
],
)
def test_get_file_name_without_extension(path: str, expected: str):
assert get_file_name(path, include_extension=False) == expected


@pytest.mark.parametrize(
("path", "expected"),
[
("/tmp/foo.bar", "foo.bar"),
("https://example.com/videos/clip.mp4?token=1", "clip.mp4"),
("https://example.com/", "example.com"),
],
)
def test_get_file_name_with_extension(path: str, expected: str):
assert get_file_name(path, include_extension=True) == expected


def test_is_url():
assert is_url("https://example.com/clip.mp4")
assert is_url("rtsp://192.168.0.1:554/live")
assert not is_url("/tmp/clip.mp4")
assert not is_url(r"C:\videos\clip.mp4")
assert not is_url("img_%04d.jpg")
1 change: 1 addition & 0 deletions website/pages/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

### Unreleased

- [bugfix] `$VIDEO_NAME` is now derived from the path component of URL inputs, ignoring the query string and fragment (e.g. `https://host/clip.mp4?token=1.2` -> `clip`). URLs with no path use the host. Previously the query string leaked into output filenames, which fails on Windows. [#197](https://github.com/Breakthrough/PySceneDetect/issues/197)
- [bugfix] OTIO exports use the full source duration when provided, including from the CLI, instead of declaring a fixed 1,980-frame duration. The available range remains unspecified when the duration is unknown. Scene source ranges are unchanged. [#580](https://github.com/Breakthrough/PySceneDetect/pull/580)

### PySceneDetect 0.7.2 (October 11, 2026)
Expand Down