MadRabbit
MadRabbit

Reputation: 2520

How to record twitch stream in python, preferably using livestreamer?

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?

Upvotes: 0

Views: 2875

Answers (1)

André Laszlo
André Laszlo

Reputation: 15537

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.

Upvotes: 2

Related Questions