Skip to content

Commit 58c6c15

Browse files
committed
[video_splitter] Fix inconsistent naming in output paths
1 parent 3ee41b5 commit 58c6c15

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-6
lines changed

scenedetect/_cli/commands.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,9 @@ def split_video(
215215
"""Handles the `split-video` command."""
216216
del cuts # split-video only uses scenes.
217217

218+
if use_mkvmerge:
219+
name_format = name_format.removesuffix("-$SCENE_NUMBER")
220+
218221
# Add proper extension to filename template if required.
219222
dot_pos = name_format.rfind(".")
220223
extension_length = 0 if dot_pos < 0 else len(name_format) - (dot_pos + 1)

scenedetect/video_splitter.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ def default_formatter(template: str) -> PathFormatter:
156156
def split_video_mkvmerge(
157157
input_video_path: str,
158158
scene_list: ty.Iterable[TimecodePair],
159-
output_dir: ty.Optional[Path] = None,
160-
output_file_template: str = "$VIDEO_NAME.mkv",
159+
output_dir: ty.Optional[ty.Union[str, Path]] = None,
160+
output_file_template: ty.Optional[ty.Union[str, Path]] = "$VIDEO_NAME.mkv",
161161
video_name: ty.Optional[str] = None,
162162
show_output: bool = False,
163163
suppress_output=None,
@@ -202,8 +202,13 @@ def split_video_mkvmerge(
202202
output_path = template.safe_substitute(VIDEO_NAME=video_name)
203203
if output_dir:
204204
output_path = Path(output_dir) / output_path
205-
Path(output_path).parent.mkdir(parents=True, exist_ok=True)
205+
output_path = Path(output_path)
206206
logger.info(f"Splitting video with mkvmerge, path template: {output_path}")
207+
# If there is only one scene, mkvmerge omits the suffix for the output. To make the filenames
208+
# consistent with the output when there are multiple scenes present, we append "-001".
209+
if len(scene_list) == 1:
210+
output_path = output_path.with_stem(output_path.stem + "-001")
211+
output_path.parent.mkdir(parents=True, exist_ok=True)
207212

208213
call_list = ["mkvmerge"]
209214
if not show_output:

tests/test_cli.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -445,25 +445,44 @@ def test_cli_split_video_mkvmerge(tmp_path: Path):
445445
)
446446
== 0
447447
)
448+
for scene in range(DEFAULT_NUM_SCENES):
449+
path = tmp_path / (Path(DEFAULT_VIDEO_PATH).stem + f"-Scene-{1 + scene:03d}.mkv")
450+
path.unlink(missing_ok=False)
451+
# If only one scene (just using a few frames), should keep same output template.
452+
assert (
453+
invoke_scenedetect(
454+
"-i {VIDEO} -s {STATS} time -e 3 {DETECTOR} split-video -m", output_dir=tmp_path
455+
)
456+
== 0
457+
)
458+
path = tmp_path / (Path(DEFAULT_VIDEO_PATH).stem + "-Scene-001.mkv")
459+
path.unlink(missing_ok=False)
460+
# -m takes precedence over -c
448461
assert (
449462
invoke_scenedetect(
450463
"-i {VIDEO} -s {STATS} time {TIME} {DETECTOR} split-video -m -c", output_dir=tmp_path
451464
)
452465
== 0
453466
)
467+
# Custom filename format
468+
for scene in range(DEFAULT_NUM_SCENES):
469+
path = tmp_path / (Path(DEFAULT_VIDEO_PATH).stem + f"-Scene-{1 + scene:03d}.mkv")
470+
path.unlink(missing_ok=False)
454471
assert (
455472
invoke_scenedetect(
456-
'-i {VIDEO} -s {STATS} time {TIME} {DETECTOR} split-video -m -f "test$VIDEO_NAME"',
473+
"-i {VIDEO} -s {STATS} time {TIME} {DETECTOR} split-video -m -f test$VIDEO_NAME",
457474
output_dir=tmp_path,
458475
)
459476
== 0
460477
)
478+
for scene in range(DEFAULT_NUM_SCENES):
479+
path = tmp_path / ("test" + Path(DEFAULT_VIDEO_PATH).stem + f"-{1 + scene:03d}.mkv")
480+
path.unlink(missing_ok=False)
461481
# -a/--args and -m/--mkvmerge are mutually exclusive
462482
assert invoke_scenedetect(
463483
'-i {VIDEO} -s {STATS} time {TIME} {DETECTOR} split-video -m -a "-c:v libx264"',
464484
output_dir=tmp_path,
465485
)
466-
# TODO: Check for existence of split video files.
467486

468487

469488
def test_cli_save_images(tmp_path: Path):

website/pages/changelog.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,4 +624,6 @@ Development
624624

625625
## PySceneDetect 0.6.6 (In Development)
626626

627-
- [bugfix] Fix crash when using `-m`/`--mkvmerge` flag with `split-video` command [#473](https://github.com/Breakthrough/PySceneDetect/issues/473)
627+
- [bugfix] Fix crash when using `split-video` with `-m`/`--mkvmerge` option [#473](https://github.com/Breakthrough/PySceneDetect/issues/473)
628+
- [bugfix] Fix incorrect default filename template for `split-video` command with `-m`/`--mkvmerge` option
629+
- [bugfix] Fix inconsistent filenames when using `split_video_mkvmerge()` function in `scenedetect.video_splitter` module

0 commit comments

Comments
 (0)