Height, Width & FPS of the Video in OpenCV Python

Height, width and FPS of the Video using OpenCV Python

In this blog, we will learn how to get the Height, Width, and FPS of the Video in OpenCV Python using the get function.

Learn to Capture Video in OpenCV Python.

OpenCV ships with a get() function that takes certain OpenCV constants or numbers and returns the output.

Height of the Video in OpenCV

Let’s get the height of the Video Frame:

height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)

OR

height = cap.get(4)

Width of the Video Frame

width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)

OR

width  = cap.get(3)

FPS of the Video in OpenCV

fps = cap.get(cv2.CAP_PROP_FPS)

OR

cap.get(5)

Get Frame Count of the Video

frame_count = cap.get(cv2.CAP_PROP_FRAME_COUNT)

OR

cap.get(7)

In this way we can get all the meta data of the video source or frame.

Combining all the properties.

import cv2

cap = cv2.VideoCapture(0)

if cap.isOpened():
    width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
    height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
    fps = cap.get(cv2.CAP_PROP_FPS)
    frame_count = cap.get(cv2.CAP_PROP_FRAME_COUNT)
    print(f"Dimension of the Video Frame: width x height = {width} x {height}")
    print(f"Frame Per Second: {fps}")
    print('Frames count:', frame_count)

cap.release()
Dimension of the Video Frame: width x height = 640.0 x 480.0
Frame Per Second: 30.0
Frames count: -1.0

Learn more about get function in opencv from official opencv documentation.

Tags: ,

Leave a Reply

Your email address will not be published. Required fields are marked *