Command-line utility and Python library for extracting video streams from various streaming services
Streamlink provides multiple stream types for different protocols and use cases. All streams inherit from the base Stream class and provide methods for opening connections and accessing stream data.
The abstract base class that all stream types inherit from.
class Stream:
def __init__(self, session):
"""
Initialize stream with Streamlink session.
Parameters:
- session: Streamlink session instance
"""
def open(self) -> StreamIO:
"""
Open stream connection for reading.
Returns:
StreamIO object for data access
Raises:
StreamError: If stream cannot be opened
"""
def to_url(self) -> str:
"""
Convert stream to direct URL if supported.
Returns:
Direct stream URL
Raises:
TypeError: If stream type doesn't support URL conversion
"""
def to_manifest_url(self) -> str:
"""
Convert stream to manifest URL if supported.
Returns:
Manifest URL (e.g., M3U8 or MPD)
Raises:
TypeError: If stream type doesn't support manifest URLs
"""
@classmethod
def shortname(cls) -> str:
"""
Get stream type identifier.
Returns:
Short name identifying the stream type
"""
@property
def json(self) -> str:
"""JSON representation of stream metadata"""Base class for stream data access with file-like interface.
class StreamIO:
"""Base class for stream I/O operations with file-like interface"""Direct HTTP streams for simple video/audio files served over HTTP.
class HTTPStream(Stream):
def __init__(self, session, url, buffered=True, **kwargs):
"""
Initialize HTTP stream.
Parameters:
- session: Streamlink session
- url: HTTP URL to stream
- buffered: Whether to use buffered I/O (default: True)
- **kwargs: Additional request arguments (headers, params, etc.)
"""
@property
def url(self) -> str:
"""Prepared request URL"""
@property
def args(self) -> dict:
"""Request arguments dictionary"""HTTP Live Streaming (HLS) support for adaptive bitrate streaming.
class HLSStream(Stream):
"""
HTTP Live Streaming implementation supporting:
- M3U8 playlist parsing
- Adaptive bitrate streaming
- AES encryption
- Subtitle tracks
"""
class MuxedHLSStream(Stream):
"""
HLS stream combining multiple tracks (video, audio, subtitles).
Automatically muxes separate video and audio streams into
a single output stream using FFmpeg.
"""Dynamic Adaptive Streaming over HTTP for modern adaptive streaming.
class DASHStream(Stream):
"""
DASH (Dynamic Adaptive Streaming over HTTP) implementation supporting:
- MPD manifest parsing
- Multi-period content
- Multiple representations
- Audio/video track selection
"""Generic muxed streams combining multiple input streams using FFmpeg.
class MuxedStream(Stream):
def __init__(self, session, *substreams, **options):
"""
Combine multiple streams into a single output.
Parameters:
- session: Streamlink session
- *substreams: Input streams to mux together
- **options: FFmpeg options and stream mapping
"""Utility classes for adapting different data sources to the StreamIO interface.
class StreamIOWrapper:
"""Wraps non-IOBase file-like objects to provide consistent interface"""
class StreamIOIterWrapper:
"""Wraps iterators as file-like objects for streaming data"""
class StreamIOThreadWrapper:
"""Adds threading support to stream I/O operations"""import streamlink
# Get streams for a URL
streams = streamlink.streams("https://www.twitch.tv/example")
if streams:
# Access best quality stream
best_stream = streams['best']
print(f"Stream type: {best_stream.shortname()}")
# Open stream for reading
with best_stream.open() as stream_fd:
# Read first 1KB
data = stream_fd.read(1024)
print(f"Read {len(data)} bytes")streams = streamlink.streams("https://example.com/stream")
# Check available qualities
print("Available streams:")
for quality, stream in streams.items():
print(f" {quality}: {stream}")
# Select specific quality
if '720p' in streams:
selected_stream = streams['720p']
elif 'best' in streams:
selected_stream = streams['best']
else:
print("No suitable stream found")from streamlink.stream import HTTPStream
session = streamlink.Streamlink()
# Create HTTP stream with custom headers
http_stream = HTTPStream(
session,
"https://example.com/video.mp4",
headers={'Referer': 'https://example.com'}
)
# Get direct URL
try:
direct_url = http_stream.to_url()
print(f"Direct URL: {direct_url}")
except TypeError:
print("Stream doesn't support direct URL access")
# Read stream data
with http_stream.open() as fd:
chunk = fd.read(8192)
while chunk:
# Process chunk
process_data(chunk)
chunk = fd.read(8192)streams = streamlink.streams("https://example.com/hls_stream")
for quality, stream in streams.items():
if hasattr(stream, 'to_manifest_url'):
try:
manifest_url = stream.to_manifest_url()
print(f"{quality}: {manifest_url}")
except TypeError:
passfrom streamlink.stream import MuxedStream
# Assuming we have separate video and audio streams
video_stream = streams.get('720p_video')
audio_stream = streams.get('audio_en')
if video_stream and audio_stream:
# Create muxed stream
muxed = MuxedStream(
session,
video_stream,
audio_stream,
format='mp4',
acodec='aac',
vcodec='h264'
)
with muxed.open() as fd:
# Process combined stream
data = fd.read(1024)streams = streamlink.streams("https://example.com")
for quality, stream in streams.items():
stream_type = stream.shortname()
if stream_type == 'hls':
print(f"{quality}: HLS stream")
elif stream_type == 'dash':
print(f"{quality}: DASH stream")
elif stream_type == 'http':
print(f"{quality}: HTTP stream")
elif stream_type == 'muxed':
print(f"{quality}: Muxed stream")Install with Tessl CLI
npx tessl i tessl/pypi-streamlink