After reading the documentation of VideoCapture. I figured out that you can tell VideoCapture, which frame to process next time we call VideoCapture.read() (or VideoCapture.grab()).

The problem is that when you want to read() a frame which is not ready, the VideoCapture object stuck on that frame and never proceed. So you have to force it to start again from the previous frame.

Here is the code

import cv2

cap = cv2.VideoCapture("./out.mp4")
while not cap.isOpened():
    cap = cv2.VideoCapture("./out.mp4")
    cv2.waitKey(1000)
    print "Wait for the header"

pos_frame = cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES)
while True:
    flag, frame = cap.read()
    if flag:
        # The frame is ready and already captured
        cv2.imshow('video', frame)
        pos_frame = cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES)
        print str(pos_frame)+" frames"
    else:
        # The next frame is not ready, so we try to read it again
        cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, pos_frame-1)
        print "frame is not ready"
        # It is better to wait for a while for the next frame to be ready
        cv2.waitKey(1000)

    if cv2.waitKey(10) == 27:
        break
    if cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES) == cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT):
        # If the number of captured frames is equal to the total number of frames,
        # we stop
        break
Answer from Mehran on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-program-extract-frames-using-opencv
Program to extract frames using OpenCV-Python - GeeksforGeeks
August 11, 2025 - With OpenCV in Python, we can capture each frame sequentially from a video file or camera stream and save them as separate image files. This technique is widely used in applications such as motion analysis, object tracking and dataset creation ...
🌐
Python Guides
pythonguides.com › read-video-frames-in-python
Read Video Frames in Python
August 8, 2025 - OpenCV (Open Source Computer Vision Library) is the go-to Python library for video and image processing. It provides simple APIs to read, manipulate, and write videos. ... import cv2 # Path to your video file video_path = 'traffic_nyc.mp4' # ...
Discussions

How to process images of a video, frame by frame, in video streaming using OpenCV and Python - Stack Overflow
I just want to read the available frames and write them in a directory. Then, wait for the other part of the video to be uploaded and write the frames to the directory. And , I should wait for each frame to be completely uploaded then write it to a file. Can you tell me how can I do it with OpenCV (Python... More on stackoverflow.com
🌐 stackoverflow.com
How is a video file is read frame by frame without loading the entire video file in memory?
There are 3 types of frames: I, P and B. The I-frames contain the entire image, so you can just start at the latest I-frame instead of at the beginning. More on reddit.com
🌐 r/learnpython
2
0
August 16, 2023
How to read specific frames from a video using OpenCV's Cuda Video Reader in Python? - Python - OpenCV
I’ve compiled OpenCV 4.8.0 with Cuda 10.2, cudNN 7.6.5.32, Video_Codec_SDK_12.1.14, and am running it on Python 3.9.13. I’m trying to read specific frames from a video file (video.mkv) using OpenCV’s Cuda Video Reader, similar to how I would with the CPU-based VideoCapture. More on forum.opencv.org
🌐 forum.opencv.org
0
September 29, 2023
How to get FPS of a video file?

You should be able to get it with OpenCV

More on reddit.com
🌐 r/Python
5
1
July 5, 2019
🌐
LearnOpenCV
learnopencv.com › home › read, write and display a video using opencv
Read, Write and Display a video using OpenCV | LearnOpenCV
July 31, 2026 - The other important attributes are the width and height of the frame. In OpenCV, a video can be read either by using the feed from a camera connected to a computer or by reading a video file.
🌐
OpenCV
opencv.org › home › news › reading and writing videos using opencv
Reading and Writing Videos using OpenCV
March 28, 2025 - Practical Implementations: We provided Python and C++ code examples for reading videos from files, webcams, and image sequences, along with displaying videos frame by frame.
Top answer
1 of 7
68

After reading the documentation of VideoCapture. I figured out that you can tell VideoCapture, which frame to process next time we call VideoCapture.read() (or VideoCapture.grab()).

The problem is that when you want to read() a frame which is not ready, the VideoCapture object stuck on that frame and never proceed. So you have to force it to start again from the previous frame.

Here is the code

import cv2

cap = cv2.VideoCapture("./out.mp4")
while not cap.isOpened():
    cap = cv2.VideoCapture("./out.mp4")
    cv2.waitKey(1000)
    print "Wait for the header"

pos_frame = cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES)
while True:
    flag, frame = cap.read()
    if flag:
        # The frame is ready and already captured
        cv2.imshow('video', frame)
        pos_frame = cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES)
        print str(pos_frame)+" frames"
    else:
        # The next frame is not ready, so we try to read it again
        cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, pos_frame-1)
        print "frame is not ready"
        # It is better to wait for a while for the next frame to be ready
        cv2.waitKey(1000)

    if cv2.waitKey(10) == 27:
        break
    if cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES) == cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT):
        # If the number of captured frames is equal to the total number of frames,
        # we stop
        break
