#m3u8 #http #apple #parser #media #playlist

m3u8-rs

A library for parsing m3u8 files (Apple's HTTP Live Streaming (HLS) protocol)

18 stable releases (4 major)

5.0.4 May 8, 2023
5.0.3 Dec 2, 2022
5.0.2 Sep 23, 2022
5.0.0 Jul 30, 2022
1.0.1 Jun 3, 2016

#64 in Parser implementations

Download history 1420/week @ 2023-02-11 1382/week @ 2023-02-18 1487/week @ 2023-02-25 1578/week @ 2023-03-04 1500/week @ 2023-03-11 1765/week @ 2023-03-18 1343/week @ 2023-03-25 1454/week @ 2023-04-01 1607/week @ 2023-04-08 1407/week @ 2023-04-15 1381/week @ 2023-04-22 1482/week @ 2023-04-29 1504/week @ 2023-05-06 1532/week @ 2023-05-13 1407/week @ 2023-05-20 1191/week @ 2023-05-27

5,854 downloads per month
Used in 17 crates (14 directly)

MIT license

78KB
2K SLoC

m3u8-rs

crates.io API

A Rust library for parsing m3u8 playlists (HTTP Live Streaming) link. Uses the nom library for all of the parsing.

Examples

Examples can be found in the examples folder.


lib.rs:

A library to parse m3u8 playlists HTTP Live Streaming.

Examples

Parsing a playlist and let the parser figure out if it's a media or master playlist.

use m3u8_rs::Playlist;
use nom::IResult;
use std::io::Read;

let mut file = std::fs::File::open("playlist.m3u8").unwrap();
let mut bytes: Vec<u8> = Vec::new();
file.read_to_end(&mut bytes).unwrap();

match m3u8_rs::parse_playlist(&bytes) {
    Result::Ok((i, Playlist::MasterPlaylist(pl))) => println!("Master playlist:\n{:?}", pl),
    Result::Ok((i, Playlist::MediaPlaylist(pl))) => println!("Media playlist:\n{:?}", pl),
    Result::Err(e) =>  panic!("Parsing error: \n{}", e),
}

Parsing a master playlist directly

use std::io::Read;
use nom::IResult;

let mut file = std::fs::File::open("masterplaylist.m3u8").unwrap();
let mut bytes: Vec<u8> = Vec::new();
file.read_to_end(&mut bytes).unwrap();

if let Result::Ok((_, pl)) = m3u8_rs::parse_master_playlist(&bytes) {
    println!("{:?}", pl);
}

Creating a playlist and writing it back to a vec/file

use m3u8_rs::{MediaPlaylist, MediaPlaylistType, MediaSegment};

let playlist = MediaPlaylist {
    version: Some(6),
    target_duration: 3.0,
    media_sequence: 338559,
    discontinuity_sequence: 1234,
    end_list: true,
    playlist_type: Some(MediaPlaylistType::Vod),
    segments: vec![
        MediaSegment {
            uri: "20140311T113819-01-338559live.ts".into(),
            duration: 2.002,
            title: Some("title".into()),
            ..Default::default()
        },
    ],
    ..Default::default()
};

//let mut v: Vec<u8> = Vec::new();
//playlist.write_to(&mut v).unwrap();

//let mut file = std::fs::File::open("playlist.m3u8").unwrap();
//playlist.write_to(&mut file).unwrap();

Dependencies

~0.8–1.1MB
~19K SLoC