下载的第一帧抽搐电视流动

2024-09-27 20:17:36 发布

您现在位置:Python中文网/ 问答频道 /正文

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


Tags: to数据fromimportapistream格式this
1条回答
网友
1楼 · 发布于 2024-09-27 20:17:36

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

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

相关问题 更多 >

    热门问题