Python OpenCV在尝试读取视频时崩溃

Python OpenCV在尝试读取视频时崩溃,python,python-2.7,opencv,Python,Python 2.7,Opencv,当我调用.read()方法时,我的程序崩溃(使用.avi格式)。我的程序以其他格式(.ogv、.mkv)运行,但在这种情况下,type(last_frame)==None。是否有任何解决方案来修复这些错误 import cv2 import numpy as np in_file = r'C:\Users\Johnny\Desktop\zola\animation.avi' print(in_file) cap = cv2.VideoCapture(in_file) print(cap) re

当我调用
.read()
方法时,我的程序崩溃(使用.avi格式)。我的程序以其他格式(.ogv、.mkv)运行,但在这种情况下,
type(last_frame)==None
。是否有任何解决方案来修复这些错误

import cv2
import numpy as np

in_file = r'C:\Users\Johnny\Desktop\zola\animation.avi'
print(in_file)
cap = cv2.VideoCapture(in_file)
print(cap)
ret, last_frame = cap.read() # the program works without this line

try:
    if not cap.isOpened() : raise("damn!")
except TypeError as err:
    print(err)
输出:

C:\Users\Johnny\Desktop\zola\animation.ogv
<VideoCapture 02843110>
exceptions must be old-style classes or derived from BaseException, not str
C:\Users\Johnny\Desktop\zola\animation.ogv
异常必须是旧式类或从BaseException派生,而不是str

对最近的草率行为表示抱歉:请添加一个复选框:if not cap.isOpened():引发异常(“该死!”)(在尝试阅读任何内容之前也要这样做)此外,既然你赢了,请尝试找出avi(这只是一个容器)中使用的确切压缩方法,并安装相应的编解码器,即从这里开始:它起作用了,请键入答案,我可以接受