21

This is my video

enter image description here

This is the script to find fps:

import cv2
if __name__ == '__main__' :

    video = cv2.VideoCapture("test.mp4");

    # Find OpenCV version
    (major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.')

    if int(major_ver)  < 3 :
        fps = video.get(cv2.cv.CV_CAP_PROP_FPS)
        print "Frames per second using video.get(cv2.cv.CV_CAP_PROP_FPS): {0}".format(fps)
    else :
        fps = video.get(cv2.CAP_PROP_FPS)
        print "Frames per second using video.get(cv2.CAP_PROP_FPS) : {0}".format(fps)

    video.release(); 

This is the output of the script for this video: Frames per second using video.get(cv2.CAP_PROP_FPS) : 0.0

Why is it returning 0.0? The FPS is 14.0

5
  • 2
    did you check opencv actually opened the file? (with video.isOpened())
    – api55
    Feb 28, 2018 at 9:26
  • @api55 you are right it didn't, it returns False. Why is that?
    – Tasos
    Feb 28, 2018 at 9:47
  • 1
    well, it didn't open it, you can try with video.open("test.mp4") try, to put the full path just in case... if not try another video (to discard a codec problem)
    – api55
    Feb 28, 2018 at 10:24
  • It was actually because opencv was not installed via pip (I have no idea how it was being importing, it's my first time using it). So after doing pip-install python-opencv, it worked like a charm.
    – Tasos
    Feb 28, 2018 at 10:28
  • That just explains what was the problem in the first place, when you install opencv with pip it brings its own backends (ffmpeg) so I think the problem was that your first opencv backend was broken
    – LichKing
    Jan 9, 2019 at 13:09

2 Answers 2

12

Performing pip install python-opencv fixed the problem and the FPS is correctly detected.

EDIT: tested with python 3.8 and indeed it is pip install opencv-python. Cannot remember two years ago what python I was using.

EDIT November 2022: please also check Perry's answer below, if you are using a newer opencv-python version

3
  • 2
    When I try this on Windows 10, I get three rows of output: Collecting python-opencv, ERROR: Could not find a version that satisfies the requirement python-opencv (from versions: none) and ERROR: No matching distribution found for python-opencv Aug 12, 2019 at 9:53
  • 2
    Actually it should be pip install opencv-python
    – decadenza
    Mar 26, 2020 at 16:11
  • 2
    On a Google Colab Notebook, I had to use pip install --upgrade --force-reinstall opencv-python Jan 3, 2022 at 12:10
6

The recent versions of opencv-python will give an error called AttributeError because cv2 doesn't have any attribute named cv.

Instead use the following

import cv2

vidcap = cv2.VideoCapture('some_video.avi')
fps = vidcap.get(cv2.CAP_PROP_FPS)

print(f"{fps} frames per second")

This will give the frames per second value

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.