Skip to content
Open
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
12 changes: 11 additions & 1 deletion scenedetect/scene_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,7 @@ def detect_scenes(
)
decode_thread.start()
frame_im = None
prev_position = None

logger.info("Detecting scenes...")
try:
Expand All @@ -581,13 +582,22 @@ def detect_scenes(
if next_frame is not None:
frame_im = next_frame
assert frame_im is not None
if prev_position is None:
prev_position = self._start_pos
assert prev_position is not None
new_cuts = self._process_frame(position, frame_im, callback)
if progress_bar is not None:
if new_cuts:
progress_bar.set_description(
PROGRESS_BAR_DESCRIPTION % len(self._cutting_list), refresh=False
)
progress_bar.update(1 + frame_skip)
# Increment progress bar by delta of position.frame_num instead of 1
# to handle VFR video where frame count is an approximation.
# First frame increments by 1 as base case.
delta_pos = position.frame_num - prev_position.frame_num
is_first_frame = position.frame_num == 0

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

position.frame_num == 0 won't hold when detection starts from a seek (e.g. start_time is set) leading to an off by 1 error in some cases. Both this and the frame_skip issue below can be fixed if we keep prev_position = None and computing:

delta = 1 if prev_position is None else position.frame_num - prev_position.frame_num
progress_bar.update(delta)
prev_position = position

The first frame counts as 1, and each later frame delta covers all of the skipped frames too (no special cases needed). This also avoids reading self._start_pos which is set on the decode thread.

progress_bar.update((1 if is_first_frame else delta_pos) + frame_skip)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When frame_skip > 0, the skipped frames are already reflected in position.frame_num. See the prev. comment for an alternative.

The decode thread queues a frame, then skips, so consecutive queued positions differ by frame_skip + 1). Adding + frame_skip on top double-counts them, e.g. with frame_skip=1 the bar advances 3 per processed frame instead of 2.

prev_position = position
finally:
if progress_bar is not None:
progress_bar.set_description(
Expand Down
Loading