Streamlink

8.3.0 · active · verified Thu Apr 16

Streamlink is a command-line utility and Python library that extracts streams from various online services (like Twitch, YouTube, etc.) and pipes them into a video player of choice, such as VLC or mpv. It aims to provide a lightweight alternative to resource-heavy and unoptimized websites. The library is actively maintained, with frequent minor and patch releases, and the current stable version is 8.3.0.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use Streamlink programmatically to find and access streams from a given URL. It initializes a Streamlink session, sets an option, resolves streams for a Twitch URL, and attempts to open the 'best' quality stream. For actual media playback, the stream's file-like object would typically be piped to an external video player process.

import streamlink
import os

# Initialize a Streamlink session
session = streamlink.Streamlink()

# Example: Set a session option, e.g., for logging level
session.set_option("log-level", "info")

# Define the URL and desired stream quality
stream_url = "https://www.twitch.tv/twitchdev"
desired_quality = "best"

try:
    # Attempt to find streams for the URL
    streams = session.streams(stream_url)
    if not streams:
        print(f"No streams found for {stream_url}")
    else:
        # Get the desired stream
        stream = streams.get(desired_quality)
        if stream:
            print(f"Opening {desired_quality} stream from {stream_url}")
            # Open the stream and read a chunk (or pipe to a player)
            with stream.open() as fd:
                # This example just reads a small chunk. For actual playback,
                # you'd typically pipe this to an external player.
                # For example, to play with VLC (requires VLC to be installed and in PATH):
                # import subprocess
                # player_cmd = ["vlc", "-"]
                # p = subprocess.Popen(player_cmd, stdin=subprocess.PIPE)
                # for chunk in iter(lambda: fd.read(8192), b''):
                #     p.stdin.write(chunk)
                # p.stdin.close()
                # p.wait()
                print(f"Successfully accessed {desired_quality} stream.")
                # For a real application, you'd stream the data to a player or file
        else:
            print(f"Stream quality '{desired_quality}' not available. Available: {', '.join(streams.keys())}")
except streamlink.exceptions.NoPluginError:
    print(f"No plugin found for URL: {stream_url}")
except streamlink.exceptions.PluginError as e:
    print(f"Plugin error: {e}")

view raw JSON →