The problem is in this line:

gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

This line expects frame to be a 3 channel or 4 channel Mat object but instead it got some empty Mat and that is why you are getting this assertion failed. You need to check if the frame exists in video and need to handle end of video properly.

cap.isOpened() will just check if the video file can be opened for reading but it will not return a false when end of video file is reached.

Try this

Answer from abggcv on Stack Overflow
๐ŸŒ
LearnOpenCV
learnopencv.com โ€บ home โ€บ read, write and display a video using opencv
Read, Write and Display a video using OpenCV | LearnOpenCV
July 31, 2026 - In OpenCV, a video can be read either by using the feed from a camera connected to a computer or by reading a video file. The first step towards reading a video file is to create a VideoCapture object.
๐ŸŒ
OpenCV
opencv.org โ€บ home โ€บ news โ€บ reading and writing videos using opencv
Reading and Writing Videos using OpenCV
March 28, 2025 - Reading a video file requires processing each frame individually, and displaying or storing it. OpenCV provides the VideoCapture() function.
๐ŸŒ
LearnOpenCV
learnopencv.com โ€บ home โ€บ image processing โ€บ reading and writing videos using opencv
Reading and Writing Videos using OpenCV | LearnOpenCV
October 15, 2025 - So, all you need to do is loop over all the frames in a video sequence, and then process one ยท Getting Started with OpenCV, Image Processing, OpenCV Beginners, OpenCV Tutorials ยท Reading and writing videos in OpenCV is very similar to reading ...
๐ŸŒ
MachineLearningMastery
machinelearningmastery.com โ€บ home โ€บ blog โ€บ how to read and display videos using opencv
How to Read and Display Videos Using OpenCV - MachineLearningMastery.com
January 30, 2024 - Digital videos are close relatives of digital images because they are made up of many digital images sequentially displayed in rapid succession to create the effect of moving visual data. The OpenCV library provides several methods to work with videos, such as reading video data from different sources and accessing several of their properties.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-play-a-video-using-opencv
Python | Play a video using OpenCV - GeeksforGeeks
December 4, 2025 - OpenCV (Open Source Computer Vision) ... video files inside a Python window. To do this, we use the VideoCapture class, which allows OpenCV to open and read videos frame by frame....
๐ŸŒ
Kaggle
kaggle.com โ€บ code โ€บ zeeshanlatif โ€บ reading-and-writing-videos-with-opencv
Reading and Writing Videos with OpenCV
March 16, 2024 - Introduction1. Reading a Video File2. Processing Video Frames3. Writing the Video4.
๐ŸŒ
OpenCV-Python Tutorials
opencv24-python-tutorials.readthedocs.io โ€บ en โ€บ latest โ€บ py_tutorials โ€บ py_gui โ€บ py_video_display โ€บ py_video_display.html
Getting Started with Videos โ€” OpenCV-Python Tutorials beta documentation
import numpy as np import cv2 cap = cv2.VideoCapture(0) while(True): # Capture frame-by-frame ret, frame = cap.read() # Our operations on the frame come here gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Display the resulting frame cv2.imshow('frame',gray) if cv2.waitKey(1) & 0xFF == ord('q'): break # When everything done, release the capture cap.release() cv2.destroyAllWindows()
Find elsewhere
๐ŸŒ
Python Geeks
pythongeeks.org โ€บ python geeks โ€บ learn opencv โ€บ reading and writing videos using opencv
Reading and Writing Videos using OpenCV - Python Geeks
February 9, 2023 - To read a video, we need to loop over all the frames and then process them one by one. The cv2.VideoCapture function in OpenCV is used to perform video file reading operations.
๐ŸŒ
Master Data Science
datahacker.rs โ€บ read-write-and-display-videos-opencv
OpenCV #002 Read, Write and Display Video - Master Data Science
March 14, 2020 - Using OpenCV it can be done in two ways, either by reading a video file or using the video stream directly from a camera connected to your computer. The first approach towards reading a video file is creating a VideoCapture Object and we need to specify the video filename.
๐ŸŒ
Roboflow
roboflow.com โ€บ home โ€บ read video streams with cv2.videocapture
Read Video Streams with cv2.VideoCapture (OpenCV) | Roboflow
import cv2 as cv cap = cv.VideoCapture(0) while True: ret, frame = cap.read() if not ret: break cv.imshow('frame', frame) if cv.waitKey(1) == ord('q'): break cap.release() cv.destroyAllWindows() OpenCV can be used with the open source supervision Python package.
๐ŸŒ
OpenCV
docs.opencv.org โ€บ 3.4.20 โ€บ d8 โ€บ dfe โ€บ classcv_1_1VideoCapture.html
OpenCV: cv::VideoCapture Class Reference
The method/function combines VideoCapture::grab() and VideoCapture::retrieve() in one call. This is the most convenient method for reading video files or capturing data from decode and returns the just grabbed frame.
๐ŸŒ
Idiot Developer
idiotdeveloper.com โ€บ home โ€บ read video files using opencv python
Read Video Files Using OpenCV Python - Idiot Developer
August 25, 2024 - Unsupported File Format: OpenCV supports many video formats, but if your file format isnโ€™t supported, you may need to convert it to a compatible format like .mp4 or .avi. Slow Playback: The delay in cv2.waitKey() may need adjustment depending on the frame rate of your video. If the video plays too slowly, reduce the delay time. Reading and processing video files can be applied to many real-world scenarios, including:
๐ŸŒ
OpenCV
docs.opencv.org โ€บ 4.13.0 โ€บ dd โ€บ d43 โ€บ tutorial_py_video_display.html
OpenCV: Getting Started with Videos
Learn to read video, display video, and save video. Learn to capture video from a camera and display it. You will learn these functions : cv.VideoCapture(), cv.VideoWriter() Often, we have to capture live stream with a camera. OpenCV provides a very simple interface to do this.
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python โ€บ opencv
Capture video from camera/file with OpenCV in Python | note.nkmk.me
October 19, 2022 - Reading and saving image files with Python, OpenCV (imread, imwrite) Video I/O must be enabled to handle video in OpenCV.
๐ŸŒ
Medium
korlakuntasaikamal10.medium.com โ€บ reading-videos-in-opencv-using-cv2-videowriter-module-in-python-84d8706c12db
Reading Videos in OpenCV using cv2.VideoWriter Module in Python | by kamal_DS | Medium
February 8, 2023 - Reading Videos in OpenCV using cv2.VideoWriter module in Python: The cv2.VideoWriter module in OpenCV provides a simple interface for reading videos. To read a video in OpenCV, we need to create a cv2.VideoCapture object and open a video file ...
๐ŸŒ
YouTube
youtube.com โ€บ watch
Reading and Writing Videos using OpenCV - YouTube
๐Ÿ“š Blog post Link: https://learnopencv.com/reading-and-writing-videos-using-opencv/๐Ÿ“š Check out our FREE Courses at OpenCV University : https://opencv.org/un...
Published: February 12, 2024
๐ŸŒ
D-Lab
dlab.berkeley.edu โ€บ news โ€บ processing-videos-python-opencv
Processing Videos in Python with OpenCV | D-Lab
November 28, 2023 - Videos have different properties, such as resolutions, frame rates, and duration, which impact how we analyze them in OpenCV. A VideoCapture object contains information about the video properties, which can be accessed through specific variables in cv2.
๐ŸŒ
YouTube
youtube.com โ€บ watch
OpenCV Python Tutorial For Beginners 4 - How to Read, Write, Show Videos from Camera in OpenCV - YouTube
In this video on OpenCV Python Tutorial For Beginners, I am going to show How to Read, Write, Show Videos from Camera in OpenCV.We will see How to read video...
Published: February 17, 2019