From 43a02d12a5bffd55d22409ce58f81d6504923c94 Mon Sep 17 00:00:00 2001 From: Breakthrough Date: Sat, 19 Oct 2024 23:33:03 -0400 Subject: [PATCH] [cli] Fix type hints for some context fields --- scenedetect/_cli/context.py | 6 +++--- scenedetect/_cli/controller.py | 25 ++++++++++--------------- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/scenedetect/_cli/context.py b/scenedetect/_cli/context.py index 7843a031..3e3dfda1 100644 --- a/scenedetect/_cli/context.py +++ b/scenedetect/_cli/context.py @@ -90,9 +90,9 @@ def __init__(self): self.video_stream: VideoStream = None self.load_scenes_input: str = None # load-scenes -i/--input self.load_scenes_column_name: str = None # load-scenes -c/--start-col-name - self.start_time: FrameTimecode = None # time -s/--start - self.end_time: FrameTimecode = None # time -e/--end - self.duration: FrameTimecode = None # time -d/--duration + self.start_time: ty.Optional[FrameTimecode] = None # time -s/--start + self.end_time: ty.Optional[FrameTimecode] = None # time -e/--end + self.duration: ty.Optional[FrameTimecode] = None # time -d/--duration self.frame_skip: int = None # Options: diff --git a/scenedetect/_cli/controller.py b/scenedetect/_cli/controller.py index 313997fd..28d52b9d 100644 --- a/scenedetect/_cli/controller.py +++ b/scenedetect/_cli/controller.py @@ -160,19 +160,16 @@ def _load_scenes(context: CliContext) -> ty.Tuple[SceneList, CutList]: csv_headers = next(file_reader) if context.load_scenes_column_name not in csv_headers: csv_headers = next(file_reader) - # Check to make sure column headers are present + # Check to make sure column headers are present and then load the data. if context.load_scenes_column_name not in csv_headers: raise ValueError("specified column header for scene start is not present") - col_idx = csv_headers.index(context.load_scenes_column_name) - cut_list = sorted( FrameTimecode(row[col_idx], fps=context.video_stream.frame_rate) - 1 for row in file_reader ) - # `SceneDetector` works on cuts, so we have to skip the first scene and use the first frame - # of the next scene as the cut point. This can be fixed if we used `SparseSceneDetector` - # but this part of the API is being reworked and hasn't been used by any detectors yet. + # `SceneDetector` works on cuts, so we have to skip the first scene and place the first + # cut point where the next scenes starts. if cut_list: cut_list = cut_list[1:] @@ -182,14 +179,12 @@ def _load_scenes(context: CliContext) -> ty.Tuple[SceneList, CutList]: cut_list = [cut for cut in cut_list if cut > context.start_time] end_time = context.video_stream.duration - if context.end_time is not None or context.duration is not None: - if context.end_time is not None: - end_time = context.end_time - elif context.duration is not None: - end_time = start_time + context.duration - end_time = min(end_time, context.video_stream.duration) + if context.end_time is not None: + end_time = min(context.end_time, context.video_stream.duration) + elif context.duration is not None: + end_time = min(start_time + context.duration, context.video_stream.duration) + cut_list = [cut for cut in cut_list if cut < end_time] + scene_list = get_scenes_from_cuts(cut_list=cut_list, start_pos=start_time, end_pos=end_time) - return get_scenes_from_cuts( - cut_list=cut_list, start_pos=start_time, end_pos=end_time - ), cut_list + return (scene_list, cut_list)