Skip to content

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

Open
sungbin1015 wants to merge 1 commit into
Breakthrough:mainfrom
sungbin1015:fix/opencv-position-after-eof
Open

sungbin1015 wants to merge 1 commit into
Breakthrough:mainfrom
sungbin1015:fix/opencv-position-after-eof

Conversation

@sungbin1015

Copy link
Copy Markdown

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant