Skip to content

Keep OpenCV position on the last frame at the end of VFR video - #586

Merged
Breakthrough merged 1 commit into
Breakthrough:mainfrom
sungbin1015:fix/opencv-position-after-eof
Oct 20, 2026
Merged

Breakthrough merged 1 commit into
Breakthrough:mainfrom
sungbin1015:fix/opencv-position-after-eof

Conversation

@sungbin1015

Copy link
Copy Markdown
Contributor

Refs #561

With the OpenCV backend, position moves backwards once read() hits the end of a VFR video: after the last failed grab(), CAP_PROP_POS_MSEC reports an earlier time than the last frame that was actually read. SceneManager uses video.position after the loop as the end of the last scene, so that scene ends early, and if the last cut falls after that point the end comes out before the start, which is the invalid end time in #561. PyAV doesn't do this, and CFR files are fine with both backends.

It shows up with goldeneye-vfr-drop3.mp4 from the resources branch:

video = open_video("tests/resources/goldeneye-vfr-drop3.mp4", backend="opencv")
while video.read() is not False:
    last = video.position
print(last, video.position)  # main: 00:00:09.927 00:00:09.906

VideoStreamCv2.read() now remembers the timecode of the last frame it read and keeps reporting it after a failed read, until the next successful read, seek() or reset(). Nothing changes while frames are being read.

I couldn't test with the DVD from #561, so I'm not marking it as fixed. On a synthetic MPEG-2 MKV with a timestamp gap, the last scene now ends at the same time as with PyAV instead of about 0.65 s early.

Added test_vfr_position_after_eof_opencv, which fails on main and passes with this change. pytest, ruff check, ruff format --check and pyright pass locally; the two moviepy CLI tests that fail here fail the same way on main.

After the final failed grab(), CAP_PROP_POS_MSEC can report an earlier time
than the last frame read, so the last scene ended early on VFR input (Breakthrough#561).
Remember the last frame's timecode and report it until the next successful
read, seek or reset.
@Breakthrough
Breakthrough self-requested a review October 20, 2026 01:34
@Breakthrough Breakthrough added this to the 0.7.3 milestone Oct 20, 2026

@Breakthrough Breakthrough left a comment

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.

Thanks for the fix!

@Breakthrough
Breakthrough merged commit 1b6c928 into Breakthrough:main Oct 20, 2026
27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants