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

14,192

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.

Share:
14,192

Related videos on Youtube

Juha Syrjälä
Author by

Juha Syrjälä

GitHublinkedin

Updated on October 02, 2022

Comments

  • Juha Syrjälä
    Juha Syrjälä over 1 year

    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.

  • Andrzej Pronobis
    Andrzej Pronobis almost 9 years
    I can confirm that I can indeed read from VideoCapture faster than the framerate, but I'm on OpenCV 3.0 compiled with ffmpeg.
  • Plankalkül
    Plankalkül almost 8 years
    That seems to be the solution. Thanks