我正在尝试使用 opencv 和 pafy 访问 youtube 视频。我按照这里给出的说明是否可以使用 OpenCV 将视频从 https://(例如 YouTube)流式传输到 python 中? . 但是按照说明后我得到了下面提到的错误 - cv2.error: OpenCV(4.0.0) /io/opencv/modules/highgui/src/window.cpp:350: error: (-215:Assertion failed) size.width >0 && size.height>0 在函数“imshow”中
import cv2
import pafy
url = "https://www.youtube.com/watch?v=_p9VEKecHKI"
live = pafy.new(url)
stream = live.getbest(preftype="mp4")
cap = cv2.VideoCapture(stream.url)
#cap = cv2.VideoCapture()
#cap.open(stream.url)
while True:
ret, frame = cap.read()
cv2.imshow('frame', frame)
if cv2.waitKey(1) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
我得到的错误 -
Traceback (most recent call last):
File "videoCapture.py", line 20, in <module>
cv2.imshow('frame', frame)
cv2.error: OpenCV(4.0.0) /io/opencv/modules/highgui/src/window.cpp:350: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'imshow'
当我在这一行给出 preftype="webm" -
stream = live.getbest(preftype="webm")
我得到以下错误 -
Traceback (most recent call last):
File "videoCapture.py", line 11, in <module>
cap = cv2.VideoCapture(stream.url)
AttributeError: 'NoneType' object has no attribute 'url'