Play A Video In Python With OpenCV Module

by | Mar 19, 2022 | Coding, Python

Home » DIY » Play A Video In Python With OpenCV Module

Introduction of the Project

Today, we will learn how to Play a Video in Python with OpenCV Module. OpenCV refers to Open source Computer Vision. It’s a library that helps in performing various tasks related to image processing & computer vision. Here, we demonstrated a code that uses the OpenCV module to pause and play the video. For this action to process, the OpenCV has several elements, and we are trying to capture the frames to proceed with this.

So let’s write code for it!


1. You need Python to run the code. You can use VSCode or any python IDE.

2. OpenCV and Numpy modules must be installed on your system.

3. A demo video for it to process the given commands through code (here playing a video).

Steps To Play A Video In Python With OpenCV Module

Step 1: Install OpenCV if you haven’t it in your system.

Paste the below line of command in your command prompt and press enter.

pip install opencv-python

Step 2: Now copy this source code into your editor/IDE.

Source Code

# Import OpenCV module
import cv2

# Capture the video from its path
capture = cv2.VideoCapture('videos/sampleVideo.mp4')

while True:

    # Read each frame of the video
    ret_val, frame =

    # Resize the output video frame
    video = cv2.resize(frame,(600,700))

    # Dispay the frames of the video
    cv2.imshow('Playing video using OpenCV in python', video)

    # Plays video and waits for a pressed key
    if cv2.waitKey(1) == 27:

# Destroys all of the HighGUI windows

Explanation Of The Code

In the beginning, we imported the OpenCV module.

1. At first, we are capturing the video from its path.

2. After this, we read and resize each frame using the resize() function.

3. Then, we display each video frame using the imshow() function.

4. Finally, we are destroying the video window.


The below picture shows the video played using the OpenCV module in python;

Play A Video In Python With OpenCV Module

Things to Remember 

  • Install the OpenCV module prior to pasting the code.
  • Write the name of the module in lowercase only.
  • Set the path of the video according to your video path.


You May Also Like To Create…