【问题标题】:Downloading the first frame of a twitch.tv stream下载 twitch.tv 流的第一帧
【发布时间】:2013-10-01 10:40:32
【问题描述】:

使用this api 我已经设法下载流数据,但我不知道如何解析它。我查看了 RMTP 格式,但似乎不匹配。

from livestreamer import Livestreamer

livestreamer = Livestreamer()

# set to a stream that is actually online
plugin = livestreamer.resolve_url("http://twitch.tv/froggen")
streams = plugin.get_streams()
stream = streams['mobile_High']
fd = stream.open()
data = fd.read()

我已经上传了here的数据示例。

理想情况下,我不必将其解析为视频,我只需要将第一个关键帧作为图像。任何帮助将不胜感激!

更新:好的,我让 OpenCV 工作了,它可以抓取我拥有的随机视频文件的第一帧。然而,当我在文件中使用相同的代码和流数据时,它产生了nonsense image

【问题讨论】:

  • 我设法用 OpenCV 库打开了你的文件(Python 代码:capture = cv2.VideoCapture("downloads/( uploadMB.com ) stream.bin"))我设法从中得到了一个图像(3 通道,1280x720),但它看起来像一个五彩斑斓的烂摊子。您确定文件中存在有效的关键帧吗?
  • 我刚刚尝试下载the first 1 MB of stream data,然后使用cv2.VideoCapture,然后使用capture.read,但它返回False/None。您是如何从中获得图像的?

标签: python video-streaming rtmp


【解决方案1】:

好吧,我想通了。确保写入二进制数据,OpenCV 能够解码第一个视频帧。生成的图像切换了 R 和 B 通道,但这很容易纠正。下载大约 300 kB 似乎足以确保完整的图像在那里。

import time, Image

import cv2
from livestreamer import Livestreamer

# change to a stream that is actually online
livestreamer = Livestreamer()
plugin = livestreamer.resolve_url("http://twitch.tv/flosd")
streams = plugin.get_streams()
stream = streams['mobile_High']

# download enough data to make sure the first frame is there
fd = stream.open()
data = ''
while len(data) < 3e5:
    data += fd.read()
    time.sleep(0.1)
fd.close()

fname = 'stream.bin'
open(fname, 'wb').write(data)
capture = cv2.VideoCapture(fname)
imgdata = capture.read()[1]
imgdata = imgdata[...,::-1] # BGR -> RGB
img = Image.fromarray(imgdata)
img.save('frame.png')
# img.show()

【讨论】:

  • 我正在尝试完成相同的任务,但对capture.read() 的调用没有返回任何结果。我已经用你上传的 bin 进行了尝试,但我得到了相同的 None 结果。有什么想法吗?
  • 您无法对 bin 文件调用 read(),因为 ffmpeg(视频解码器)无法正确访问,因此打开文件失败。请参阅stackoverflow.com/questions/11699298/… 和类似内容。我通过从这里下载和安装 opencv 来修复:lfd.uci.edu/~gohlke/pythonlibs
猜你喜欢
  • 2014-10-26
  • 2012-03-19
  • 1970-01-01
  • 2015-04-28
  • 2012-06-18
  • 1970-01-01
  • 1970-01-01
  • 2019-03-02
  • 2011-12-04
相关资源
最近更新 更多