2 of 7
31

Use this:

import cv2
cap = cv2.VideoCapture('path to video file')
count = 0
while cap.isOpened():
    ret,frame = cap.read()
    cv2.imshow('window-name', frame)
    cv2.imwrite("frame%d.jpg" % count, frame)
    count = count + 1
    if cv2.waitKey(10) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows() # destroy all opened windows
🌐
Reddit
reddit.com › r/learnpython › how is a video file is read frame by frame without loading the entire video file in memory?
r/learnpython on Reddit: How is a video file is read frame by frame without loading the entire video file in memory?
August 16, 2023 -

As I understand that a video file is a binary file, the frames are not really present in a contiguous manner in a video file, so reading the video file by doing a seek and reading it frame by frame wouldn't work until you have loaded the entire file into memory and then go through it frame by frame.

But if you see any video file reading library eg opencv code, you can read each frame one by one by calling read multiple times in a loop.

video=cv2.VideoCapture(video_path)

grabbed, frame=video.read()

My question is how do they(such libraries) work internally to read the frames one by one without loading the entire video file in memory since frames are not really present in a video file in a contiguous manner so the seek and read x+size data wouldn't work.

🌐
OpenCV
docs.opencv.org › 4.13.0 › dd › d43 › tutorial_py_video_display.html
OpenCV: Getting Started with Videos
So we capture a video and process it frame-by-frame, and we want to save that video. For images, it is very simple: just use cv.imwrite().
Find elsewhere
🌐
Python Geeks
pythongeeks.org › python geeks › learn opencv › reading and writing videos using opencv
Reading and Writing Videos using OpenCV - Python Geeks
February 9, 2023 - To display a video file, we have read using one of three methods mentioned above, we can use the cv2.imshow function. The cv2.imshow() function in OpenCV displays the video frame by frame.
🌐
Tam Vu
vuamitom.github.io › 2019 › 12 › 13 › fast-iterate-through-video-frames.html
Fast way to iterate through video frames with Python and OpenCV | Tam Vu
December 13, 2019 - This simple modification would result in a much needed performance boost. But can we do better? Looking through OpenCV documentation, it’s possible to read from a specific frame by call to cap.set(cv2.CAP_PROP_POS_FRAMES, fno).
🌐
TheAILearner
theailearner.com › 2018 › 10 › 15 › extracting-and-saving-video-frames-using-opencv-python
Extracting and Saving Video Frames using OpenCV-Python | TheAILearner
April 27, 2019 - Read frame by frame · Save each ... · Python · If you don’t have any video, no need to worry. Open the camera instead of the file using cv2.VideoCapture(0) and start extracting frames....
🌐
OpenCV
forum.opencv.org › python
How to read specific frames from a video using OpenCV's Cuda Video Reader in Python? - Python - OpenCV
September 29, 2023 - I’ve compiled OpenCV 4.8.0 with Cuda 10.2, cudNN 7.6.5.32, Video_Codec_SDK_12.1.14, and am running it on Python 3.9.13. I’m trying to read specific frames from a video file (video.mkv) using OpenCV’s Cuda Video Reader, s…
🌐
FutureLearn
futurelearn.com › home › blog
Extracting video frames using OpenCV
October 25, 2022 - Another way to extract the frame, without finding the frame number, is by using the time in milliseconds directly using CAP_PROP_POS_MSEC instead as follows: t_msec = 1000*(minutes*60 + seconds) video.set(cv2.CAP_PROP_POS_MSEC, t_msec) ret, frame = video.read() This should give exactly the same frame as before. Now we have our frame, we can display and/or save it as before in OpenCV:
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-play-a-video-using-opencv
Python | Play a video using OpenCV - GeeksforGeeks
December 4, 2025 - OpenCV (Open Source Computer Vision) ... video files inside a Python window. To do this, we use the VideoCapture class, which allows OpenCV to open and read videos frame by frame....
🌐
YouTube
youtube.com › watch
Tutorial - How to Process Video Frames Using OpenCV - YouTube
Access COMPLETE COURSES on Python HERE: https://academy.zenva.com/product/python-programming-mini-degree/?zva_src=youtube-description-securitycamopencvThis c...
Published: May 16, 2017
🌐
Binarystudy
binarystudy.com › 2021 › 10 › extracting-and-saving-video-frames-opencv-python.html
Extracting and Saving Video Frames using OpenCV Python
October 2, 2022 - The next step is to extract the video frames, if our video is captured successfully. Using cap.read() we read the frames one by one. It returns two values, first value is True or False and second value is the frame.
🌐
Master Data Science
datahacker.rs › read-write-and-display-videos-opencv
OpenCV #002 Read, Write and Display Video - Master Data Science
March 14, 2020 - When working with video files, reading a video file or capturing live stream is not enough. We also want to process the video and display it. To display a video in an open window, OpenCV provides a very simple interface to do this, using the command cv2.imshow in Python or cv::imshow in C++.
🌐
GeeksforGeeks
geeksforgeeks.org › python-program-extract-frames-using-opencv
Python | Program to extract frames using OpenCV - GeeksforGeeks
January 4, 2023 - We can select a color by slide bar which is created by the cv2 command cv2.createTrackbar. Libraries needed:OpenCV NumpyApproach: 3 min read Opening multiple color windows to capture using OpenCV in Python · OpenCV is an open source computer vision library that works with many programming languages and provides a vast scope to understand the subject of computer vision.In this example we will use OpenCV to open the camera of the system and capture the video in two different colors.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-process-images-of-a-video-using-opencv
Python - Process images of a video using OpenCV - GeeksforGeeks
June 4, 2024 - # importing the necessary libraries import cv2 import numpy as np # Creating a VideoCapture object to read the video cap = cv2.VideoCapture('sample.mp4') # Loop until the end of the video while (cap.isOpened()): # Capture frame-by-frame ret, frame = cap.read() frame = cv2.resize(frame, (540, 380), fx = 0, fy = 0, interpolation = cv2.INTER_CUBIC) # Display the resulting frame cv2.imshow('Frame', frame) # using cv2.Gaussianblur() method to blur the video # (5, 5) is the kernel size for blurring.
🌐
MachineLearningMastery
machinelearningmastery.com › home › blog › how to read and display videos using opencv
How to Read and Display Videos Using OpenCV - MachineLearningMastery.com
January 30, 2024 - Digital videos are close relatives of digital images because they are made up of many digital images sequentially displayed in rapid succession to create the effect of moving visual data. The OpenCV library provides several methods to work with videos, such as reading video data from different sources and accessing several of their properties.
🌐
Medium
medium.com › @a4pratapsingh › how-to-read-and-write-videos-in-python-using-opencv-full-guide-code-examples-fdf446d2b1d8
How to Read and Write Videos in Python using OpenCV (Full Guide + Code Examples) | by Ankit Singh | Medium
November 4, 2025 - This function opens a video file ... a number (like 0), it opens your webcam. Once the video is opened, .read () helps you read each frame one by one....