流链接的OpenCV错误:断言失败(size.width> 0 && size.height> 0)

时间:2018-10-19 16:25:10

标签: python opencv

我正在尝试让Streamlink与OpenCV一起使用,不幸的是,似乎Streamlink无法将输入放入框架中。我从OpenCV收到断言失败错误。当我打印cap.read()时说“无”,这很奇怪,因为如果我在终端中执行以下命令,它可以正常工作,但OpenCV不会检测到任何输入。

streamlink --player=mpv https://www.youtube.com/watch?v=IFMzbFyv0sY 360p

错误输出:

info:main:Loading stream https://manifest.googlevideo.com/api/manifest/hls_playlist/id/IFMzbFyv0sY.0/itag/96/source/yt_live_broadcast/requiressl/yes/ratebypass/yes/live/1/cmbypass/yes/goi/160/sgoap/gir%3Dyes%3Bitag%3D140/sgovp/gir%3Dyes%3Bitag%3D137/hls_chunk_host/r1---sn-25ge7nse.googlevideo.com/playlist_type/DVR/gcr/us/ei/99_JW87WDJSQ1wbihZ2oDQ/mm/32/mn/sn-25ge7nse/ms/lv/mv/u/pl/23/dover/11/keepalive/yes/mt/1539956537/ip/35.241.130.76/ipbits/0/expire/1539978327/sparams/ip,ipbits,expire,id,itag,source,requiressl,ratebypass,live,cmbypass,goi,sgoap,sgovp,hls_chunk_host,playlist_type,gcr,ei,mm,mn,ms,mv,pl/signature/270644DCB50A64616DF68375A3BDE0FF795D1192.4F99BE92EF339860BDAFD1EED8D7F2036D07DE10/key/dg_yt0/playlist/index.m3u8
waiting 10 secondes
Traceback (most recent call last):
File "youtube_test.py", line 59, in 
main(opts.url, opts.quality, opts.fps)
File "youtube_test.py", line 34, in main
cv2.imshow('frame', frame)
cv2.error: OpenCV(3.4.3) /io/opencv/modules/highgui/src/window.cpp:356: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'imshow'

代码:

# Detection on Youtube Videos and Livestreams
import cv2
import logging
import streamlink
import time

log = logging.getLogger(__name__)
GREEN = (0, 255, 0)


def stream_to_url(url, quality='720p'):
    streams = streamlink.streams(url)
    if streams:
        return streams[quality].to_url()
    else:
        raise ValueError("No streams were available")


def main(url, quality='720p', fps=30.0):
    stream_url = stream_to_url(url, quality)
    log.info("Loading stream {0}".format(stream_url))

    # start the video
    cap = cv2.VideoCapture(stream_url)

    while (True):
        ret, frame = cap.read()
        cv2.imshow('frame', frame)

        if cv2.waitKey(20) & 0xFF == ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()


if __name__ == "__main__":
    import argparse
    logging.basicConfig(level=logging.INFO)

    parser = argparse.ArgumentParser(description="DOPR detection on streams via Streamlink")
    parser.add_argument("url", help="Stream to play")
    parser.add_argument("--stream-quality", help="Requested stream quality [default=720p]",
                        default="best", dest="quality")
    parser.add_argument("--fps", help="Play back FPS for opencv [default=30]",
                        default=30.0, type=float)

    opts = parser.parse_args()

    main(opts.url, opts.quality, opts.fps)

0 个答案:

没有答案