Get video dimension in python-opencv Get video dimension in python-opencv python python

Get video dimension in python-opencv


It gives width and height of file or camera as float (so you may have to convert to integer)

But it always gives me 0.0 FPS.

import cv2vcap = cv2.VideoCapture('video.avi') # 0=camera if vcap.isOpened():     # get vcap property     width  = vcap.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)   # float `width`    height = vcap.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)  # float `height`    # or    width  = vcap.get(3)  # float `width`    height = vcap.get(4)  # float `height`    # it gives me 0.0 :/    fps = vcap.get(cv2.cv.CV_CAP_PROP_FPS)

It seems it can works fps = vcap.get(7) but I checked this only on one file.


EDIT 2019: Currently cv2 uses little different names

cv2.CAP_PROP_FRAME_WIDTH   # 3cv2.CAP_PROP_FRAME_HEIGHT  # 4cv2.CAP_PROP_FPS           # 5cv2.CAP_PROP_FRAME_COUNT   # 7

but they have the same values: 3, 4, 5, 7

import cv2#vcap = cv2.VideoCapture(0)  # built-in webcameravcap = cv2.VideoCapture('video.avi')if vcap.isOpened():     width  = vcap.get(cv2.CAP_PROP_FRAME_WIDTH)   # float `width`    height = vcap.get(cv2.CAP_PROP_FRAME_HEIGHT)  # float `height`    # or    width  = vcap.get(3)  # float `width`    height = vcap.get(4)  # float `height`    print('width, height:', width, height)        fps = vcap.get(cv2.CAP_PROP_FPS)    # or    fps = vcap.get(5)        print('fps:', fps)  # float `fps`        frame_count = vcap.get(cv2.CAP_PROP_FRAME_COUNT)    # or    frame_count = vcap.get(7)        print('frames count:', frame_count)  # float `frame_count`    #print('cv2.CAP_PROP_FRAME_WIDTH :', cv2.CAP_PROP_FRAME_WIDTH)   # 3    #print('cv2.CAP_PROP_FRAME_HEIGHT:', cv2.CAP_PROP_FRAME_HEIGHT)  # 4    #print('cv2.CAP_PROP_FPS         :', cv2.CAP_PROP_FPS)           # 5    #print('cv2.CAP_PROP_FRAME_COUNT :', cv2.CAP_PROP_FRAME_COUNT)   # 7

EDIT 2020: All properties in How do I get usb webcam property IDs for OpenCV


width = vcap.get(cv2.CAP_PROP_FRAME_WIDTH )height = vcap.get(cv2.CAP_PROP_FRAME_HEIGHT )fps =  vcap.get(cv2.CAP_PROP_FPS)

or

width = vcap.get(3)height = vcap.get(4)fps = vcap.get(5)


For the 3.3.1 version, the methods have changed. Check this link for the changes: https://docs.opencv.org/3.3.1/d4/d15/group__videoio__flags__base.html#ga023786be1ee68a9105bf2e48c700294d

Instead of cv2.cv.CV_CAP_PROP_FRAME_WIDTH use cv2.CAP_PROP_FRAME_WIDTH and others as necessary from the link above.