M3U8 Parser
A Rust crate for parsing and creating M3U8 files for HTTP Live Streaming (HLS), as specified by RFC 8216 and the protocol versions up to 12 defined by draft-pantos-hls-rfc8216bis.
[!IMPORTANT] This project is currently under active development. Please note that features and APIs are subject to change. My goal is to ensure full compatibility with RFC 8216.
Features
- Parse M3U8 playlists from strings, files, or readers
- Generate M3U8 playlists with
PlaylistBuilderand write them to strings (Display/FromStr), files, or anystd::io::Write - Resolve
EXT-X-DEFINEvariables (NAME/VALUE,IMPORT,QUERYPARAM) withPlaylist::substitute_variables - Typed attribute values such as
ByteRange,Key,Map, and numericTIME-OFFSET, withf64durations - Derive
MediaSegmentvalues from a playlist with the media sequence number, discontinuity flag, and effectiveKEY,MAP,BYTERANGE,GAP, andPROGRAM-DATE-TIMEstate for eachEXTINFsegment. - Parses, models, and serializes every RFC 8216 tag family listed below. Standard
tags use order-independent attribute parsing, and unsupported extension tags and
unmodeled attributes on
EXT-X-DATERANGE,EXT-X-STREAM-INF,EXT-X-I-FRAME-STREAM-INF, andEXT-X-MEDIA(such asVIDEO-RANGEandHDCP-LEVEL) are preserved for round-tripping. - Typed parse errors (
ParseError) carrying the 1-based line number and a matchableSyntaxErrorkind. - RFC 8216 semantic validation enforces
EXTM3Uplacement, singleton tags, Master/Media Playlist separation,EXT-X-STREAM-INFURI pairing, required media target durations, RFC target-duration rounding, tag version gates (versions 1 through 12), tags removed in later versions, encryption and session key attribute combinations,CLOSED-CAPTIONSrendition rules, ISO 8601 date-times, byte-range continuity,END-ON-NEXTdate-range constraints, and Master Playlist rendition-group references. Validation still does not enforce every cross-tag constraint defined by the RFC. - RFC 8216 tag coverage:
- Basic Tags:
#EXTM3U#EXT-X-VERSION
- Media Playlist Tags:
#EXT-X-TARGETDURATION#EXT-X-MEDIA-SEQUENCE#EXT-X-ALLOW-CACHE#EXT-X-DISCONTINUITY-SEQUENCE#EXT-X-ENDLIST#EXT-X-PLAYLIST-TYPE#EXT-X-I-FRAMES-ONLY
- Media Segment Tags:
#EXTINF#EXT-X-KEY#EXT-X-BYTERANGE#EXT-X-MAP#EXT-X-GAP#EXT-X-PROGRAM-DATE-TIME#EXT-X-DATERANGE
- Master Playlist Tags:
#EXT-X-STREAM-INF#EXT-X-MEDIA#EXT-X-I-FRAME-STREAM-INF#EXT-X-SESSION-DATA#EXT-X-SESSION-KEY#EXT-X-INDEPENDENT-SEGMENTS#EXT-X-START#EXT-X-DEFINE
- Basic Tags:
- Low-latency and draft extension tags from draft-pantos-hls-rfc8216bis:
#EXT-X-PART,#EXT-X-PART-INF,#EXT-X-PRELOAD-HINT,#EXT-X-RENDITION-REPORT,#EXT-X-SERVER-CONTROL,#EXT-X-SKIP, and#EXT-X-BITRATE.
Installation
Add this to your Cargo.toml:
[]
= "0.8.0"
Usage
Parsing a Playlist
use Playlist;
Parse failures are reported as ParseError, which carries the line number and
a matchable SyntaxError:
use ;
use Playlist;
let data = "#EXTM3U\n#EXTINF:6.0,\n";
match from_reader
Creating a Playlist
use PlaylistBuilder;