gpt4 book ai didi

python - 是否可以从 https ://(e. g. YouTube)用 OpenCV 转换成 python?

转载 作者:太空狗 更新时间:2023-10-29 17:45:46 25 4
gpt4 key购买 nike

This link有一个简洁的小示例,说明如何使用 python 的 OpenCV 库 cv2 将数据从相机流式传输到 python shell。我想做一些实验并想使用以下 YouTube 视频源:https://www.youtube.com/watch?v=oCUqsPLvYBQ

我已尝试按如下方式调整示例:

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,如果有其他库可以实现相同的目的。

最佳答案

你需要安装两个东西

  1. pafy(pip 安装 pafy)
  2. youtube_dl(sudo pip install --upgrade youtube_dl)

安装这两个软件包后,您可以使用 youtube url 播放来自 youtube 的流媒体视频。请引用下面的代码

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()

关于python - 是否可以从 https ://(e. g. YouTube)用 OpenCV 转换成 python?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37555195/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com