0

Currently all I have is:

from livestreamer import Livestreamer
session = Livestreamer()
stream = session.streams('http://www.twitch.tv/riotgames')
stream = stream['source']
fd = stream.open()

As I'm still a newbie to python I'm at complete loss on what I should do next. How do I continuously save, let's say, last 40 seconds of the stream to file?

MadRabbit
  • 2,460
  • 2
  • 17
  • 18

1 Answers1

2

Here's a start:

from livestreamer import Livestreamer
session = Livestreamer()
stream = session.streams('http://www.twitch.tv/riotgames')
stream = stream['source']
fd = stream.open()
with open("/tmp/stream.dat", 'wb') as f:
    while True:
        data = fd.read(1024)
        f.write(data)

I've tried it. You can open the /tmp/stream.dat in VLC, for example. The example will read 1 kb at a time and write it to a file.

The program will run forever so you have to interrupt it with Ctrl-C, or add some logic for that. You probably need to handle errors and the end of a stream somehow.

André Laszlo
  • 15,169
  • 3
  • 63
  • 81
  • Thanks! Could you explain what exactly does this do this do `data = fd.read(1024)` ? What changes if I change it to more or less kilobits? @AndréLaszlo – MadRabbit Jul 25 '15 at 22:13
  • 1
    Not much will change. Too little and you will write more often which might lead to more overhead and too big will probably not be a problem except for memory usage. I think. – André Laszlo Jul 25 '15 at 22:24
  • 1
    [This answers](http://stackoverflow.com/a/237495/98057) discusses some of the things to consider when choosing a buffer size (but for reading from disk). I guess you can try different sizes and see what works best, if you want to optimize. Maybe 1024 is a bit small if most system block sizes are 4096. – André Laszlo Jul 26 '15 at 14:04