To capture video in Python using cv2, you can “use the cv2.VideoCapture() function.” This function creates an object of the VideoCapture class and accepts either a device index or the name of a video file. You can select a camera by passing 0 or 1 as an argument and then capture the video frame-by-frame.
Steps to capture a video
- Use the “cv2.VideoCapture()” method to get a video capture object for the camera.
- Set up an infinite while loop and use the “read()” method to read the frames using the above-created object.
- Use the “cv2.imshow()” method to show the frames in the Video.
- Breaks the “loop” when the user clicks a specific key.
Example
import cv2
cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read()
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Go to the terminal and run the Python file using the following command.
python3 app.py
It will ask for camera permission, and when you permit it, the webcam will be opened, and in the Python window, you will see yourself.
Output
You can pass one of the following values based on your requirements.
cv2.VideoCapture(0): Means first camera or webcam.
cv2.VideoCapture(1): Means second camera or webcam.
cv2.VideoCapture("file name.mp4"): Means video file
After this, we can start reading a Video from the camera frame by frame. We do this by calling the read method on the VideoCapture object.
That’s it.

Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.
T1000s Effects – LedEdit Effects Download Free Led Edit 2014 Download
Python cv2.VideoCatpture() is a python-opencv module’s function that captures video from a camera or a file. It takes a single argument, which is the index of the camera to be used or the name of the video file to be opened.
spelling error in “VideoCatpture”