0

我正在尝试使用 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'
4

2 回答 2

0

你也可以在这里试试这个,我们首先检查我们是否有一个框架,然后只有我们调用,cv.imshow('Video', frames)否则它将退出循环。

while True:
    ret, frames = capture.read()

    # for preventing warning (-255 assertion failed)
    if ret:
        cv.imshow('Video', frames)
    else:
        break
    
    # for stopping the window manullaly
    if cv.waitKey(1) & 0xFF == ord('e'):
        break

于 2021-01-11T11:03:53.190 回答
-1
import cv2 
import pafy
import time

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()
        if ret:
            cv2.imshow('frame', frame)

        if cv2.waitKey(1) == ord('q'):
                break

cap.release()
cv2.destroyAllWindows()

在此处输入图像描述

于 2019-06-13T10:41:22.837 回答