Python – How to process video files with python OpenCV faster than file frame rate

opencvpythonvideo-processing

I have video file that I am trying to process one frame at a time,. I tried use VideoCapture class to do reading with following type of code. The problem is that if video file is recorded at 25 frames / second, the reading happens at same pace. How to get frames as fast as my computer can decode them?

I plan to process the video stream and then store it to a file.

import cv2
import sys
import time

cap = cv2.VideoCapture(sys.argv[1])
start = time.time()

counter = 0
while True:
    counter += 1;
    image = cap.read()[1]
    if counter %25 == 0:
        print "time", time.time() - start

Output: It prints a timestamp once every 25 frames. Notice how timestamps change almost exactly by 1 second on every line => program processes about 25 frames per second. This with video file that is 25 frames/second.

time 1.25219297409
time 2.25236606598
time 3.25211691856
time 4.25237703323
time 5.25236296654
time 6.25234603882
time 7.252161026
time 8.25258207321
time 9.25195503235
time 10.2523479462

Probably VideoCapture is the wrong API for this kind of work, but what to use instead?

Using Linux, Fedora 20, opencv-python 2.4.7 and python 2.7.5.

Best Solution

I can reproduce the behavior you describe (i.e. cv::VideoCapture >> image locked to the frame rate of the recorded video) if opencv is compiled without ffmpeg support. If I compile opencv with ffmpeg support, I can read images from file as fast as my computer will allow. I think that in the absence of ffmpeg, opencv uses gstreamer and essentially treats the video file like its playing back a movie.

If you are using Linux, this link shows which packages you must install to get ffmpeg support for opencv.