Downloading the first frame of a twitch.tv stream

懵懂的女人 提交于 2019-11-29 22:43:42

问题


Using this api I've managed to download stream data, but I can't figure out how to parse it. I've looked at the RMTP format, but it doesn't seem to match.

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

I've uploaded an example of the data here.

Ideally I wouldn't have to parse it as video, I only need the first keyframe as an image. Any help would be greatly appreciated!

Update: Ok, I got OpenCV working, it works for grabbing the first frame of a random video file I had. However, it produced a nonsense image when I used the same code on file with stream data.


回答1:


Alright, I figured it out. Made sure to write as binary data, and OpenCV is able to decode the first video frame. The resulting image had R and B channels switched, but that was easily corrected. Downloading about 300 kB seems to be enough to be sure that the full image is there.

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


来源:https://stackoverflow.com/questions/19011691/downloading-the-first-frame-of-a-twitch-tv-stream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!