Python OpenCV video.get(cv2.CAP_PROP_FPS) returns 0.0 FPS
Question:
This is my video
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
Answers:
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 Python version
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
This is my video
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
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 Python version
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