m3u8

6.0.0 · active · verified Sat Apr 11

m3u8 is a Python library designed to parse M3U8 HTTP Live Streaming (HLS) playlists. It allows loading playlists from URIs, file paths, or strings, and provides access to segments, streams, and other HLS tag information. The library is actively maintained, with its current version being 6.0.0, and receives regular updates to support new HLS specifications and bug fixes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to load an M3U8 playlist from a string using `m3u8.loads()` and access its basic properties like target duration and individual segments. An commented-out section shows how to load directly from a URL using `m3u8.load()` for live HLS streams.

import m3u8
import requests
import os

# Example: Load a remote M3U8 playlist
# In a real scenario, use a specific, stable M3U8 URL.
# For a runnable example, we'll use a placeholder or local file.
# For demonstration, we'll simulate a playlist string.

# --- Option 1: Load from a URL (requires internet access) ---
# remote_m3u8_url = 'https://example.com/path/to/playlist.m3u8' # Replace with a real M3U8 URL if testing live
# try:
#     playlist = m3u8.load(remote_m3u8_url)
#     print(f"Loaded playlist from URL: {remote_m3u8_url}")
#     if playlist.segments:
#         print(f"First segment URI: {playlist.segments[0].uri}")
#     else:
#         print("No segments found in remote playlist.")
# except requests.exceptions.RequestException as e:
#     print(f"Error loading remote playlist: {e}")

# --- Option 2: Load from a string (always runnable) ---
local_m3u8_content = """
#EXTM3U
#EXT-X-VERSION:3
#EXT-X-TARGETDURATION:10
#EXT-X-MEDIA-SEQUENCE:0
#EXTINF:9.009,
http://example.com/segment0.ts
#EXTINF:10.000,
http://example.com/segment1.ts
#EXT-X-ENDLIST
"""

playlist_from_string = m3u8.loads(local_m3u8_content)
print("\n--- Playlist from String ---")
print(f"Target Duration: {playlist_from_string.target_duration}")
print(f"Number of segments: {len(playlist_from_string.segments)}")
if playlist_from_string.segments:
    print(f"First segment URI: {playlist_from_string.segments[0].uri}")

# Iterate through segments
print("\nSegments:")
for i, segment in enumerate(playlist_from_string.segments):
    print(f"  Segment {i}: Duration={segment.duration}, URI={segment.uri}")

view raw JSON →