Skip to content

Commit fd6a5d3

Browse files
committed
[backends] Fix image sqeuences with OpenCV 5.0
CAP_PROP_POS_MSEC returns -1 for image sequences with OpenCV 5.
1 parent 4fad8b8 commit fd6a5d3

2 files changed

Lines changed: 5 additions & 3 deletions

File tree

scenedetect/backends/opencv.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,10 @@ def timecode(self) -> Timecode:
221221
@property
222222
def position(self) -> FrameTimecode:
223223
timecode = self.timecode
224-
# If PTS is 0 but we've read frames, derive from frame number.
225-
# This handles image sequences and cases where CAP_PROP_POS_MSEC is unreliable.
226-
if timecode.pts == 0 and self.frame_number > 0:
224+
# If PTS is non-positive but we've read frames, derive from frame number. This handles
225+
# image sequences and cases where CAP_PROP_POS_MSEC is unreliable. OpenCV 5 reports
226+
# CAP_PROP_POS_MSEC as -1 (rather than 0) for image sequences on Windows, so check <= 0.
227+
if timecode.pts <= 0 and self.frame_number > 0:
227228
fps = self.frame_rate
228229
time_base = Fraction(1, fps.numerator)
229230
pts = (self.frame_number - 1) * fps.denominator

website/pages/changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,3 +762,4 @@ Development
762762
- [bugfix] The PyAV backend (`VideoStreamAv`) now skips corrupt frames during `read()` and continues decoding instead of failing, giving up only after 8 consecutive decode failures (matching the OpenCV backend's tolerance behavior)
763763
- [bugfix] The PyAV backend now normalizes presentation times by the stream start time, so files with a delayed start (e.g. from edit lists) report the first frame at `position` 0, consistent with other backends and with `seek()`
764764
- [bugfix] Comparisons between two `FrameTimecode` objects that both carry exact presentation times (e.g. positions from VFR videos) and share the same frame rate are now performed exactly using `pts` and `time_base` instead of rounded frame numbers. Previously, distinct frames in VFR sections could compare equal or fail strict ordering when their times rounded to the same approximate frame number. Comparisons involving frame- or seconds-based timecodes, plain values (`int`/`float`/`str`), or differing frame rates are unchanged
765+
- [bugfix] Fix image sequence inputs when using OpenCV 5.0

0 commit comments

Comments
 (0)