Downloading the first frame of a twitch.tv stream

前端 未结 1 1497
孤街浪徒
孤街浪徒 2021-01-03 11:13

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.



        
1条回答
  •  花落未央
    2021-01-03 11:21

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

    0 讨论(0)
提交回复
热议问题