4

使用这个 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()

我在这里上传了一个数据示例。

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

更新:好的,我让 OpenCV 工作了,它可以抓取我拥有的随机视频文件的第一帧。但是,当我在文件中使用相同的代码和流数据时,它会产生一个无意义的图像。

4

1 回答 1

5

好吧,我想通了。确保写入二进制数据,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()
于 2013-09-28T15:56:01.830 回答