Open In App

Python | Play a video using OpenCV

Last Updated : 03 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

OpenCV (Open Source Computer Vision) is a computer vision library that contains various functions to perform operations on Images or videos. OpenCV library can be used to perform multiple operations on videos. 

Let’s see how to play a video using the OpenCV Python. To capture a video, we need to create a VideoCapture object. VideoCapture have the device index or the name of a video file. Device index is just the number to specify which camera. If we pass 0 then it is for first camera, 1 for second camera so on. We capture the video frame by frame.

 Syntax

  • cv2.VideoCapture(0): Means first camera or webcam.
  • cv2.VideoCapture(1):  Means second camera or webcam.
  • cv2.VideoCapture(“file name.mp4”): Means video file

Step 1: Import the required modules,

Python
import cv2

Step 2: Creating the object of the VideoCapture and read the input file

Python
cap = cv2.VideoCapture('Spend Your Summer Vacations Wisely! Ft. Sandeep Sir _ GeeksforGeeks.mp4')