Skip to content

Commit 98a560c

Browse files
committed
[cli] Add tests for save-qp command
1 parent 30dce12 commit 98a560c

File tree

5 files changed

+41
-12
lines changed

5 files changed

+41
-12
lines changed

scenedetect.cfg

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,9 @@
291291
# Folder to output QP file to. Overrides [global] output option.
292292
#output = /usr/tmp/images
293293

294+
# Disable shifting frame numbers by start time (yes/no).
295+
#disable-shift = no
296+
294297

295298
#
296299
# BACKEND OPTIONS

scenedetect/_cli/__init__.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1467,6 +1467,15 @@ def save_images_command(
14671467
help="Output directory to save QP file to. Overrides global option -o/--output.%s"
14681468
% (USER_CONFIG.get_help_string("save-qp", "output", show_default=False)),
14691469
)
1470+
@click.option(
1471+
"--disable-shift",
1472+
"-d",
1473+
is_flag=True,
1474+
flag_value=True,
1475+
default=None,
1476+
help="Disable shifting frame numbers by start time.%s"
1477+
% (USER_CONFIG.get_help_string("save-qp", "disable-shift")),
1478+
)
14701479
@click.pass_context
14711480
def save_qp_command(
14721481
ctx: click.Context,
@@ -1483,7 +1492,7 @@ def save_qp_command(
14831492
save_qp_args = {
14841493
"filename_format": ctx.config.get_value("save-qp", "filename", filename),
14851494
"output_dir": ctx.config.get_value("save-qp", "output", output),
1486-
"disable_shift": ctx.config.get_value("save-qp", "disable_shift", disable_shift),
1495+
"shift_start": not ctx.config.get_value("save-qp", "disable-shift", disable_shift),
14871496
}
14881497
ctx.add_command(cli_commands.save_qp, save_qp_args)
14891498

scenedetect/_cli/commands.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,21 +70,19 @@ def save_qp(
7070
cuts: CutList,
7171
output_dir: str,
7272
filename_format: str,
73-
disable_shift: bool,
73+
shift_start: bool,
7474
):
7575
"""Handler for the `save-qp` command."""
7676
del scenes # We only use cuts for this handler.
77-
7877
qp_path = get_and_create_path(
7978
Template(filename_format).safe_substitute(VIDEO_NAME=context.video_stream.name),
8079
output_dir,
8180
)
82-
# We assume that if a start time was set, the user will encode the video starting from that
83-
# point, so we shift all frame numbers such that the QP file always starts from frame 0.
81+
start_frame = context.start_time.frame_num if context.start_time else 0
82+
offset = start_frame if shift_start else 0
8483
with open(qp_path, "wt") as qp_file:
85-
qp_file.write("0 I -1\n")
84+
qp_file.write(f"{0 if shift_start else start_frame} I -1\n")
8685
# Place another I frame at each detected cut.
87-
offset = 0 if context.start_time is None else context.start_time.frame_num
8886
qp_file.writelines(f"{cut.frame_num - offset} I -1\n" for cut in cuts)
8987
logger.info(f"QP file written to: {qp_path}")
9088

scenedetect/_cli/config.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -301,10 +301,6 @@ def format(self, timecode: FrameTimecode) -> str:
301301
"image-width": 0,
302302
"no-images": False,
303303
},
304-
"save-qp": {
305-
"filename": "$VIDEO_NAME.qp",
306-
"output": None,
307-
},
308304
"list-scenes": {
309305
"cut-format": "timecode",
310306
"display-cuts": True,
@@ -340,6 +336,11 @@ def format(self, timecode: FrameTimecode) -> str:
340336
"scale-method": "linear",
341337
"width": 0,
342338
},
339+
"save-qp": {
340+
"disable-shift": False,
341+
"filename": "$VIDEO_NAME.qp",
342+
"output": None,
343+
},
343344
"split-video": {
344345
"args": DEFAULT_FFMPEG_ARGS,
345346
"copy": False,

tests/test_cli.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ def test_cli_save_qp(tmp_path: Path):
450450
assert output_path.read_text() == EXPECTED_QP_CONTENTS[1:]
451451

452452

453-
def test_cli_save_qp_start_shift(tmp_path: Path):
453+
def test_cli_save_qp_start_offset(tmp_path: Path):
454454
"""Test `save-qp` command but using a shifted start time."""
455455
# The QP file should always start from frame 0, so we expect a similar result to the above, but
456456
# with the frame numbers shifted by the start frame. Note that on the command-line, the first
@@ -473,6 +473,24 @@ def test_cli_save_qp_start_shift(tmp_path: Path):
473473
assert output_path.read_text() == EXPECTED_QP_CONTENTS[1:]
474474

475475

476+
def test_cli_save_qp_no_shift(tmp_path: Path):
477+
"""Test `save-qp` command with start time shifting disabled."""
478+
EXPECTED_QP_CONTENTS = """
479+
50 I -1
480+
90 I -1
481+
"""
482+
assert (
483+
invoke_scenedetect(
484+
"-i {VIDEO} time -s 51 -e 95 {DETECTOR} save-qp --disable-shift",
485+
output_dir=tmp_path,
486+
)
487+
== 0
488+
)
489+
output_path = tmp_path.joinpath(f"{DEFAULT_VIDEO_NAME}.qp")
490+
assert os.path.exists(output_path)
491+
assert output_path.read_text() == EXPECTED_QP_CONTENTS[1:]
492+
493+
476494
@pytest.mark.parametrize("backend_type", ALL_BACKENDS)
477495
def test_cli_backend(backend_type: str):
478496
"""Test setting the `-b`/`--backend` argument."""

0 commit comments

Comments
 (0)