Fix slow frame grabbing when max_fps is set on FileStreamFrameGrabber#95
Merged
Conversation
sunildkumar
reviewed
Mar 17, 2026
Comment on lines
+1559
to
+1563
| """Drop frames to achieve target frame rate by reading and discarding. | ||
|
|
||
| Sequential reads are used instead of CAP_PROP_POS_FRAMES seeking | ||
| because seeking forces the decoder to find the nearest keyframe and | ||
| decode forward, which is extremely expensive on H.264/H.265 video. |
Member
There was a problem hiding this comment.
nit: some of this docstring reads as being very focused on this specific bug fix. (because claude thinks it is the center of the universe...) I would move this implementation detail to a comment instead.
Collaborator
Author
There was a problem hiding this comment.
I cleaned up the docstring. It was actually written by ChatGPT
sunildkumar
approved these changes
Mar 17, 2026
added 2 commits
March 17, 2026 14:11
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
_drop_frames()inFileStreamFrameGrabberusedcv2.CAP_PROP_POS_FRAMESto seek past unwanted frames. On H.264/H.265 video, every seek forces the decoder to locate the nearest keyframe and decode forward to the target frame, producing a sawtooth timing pattern where grab times ramp up between keyframes. This made grabbing fewer frames (e.g. 5fps from a 30fps source) paradoxically much slower than reading every frame sequentially.The fix replaces the seek with sequential
capture.read()calls that discard unwanted frames. Sequential reads use the decoder's internal buffer and cost ~1ms each, while seeks were averaging ~40-120ms depending on distance from the nearest keyframe.The bug
_drop_frames()called:The fix
Replaced with:
How this bug was introduced
Git archeology shows this bug did not exist in the original implementation. The first commit on the feature branch (
27a5f34, Dec 13, 2024) correctly used sequentialcapture.read()calls to discard frames. Four days later, a cleanup commit (f109a87, Dec 17, 2024) on the same branch replaced the sequential reads withCAP_PROP_POS_FRAMESseeking -- likely as a perceived optimization. The branch was then squash-merged to main asa4f1fd8("Add FileStreamFrameGrabber (#62)"), baking the regression into the mainline. The_drop_framesmethod has not been modified since.Performance results (25fps H.264 source, max_fps=5, 200 frames)