[英]Unable to read MP4 and avi files in OpenCV Python
I want to just read and display an MP4 video using OpenCV, I wrote the following basic code for it:我只想使用 OpenCV 读取和显示 MP4 视频,我为此编写了以下基本代码:
import cv2
input_video_path = './Input Video/Input_video1.mp4'
cap = cv2.VideoCapture(input_video_path)
while(cap.isOpened()):
ret, frame = cap.read()
print(frame, ret)
cv2.imshow("frame", frame)
cap.release()
cv2.destroyAllWindows()
When I run it, it reads 1st few frames and then all other frames are None
:当我运行它时,它会读取第一几帧,然后所有其他帧都是
None
:
[[[ 7 14 27]
[ 7 14 27]
[ 7 14 27]
...
...
[ 60 57 64]
[ 70 62 64]
[ 72 64 66]]] True
None False
Traceback (most recent call last):
File "D:/Project/ML IP and Coding/Cynapto_Task/exploring_face_detection_methods.py", line 10, in <module>
cv2.imshow("frame", frame)
cv2.error: OpenCV(3.4.2) C:\Miniconda3\conda-bld\opencv-suite_1534379934306\work\modules\highgui\src\window.cpp:356: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'
I also printed the ret
variable which confirms the same behavior.我还打印了确认相同行为的
ret
变量。 I tried with 2-3 different videos and even with .avi
format but got the same result.我尝试了 2-3 个不同的视频,甚至使用
.avi
格式,但得到了相同的结果。
I searched online for a solution but could only find about codecs and installing codecs in my virtual environment.我在网上搜索了解决方案,但只能找到有关编解码器和在我的虚拟环境中安装编解码器的信息。 I don't have much knowledge about video file formats and codecs.
我对视频文件格式和编解码器知之甚少。
Can someone help me with this?有人可以帮我弄这个吗?
I am using:我在用:
Python 3.7, OS: Windows, environment: conda, OpenCV v4.5.0 Python 3.7,操作系统:Windows,环境:康达,OpenCV v4.5.0
If you recieved ret
as False
it means that video reach end frame.如果您收到
ret
为False
,则表示视频到达结束帧。 If video isn't finished but you recieved False
, it probably broken.如果视频未完成但您收到
False
,则它可能已损坏。
Try this code:试试这个代码:
import cv2
input_video_path = './Input Video/Input_video1.mp4'
cap = cv2.VideoCapture(input_video_path)
while(cap.isOpened()):
ret, frame = cap.read()
print(frame, ret)
if ret:
cv2.imshow("frame", frame)
cv2.waitKey(1)
else:
break
cap.release()
cv2.destroyAllWindows()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.