December 23, 2024

How to Open a Video from a File and Play it on a Computer Screen in OpenCV and Python

In this Python and OpenCV tutorial, we explain

  1. How to open a video from a video file in OpenCV and Python.
  2. How to obtain video properties such as total number of frames, frame rate, video width, and video height in Python and OpenCV.
  3. How to play the opened video in OpenCV and Python.

The YouTube tutorial accompanying this webpage tutorial is given below.

How to Read a Video From a File in Python and OpenCV

The Python script given below opens a video from a file, checks if the video is properly opened, and obtains the video properties

# import OpenCV
import cv2

# import this to ensure that the video with the proper 
# frame rate is displayed
import time

# create a VideoCapture object
# the input is the name of the video file that we want to open

# open the video and create VideoCapture object that is used to access 
# the video and its properties 
video=cv2.VideoCapture('recordedVideo.avi')

# test video
if (video.isOpened()):
    print("The video is successfully opened")
    
else:
    print("Could not open the video")



# let us learn how to get the video properties 

# total frame count
totalFrameCount=int(video.get(cv2.CAP_PROP_FRAME_COUNT))

# get the frame rate
frameRate=int(video.get(cv2.CAP_PROP_FPS))

# get the frame width and height
frameWidth=int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
frameHeight=int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))

The OpenCV is imported as cv2. We also import the time library. We use this library later on to make sure that the video is displayed with the proper frame rate. We use the function “cv2.VideoCapture” to open a video from the file. This function creates a VideoCapture object that is used to display the video. We call the returned VideoCapture object as “video”. We check if the video is properly opened by using the IF statement and the function “isOpened()”.

The input of this function is the file name of the video that we want to open. Next, we obtain the main properties of the opened video. We use the OpenCV flags “cv2.CAP_PROP_FRAME_COUNT”, “cv2.CAP_PROP_FPS”, “cv2.CAP_PROP_FRAME_WIDTH”, and “cv2.CAP_PROP_FRAME_HEIGHT”, to obtain the total number of frames, frame rate, frame width, and frame height, respectively.

We play the video by using the while loop given below

# play the video 

while True:
    # read the video frame by frame
    success,frame=video.read()
    
    if not success:
        print("Reached the end of the video.")
        break
    
    
    
    # display the video frame by frame
    cv2.imshow('Camera video',frame)
    
    # introduce pause, such that the video is played with the 
    # correct frame rate
    time.sleep(1/frameRate)
    # stop the video if the user presses the keyboard key "c"
    if cv2.waitKey(1) == ord('c'):
        break
    
    
# release the VideoCapture object
video.release()
# close all the windows
cv2.destroyAllWindows()    

In the sequel, we provide a detailed explanation of this script. We read the video frame by frame by using the function “video.read()”

 success,frame=video.read()

The first output is the Boolean variable indicating if we were able to read the frame or not. This variable is used to stop the code once we reach the end of the video. The second output is the frame. The frame is a NumPy array, representing the image.

We display the video frame by frame by using the function cv2.imshow()

 # display the video frame by frame
    cv2.imshow('Camera video',frame)

The first input of this function is the title of the video screen. The second input is the name of the frame that we want to display. Next, we introduce pause in the code in order to ensure that the video is displayed with the proper frame rate:

    time.sleep(1/frameRate)

Finally, we introduce an IF statement that will ensure that the video is stopped after the user presses the keyboard key “c”. Finally, we release the video capture object and close all the windows:

# release the VideoCapture object
video.release()
# close all the windows
cv2.destroyAllWindows()