Get video dimension in python-opencv

PythonOpencvVideo

Python Problem Overview


I can get size of image, like this:

import cv2

img = cv2.imread('my_image.jpg',0)
height, width = img.shape[:2]

How about video?

Python Solutions


Solution 1 - Python

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 cv2

vcap = 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   # 3
cv2.CAP_PROP_FRAME_HEIGHT  # 4

cv2.CAP_PROP_FPS           # 5
cv2.CAP_PROP_FRAME_COUNT   # 7

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

import cv2

#vcap = cv2.VideoCapture(0)  # built-in webcamera

vcap = 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

Solution 2 - Python

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)

Solution 3 - Python

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.

Solution 4 - Python

cv2.__version__
'3.4.3'	

w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = int(cap.get(cv2.CAP_PROP_FPS))
n_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))

Solution 5 - Python

For OpenCV versions = 4.x.x:

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

Solution 6 - Python

Though solved answers are here, in 2021, it got a little changed. (OpenCV version = 4.5.2)

import cv2
cap = cv2.VideoCapture('video.mp4') #0 for camera


if cap.isOpened(): 
    width  = cap.get(cv2.CAP_PROP_FRAME_WIDTH)   # float `width`
    height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)  # float `height`
    fps = cap.get(cv2.CAP_PROP_FPS) # float `fps`
    total_frames = cap.get(cv2.CAP_PROP_FRAME_COUNT) # float `total_frame_in_the_video` (should not be applicable for camera)

Solution 7 - Python

for ret, frame = cv2.VideoCapture use frame.shape to get Width and Height.

height, width, channels = frame.shape
    print(width)

Solution 8 - Python

You can use the vcap.get(i) method, where i can be between 0 and 21, according to the OpenCV docs.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkaravanjoView Question on Stackoverflow
Solution 1 - PythonfurasView Answer on Stackoverflow
Solution 2 - PythonGGEvView Answer on Stackoverflow
Solution 3 - PythonNikhil KasukurthiView Answer on Stackoverflow
Solution 4 - PythonmrgloomView Answer on Stackoverflow
Solution 5 - PythonYScharfView Answer on Stackoverflow
Solution 6 - PythonJoyanta J. MondalView Answer on Stackoverflow
Solution 7 - PythonSteven CornectView Answer on Stackoverflow
Solution 8 - PythonIgnacio HernándezView Answer on Stackoverflow