#m3u8 #tags #parser #hls #playlist #version #rfc

m3u8-parser

A Rust crate for parsing and creating M3U8 files (HLS)

7 releases (breaking)

0.6.1 Nov 5, 2024
0.6.0 Nov 5, 2024
0.5.0 Nov 2, 2024
0.4.0 Nov 1, 2024
0.1.0 Nov 1, 2024

#62 in Video

Download history 330/week @ 2024-10-28 203/week @ 2024-11-04 9/week @ 2024-11-11

542 downloads per month

MIT license

76KB
1.5K SLoC

M3U8 Parser

Crates.io Documentation check

A Rust crate for parsing and creating M3U8 version 7 files for HTTP Live Streaming (HLS), as specified by RFC 8216.

[!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 and write them to strings, files, or writers
  • Support for all tags specified in RFC 8216, including:
    • 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-MEDIA
      • #EXT-X-STREAM-INF
      • #EXT-X-I-FRAME-STREAM-INF
      • #EXT-X-INDEPENDENT-SEGMENTS
      • #EXT-X-BYTERANGE
      • #EXT-X-SESSION-DATA
      • #EXT-X-SESSION-KEY
      • #EXT-X-DEFINE
    • Media Segment Tags:
      • #EXTINF
      • #EXT-X-KEY
      • #EXT-X-BYTERANGE
      • #EXT-X-MAP
      • #EXT-X-GAP
      • #EXT-X-PROGRAM-DATE-TIME
      • #EXT-X-PART
      • #EXT-X-PRELOAD-HINT
      • #EXT-X-START
      • #EXT-X-DATERANGE
    • Encryption Tags:
      • #EXT-X-KEY
      • #EXT-X-SESSION-KEY
    • Date Range Tags:
      • #EXT-X-DATERANGE
    • End Playlist Tags:
      • #EXT-X-ENDLIST
    • Master Playlist Tags:
      • #EXT-X-STREAM-INF
      • #EXT-X-MEDIA
      • #EXT-X-STREAM-INF
      • #EXT-X-I-FRAME-STREAM-INF
    • Program Date and Time:
      • #EXT-X-PROGRAM-DATE-TIME

Installation

Add this to your Cargo.toml:

[dependencies]
m3u8-parser = "0.6.1"

Usage

Parsing a Playlist

use m3u8_parser::m3u8::playlist::Playlist;

fn main() -> Result<(), Box<dyn std::error::Error>> {
  let data = r#"
    #EXTM3U
    #EXT-X-VERSION:7
    #EXT-X-TARGETDURATION:6
    #EXTINF:5.009,
    https://media.example.com/first.ts
    #EXTINF:5.009,
    https://media.example.com/second.ts
    #EXTINF:3.003,
    https://media.example.com/third.ts
    #EXT-X-ENDLIST
    "#;

  let playlist = Playlist::from_reader(data.as_bytes())?;
  println!("{:?}", playlist);
  Ok(())
}

Creating a Playlist

use m3u8_parser::m3u8::playlist::builder::PlaylistBuilder;

fn main() -> Result<(), Box<dyn std::error::Error>> {
  let playlist = PlaylistBuilder::new()
          .extm3u()
          .version(7)
          .target_duration(6)
          .extinf(5.009, None)
          .uri("https://media.example.com/first.ts".to_string())
          .extinf(5.009, None)
          .uri("https://media.example.com/second.ts".to_string())
          .extinf(3.003, None)
          .uri("https://media.example.com/third.ts".to_string())
          .end_list()
          .build()?;

  playlist.write_to_file("playlist.m3u8")?;
  Ok(())
}

Dependencies

~2–3MB
~54K SLoC