【问题标题】:error: (-215:Assertion failed) error when trying to capture youtube video using pafy and openCv错误:(-215:断言失败)尝试使用 pafy 和 openCv 捕获 youtube 视频时出错
【发布时间】:2019-10-27 21:45:28
【问题描述】:

我正在尝试使用 opencv 和 pafy 访问 youtube 视频。我按照这里给出的说明Is it possible to stream video from https:// (e.g. YouTube) into python with OpenCV? 。但是按照说明操作后,我得到了下面提到的错误- cv2.error: OpenCV(4.0.0) /io/opencv/modules/highgui/src/window.cpp:350: 错误: (-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'

【问题讨论】:

    标签: python opencv youtube pafy


    【解决方案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()
    

    【讨论】:

    • 我尝试了您给出的建议,但最终没有任何结果。我的意思是我的终端没有得到任何结果或任何错误。我的代码的问题是它无法从链接中检索视频。这就是为什么我收到断言错误。
    • 尝试最小化所有窗口以查看结果,它在后面。我也不能先然后我最小化所有,我找到了框架。
    【解决方案2】:

    你也可以试试这个 在这里,我们首先检查是否有框架,然后只调用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
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-04
      • 2021-09-10
      • 1970-01-01
      • 2022-01-14
      • 2021-09-01
      • 2020-03-29
      • 2017-10-24
      • 2020-08-21
      相关资源
      最近更新 更多