Geek Culture
Published in

Geek Culture

Using Python and Streamlink to Download Twitch Livestreams

Photo by Stanley Li on Unsplash

Introduction + Prerequisites

Want to download Twitch streams in real time instead of waiting for the entire recording to be available? Are you an editor and want to speed up turnaround time for creating YouTube content and compilations from your streams?

This is going to be a quick and easy Python project with a lot of use cases and ways you can build upon it. Let’s start with the prerequisites you’ll need to get started:

  1. Install Python: anything version ≥ 3.8
  2. Install Streamlink with pip3: pip3 install ‘streamlink>=2.2.0’
  3. Install ffmpeg

Now all of our dependencies installed and ready to go.

Next, we need to register our application with Twitch to get a client ID and secret. Head over to the Twitch developer console and register a new application. Once that’s done, you should now have an ID and secret to use.

Python Wrapper for Twitch and Streamlink

Thanks to the power of open source and GitHub, we don’t need to write our own Python wrapper for using the Twitch API and Streamlink. We’ll basically be using the script in this repository and adding our own configuration. I found this repository while writing my own Python script. It’s already well written and documented so I scrapped mine. Go ahead and clone that and give it a star :).

In config.py, add a path you want to download the livestream to, the Twitch username of who you want to record, and your client ID and secret.

If we take a look at twitch-recorder.py, we see that the script pulls in the configuration file and checks our client credentials. It also checks if the username’s stream is online before starting up Streamlink to begin downloading.

If we follow the README, running:

python3 twitch-recorder.py

should be all we need to start recording. Note, if this doesn’t work, go back to the previous section and make sure all of the dependencies and Twitch credentials are correct. Personally, I needed to update Streamlink to version 2.2.0 for it to work with Twitch.

Next Steps

The Python script we have created is insanely simple. It simply starts a Streamlink recording process with the given parameters. However, because we now have a Python wrapper for Streamlink, we can expand on this script in a variety of ways. I’ll leave it up to you to implement these yourself, but here’s a list of things you can try:

Integrate with the Twitch API

Integrating with the Twitch API allows you to do a bunch of cool things such as:

  • Listen for when a specific streamer has gone online
  • Listen for when a streamer changes the stream category/title
  • Read Twitch chat

By subscribing to channel events such as when a channel goes live and when it is offline, you can start recording the livestream at the right time and not miss and portions of it. We can also choose to record only when the streamer is streaming a specific category such as “Just Chatting”. This would require a server or an always-running script to subscribe to channel events.

Record Multiple Streams at Once

If you’re an editor for multiple channels or you just have multiple channels you’re interest in, you can record multiple streams at once by modifying the Python script to spawn multiple Streamlink recording processes at once for each desired stream. Doing this would be cool for compiling multiple POV’s for multiplayer games. For example, games like GTA where several streamers hop on the same server and role play together would be a good use case for this.

Sharing Recordings

If you’re part of a team of editors or content creators, you can set up a file sharing server and record the streams there so everyone can access them without recording themselves.

Video Processing

To further automate your editing process, try using Python to do some simple editing tasks. MoviePy is a Python library for common editing tasks such as cutting. This can be used for things like cutting the video whenever there is a change in stream title.

Conclusion

In summary, we were able to use Python and Streamlink to record livestreams from Twitch in real-time. This has a lot of applications and helps speed up the content creation and editing process. Hopefully you learned something from this short project and are inspired to build new things with it!

--

--

A new tech publication by Start it up (https://medium.com/swlh).

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store