Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[save-images] Add multithreaded version of save-images #456

Merged
merged 4 commits into from
Nov 24, 2024
Merged
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
13 changes: 8 additions & 5 deletions scenedetect.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -220,22 +220,25 @@
# Image quality (jpeg/webp). Default is 95 for jpeg, 100 for webp
#quality = 95

# Compression amount for png images (0 to 9). Does not affect quality.
# Compression amount for png images (0 to 9). Only affects size, not quality.
#compression = 3

# Number of frames to skip at beginning/end of scene.
# Number of frames to ignore around each scene cut when selecting frames.
#frame-margin = 1

# Factor to resize images by (0.5 = half, 1.0 = same, 2.0 = double).
# Resize by scale factor (0.5 = half, 1.0 = same, 2.0 = double).
#scale = 1.0

# Override image height and/or width. Mutually exclusive with scale.
# Resize to specified height, width, or both. Mutually exclusive with scale.
#height = 0
#width = 0

# Method to use for image scaling (nearest, linear, cubic, area, lanczos4).
# Method to use for scaling (nearest, linear, cubic, area, lanczos4).
#scale-method = linear

# Use separate threads for encoding and disk IO. Can improve performance.
#threading = yes


[export-html]
# Filename format of created HTML file. Can use $VIDEO_NAME in the name.
Expand Down
17 changes: 9 additions & 8 deletions scenedetect/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
:class:`SceneManager <scenedetect.scene_manager.SceneManager>`.
"""

import typing as ty
from logging import getLogger
from typing import List, Optional, Tuple, Union

# OpenCV is a required package, but we don't have it as an explicit dependency since we
# need to support both opencv-python and opencv-python-headless. Include some additional
Expand All @@ -30,6 +30,7 @@
) from ex

# Commonly used classes/functions exported under the `scenedetect` namespace for brevity.
# Note that order of importants is important!
from scenedetect.platform import init_logger # noqa: I001
from scenedetect.frame_timecode import FrameTimecode
from scenedetect.video_stream import VideoStream, VideoOpenFailure
Expand All @@ -50,7 +51,7 @@
VideoCaptureAdapter,
)
from scenedetect.stats_manager import StatsManager, StatsFileCorrupt
from scenedetect.scene_manager import SceneManager, save_images
from scenedetect.scene_manager import SceneManager, save_images, SceneList, CutList, Interpolation
from scenedetect.video_manager import VideoManager # [DEPRECATED] DO NOT USE.

# Used for module identification and when printing version & about info
Expand All @@ -63,7 +64,7 @@

def open_video(
path: str,
framerate: Optional[float] = None,
framerate: ty.Optional[float] = None,
backend: str = "opencv",
**kwargs,
) -> VideoStream:
Expand Down Expand Up @@ -117,12 +118,12 @@ def open_video(
def detect(
video_path: str,
detector: SceneDetector,
stats_file_path: Optional[str] = None,
stats_file_path: ty.Optional[str] = None,
show_progress: bool = False,
start_time: Optional[Union[str, float, int]] = None,
end_time: Optional[Union[str, float, int]] = None,
start_time: ty.Optional[ty.Union[str, float, int]] = None,
end_time: ty.Optional[ty.Union[str, float, int]] = None,
start_in_scene: bool = False,
) -> List[Tuple[FrameTimecode, FrameTimecode]]:
) -> SceneList:
"""Perform scene detection on a given video `path` using the specified `detector`.

Arguments:
Expand All @@ -143,7 +144,7 @@ def detect(
will always be included until the first fade-out event is detected.

Returns:
List of scenes (pairs of :class:`FrameTimecode` objects).
List of scenes as pairs of (start, end) :class:`FrameTimecode` objects.

Raises:
:class:`VideoOpenFailure`: `video_path` could not be opened.
Expand Down
3 changes: 2 additions & 1 deletion scenedetect/_cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1493,7 +1493,8 @@ def save_images_command(
"num_images": ctx.config.get_value("save-images", "num-images", num_images),
"output_dir": output,
"scale": scale,
"show_progress": ctx.quiet_mode,
"show_progress": not ctx.quiet_mode,
"threading": ctx.config.get_value("save-images", "threading"),
"width": width,
}
ctx.add_command(cli_commands.save_images, save_images_args)
Expand Down
6 changes: 3 additions & 3 deletions scenedetect/_cli/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@
write_scene_list,
write_scene_list_html,
)
from scenedetect.scene_manager import (
save_images as save_images_impl,
)
from scenedetect.scene_manager import save_images as save_images_impl
from scenedetect.video_splitter import split_video_ffmpeg, split_video_mkvmerge

logger = logging.getLogger("pyscenedetect")
Expand Down Expand Up @@ -179,6 +177,7 @@ def save_images(
height: int,
width: int,
interpolation: Interpolation,
threading: bool,
):
"""Handles the `save-images` command."""
del cuts # save-images only uses scenes.
Expand All @@ -197,6 +196,7 @@ def save_images(
height=height,
width=width,
interpolation=interpolation,
threading=threading,
)
# Save the result for use by `export-html` if required.
context.save_images_result = (images, output_dir)
Expand Down
1 change: 1 addition & 0 deletions scenedetect/_cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ def format(self, timecode: FrameTimecode) -> str:
"quality": RangeValue(_PLACEHOLDER, min_val=0, max_val=100),
"scale": 1.0,
"scale-method": Interpolation.LINEAR,
"threading": True,
"width": 0,
},
"save-qp": {
Expand Down
Loading
Loading