Skip to content

Commit

Permalink
[cli] Add tests for save-qp command
Browse files Browse the repository at this point in the history
  • Loading branch information
Breakthrough committed Oct 20, 2024
1 parent 30dce12 commit 98a560c
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 12 deletions.
3 changes: 3 additions & 0 deletions scenedetect.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,9 @@
# Folder to output QP file to. Overrides [global] output option.
#output = /usr/tmp/images

# Disable shifting frame numbers by start time (yes/no).
#disable-shift = no


#
# BACKEND OPTIONS
Expand Down
11 changes: 10 additions & 1 deletion scenedetect/_cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1467,6 +1467,15 @@ def save_images_command(
help="Output directory to save QP file to. Overrides global option -o/--output.%s"
% (USER_CONFIG.get_help_string("save-qp", "output", show_default=False)),
)
@click.option(
"--disable-shift",
"-d",
is_flag=True,
flag_value=True,
default=None,
help="Disable shifting frame numbers by start time.%s"
% (USER_CONFIG.get_help_string("save-qp", "disable-shift")),
)
@click.pass_context
def save_qp_command(
ctx: click.Context,
Expand All @@ -1483,7 +1492,7 @@ def save_qp_command(
save_qp_args = {
"filename_format": ctx.config.get_value("save-qp", "filename", filename),
"output_dir": ctx.config.get_value("save-qp", "output", output),
"disable_shift": ctx.config.get_value("save-qp", "disable_shift", disable_shift),
"shift_start": not ctx.config.get_value("save-qp", "disable-shift", disable_shift),
}
ctx.add_command(cli_commands.save_qp, save_qp_args)

Expand Down
10 changes: 4 additions & 6 deletions scenedetect/_cli/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,19 @@ def save_qp(
cuts: CutList,
output_dir: str,
filename_format: str,
disable_shift: bool,
shift_start: bool,
):
"""Handler for the `save-qp` command."""
del scenes # We only use cuts for this handler.

qp_path = get_and_create_path(
Template(filename_format).safe_substitute(VIDEO_NAME=context.video_stream.name),
output_dir,
)
# We assume that if a start time was set, the user will encode the video starting from that
# point, so we shift all frame numbers such that the QP file always starts from frame 0.
start_frame = context.start_time.frame_num if context.start_time else 0
offset = start_frame if shift_start else 0
with open(qp_path, "wt") as qp_file:
qp_file.write("0 I -1\n")
qp_file.write(f"{0 if shift_start else start_frame} I -1\n")
# Place another I frame at each detected cut.
offset = 0 if context.start_time is None else context.start_time.frame_num
qp_file.writelines(f"{cut.frame_num - offset} I -1\n" for cut in cuts)
logger.info(f"QP file written to: {qp_path}")

Expand Down
9 changes: 5 additions & 4 deletions scenedetect/_cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,10 +301,6 @@ def format(self, timecode: FrameTimecode) -> str:
"image-width": 0,
"no-images": False,
},
"save-qp": {
"filename": "$VIDEO_NAME.qp",
"output": None,
},
"list-scenes": {
"cut-format": "timecode",
"display-cuts": True,
Expand Down Expand Up @@ -340,6 +336,11 @@ def format(self, timecode: FrameTimecode) -> str:
"scale-method": "linear",
"width": 0,
},
"save-qp": {
"disable-shift": False,
"filename": "$VIDEO_NAME.qp",
"output": None,
},
"split-video": {
"args": DEFAULT_FFMPEG_ARGS,
"copy": False,
Expand Down
20 changes: 19 additions & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ def test_cli_save_qp(tmp_path: Path):
assert output_path.read_text() == EXPECTED_QP_CONTENTS[1:]


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


def test_cli_save_qp_no_shift(tmp_path: Path):
"""Test `save-qp` command with start time shifting disabled."""
EXPECTED_QP_CONTENTS = """
50 I -1
90 I -1
"""
assert (
invoke_scenedetect(
"-i {VIDEO} time -s 51 -e 95 {DETECTOR} save-qp --disable-shift",
output_dir=tmp_path,
)
== 0
)
output_path = tmp_path.joinpath(f"{DEFAULT_VIDEO_NAME}.qp")
assert os.path.exists(output_path)
assert output_path.read_text() == EXPECTED_QP_CONTENTS[1:]


@pytest.mark.parametrize("backend_type", ALL_BACKENDS)
def test_cli_backend(backend_type: str):
"""Test setting the `-b`/`--backend` argument."""
Expand Down

0 comments on commit 98a560c

Please sign in to comment.