是否可以使用OpenCV将https://(例如YouTube)的视频流传输到python中? [英] Is it possible to stream video from https:// (e.g. YouTube) into python with OpenCV?
问题描述
此链接有一个关于如何使用python的OpenCV库cv2
的整洁小示例,该数据将相机中的数据流传输到python shell中.我正在尝试做一些实验,并希望使用以下YouTube视频供稿:https://www.youtube.com/watch?v=oCUqsPLvYBQ
.
This link has a tidy little example of how to use python's OpenCV library, cv2
to stream data from a camera into your python shell. I'm looking to do some experiments and would like to use the following YouTube video feed: https://www.youtube.com/watch?v=oCUqsPLvYBQ
.
我尝试将示例修改如下:
I've tried adapting the example as follows:
import numpy as np
import cv2
cap = cv2.VideoCapture('https://www.youtube.com/watch?v=oCUqsPLvYBQ')
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Display the resulting frame
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
哪个会产生错误:
WARNING: Couldn't read movie file https://www.youtube.com/watch?v=oCUqsPLvYBQ
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file /tmp/opencv20160107-29960-t5glvv/opencv-2.4.12/modules/highgui/src/window.cpp, line 261
是否有一个简单的修复程序,可以让我通过cv2
将此视频提要流式传输到python shell中?如果有其他可以实现相同目的的库,也不是绝对致力于cv2
.
Is there a simple fix that would allow me to stream this video feed into my python shell via cv2
? Not absolutely committed to cv2
, either, if there are other libraries out there that will accomplish the same purpose.
推荐答案
您需要安装2件东西
- pafy(通过pip安装pafy)
- youtube_dl(sudo pip安装--upgrade youtube_dl)
安装这两个软件包后,您可以使用youtube网址播放youtube上的流视频. 请参考下面的代码
after installing these two packages you can use the youtube url to play the streaming videos from youtube. Please refer the code below
url = 'https://youtu.be/W1yKqFZ34y4'
vPafy = pafy.new(url)
play = vPafy.getbest(preftype="webm")
#start the video
cap = cv2.VideoCapture(play.url)
while (True):
ret,frame = cap.read()
"""
your code here
"""
cv2.imshow('frame',frame)
if cv2.waitKey(20) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
这篇关于是否可以使用OpenCV将https://(例如YouTube)的视频流传输到python中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!