下载twitch.tv流的第一帧

时间:2013-09-25 17:42:36

标签: python video-streaming rtmp

使用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

1 个答案:

答案 0 :(得分:3)

好吧,我明白了。确保写为二进制数据,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()