Overview
This service implements theIParser interface from @hls-downloader/core and wraps the m3u8-parser library to:
- Parse master playlists to extract available quality levels and media groups
- Parse media playlists to extract fragment URIs and encryption keys
- Inspect encryption metadata including AES-128 keys and initialization vectors
- Convert relative URLs to absolute URLs using the playlist base URL
Interface: IParser
The M3U8 parser implements three core methods:parseMasterPlaylist
Parses a master playlist to extract video streams, audio tracks, subtitles, and closed captions.Parameters
Raw M3U8 master playlist content
Base URL for resolving relative playlist URIs
Returns
Level[]
Array of level objects representing all available streams, audio tracks, subtitles, and closed captions
Level types
The returned array contains different level types: Stream levels (type: "stream"):
Unique identifier (UUID v4)
Base URL of the master playlist
Absolute URL to the media playlist
Bandwidth in bits per second (from BANDWIDTH attribute)
Frame rate (from FRAME-RATE attribute)
Video width in pixels
Video height in pixels
Audio group ID that this stream is associated with
type: "audio"):
Unique identifier combining label and group ID
Absolute URL to the audio playlist
ISO 639 language code
Human-readable track name
Audio channel configuration (e.g., “2”, “6”)
Additional track characteristics
Whether this is the default audio track
Whether this track should be auto-selected
Audio group identifier
type: "subtitle"):
Unique identifier combining label and group ID
Absolute URL to the subtitle playlist
ISO 639 language code
Human-readable subtitle name
Whether this is a forced subtitle track
Additional track characteristics
Whether this is the default subtitle track
Whether this track should be auto-selected
Subtitle group identifier
In-stream identifier for closed captions
Example
parseLevelPlaylist
Parses a media playlist to extract individual fragment URIs and encryption metadata.Parameters
Raw M3U8 media playlist content
Base URL for resolving relative fragment URIs
Returns
Fragment[]
Array of fragment objects representing downloadable media segments
Fragment structure
Sequential index of the fragment (starting at 0)
Absolute URL to the fragment file
Encryption key information:
uri: Absolute URL to the encryption key (ornullif not encrypted)iv: Initialization vector as a hex string (ornullif not specified)
Behavior
- Init segments: If a segment has a
#EXT-X-MAPtag, the parser inserts a separate fragment entry for the initialization segment before the media segment - Encryption: Each fragment includes encryption metadata from the
#EXT-X-KEYtag. If no key is present, bothuriandivarenull - URL resolution: All URIs are converted to absolute URLs using
url-toolkit’sbuildAbsoluteURLfunction
Example
inspectLevelEncryption
Extracts encryption metadata from a media playlist without parsing individual segments.Parameters
Raw M3U8 media playlist content
Base URL for resolving relative key URIs
Returns
ParsedEncryption
Object containing encryption methods, key URIs, and initialization vector
ParsedEncryption structure
Array of encryption methods used (e.g.,
["AES-128"]). Excludes "NONE"Array of absolute URLs to encryption keys
Initialization vector as a hex string (e.g.,
"0x123..."), or null if not foundBehavior
- Inspects both segment keys and session keys (if present)
- Normalizes all encryption methods to uppercase
- Converts initialization vectors from
Uint8ArrayorUint32Arrayto hex strings - Returns the first IV found in the playlist
Example
The parser uses
m3u8-parser under the hood, which handles various HLS specification edge cases and vendor-specific extensions.TypeScript definitions
The parser includes type definitions for the underlyingm3u8-parser library:
Source location
- Implementation:
~/workspace/source/src/background/src/services/m3u8-parser.ts:9 - Type definitions:
~/workspace/source/src/background/src/services/m3u8-parser.d.ts:1