问题描述
此链接有关于如何使用 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 个东西
you need to have 2 things installed
- pafy (pip install pafy)
- youtube_dl (sudo pip install --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 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!