3

I am looking for a way to save the current frame, from a specified live Twitch.tv channel, to disk. Any programming language is welcomed.


So far I've found a possible solution using Python here but unfortunately it doesn't work.

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['best']

# 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')


Apparently cv2.VideoCapture(fname) returns none although it managed to write about 300K of information to stream.bin

Community
  • 1
  • 1
sheitan
  • 1,076
  • 12
  • 27
  • Maybe a dumb question, but was the twitch stream live when you tried to run the script? What does the contents of the `bin` look like? – Danny Nov 18 '13 at 13:40
  • The stream was live. To avoid uploading another file you can use the one that the author of the code provided [from here](http://www.uploadmb.com/dw.php?id=1380130622). I couldn't call read() on my `bin` nor his – sheitan Nov 18 '13 at 13:52
  • You can't call read() on the bin file because ffmpeg (the video decoder) is not properly accessible, so opening the file is failing. See http://stackoverflow.com/questions/11699298/opencv-2-4-videocapture-not-working-on-windows and similar. I fixed by downloading and installing opencv from here: http://www.lfd.uci.edu/~gohlke/pythonlibs/ – profesor_tortuga Sep 24 '15 at 16:31

0 Answers0