The HLS Playlist Parse and Sort is a command-line application and library written in Rust. It provides functionality to fetch, parse, and sort M3U8 master playlists. The application allows you to sort stream variants, media tracks, and I-Frame streams by multiple criteria, such as bandwidth, resolution, codecs, and more.
- Fetch playlists from URLs or local file paths.
- Parse M3U8 master playlists into a structured data format.
- Sort playlists by various attributes such as bandwidth, resolution, and codecs.
- Serialize sorted playlists back to M3U8 format.
- Async operations using the
tokio
runtime.
The underlying functionality is also exposed as a library, making it easy to integrate into other projects.
The HLS Playlist Parse and Sort allows sorting HLS playlists directly from the command line. It supports sorting stream variants, media tracks, and I-Frame streams by primary and secondary attributes.
Sort an HLS playlist from a URL or file
Usage: m3u8-parse-sort [OPTIONS] <PLAYLIST_LOCATION>
Arguments:
<PLAYLIST_LOCATION> The location of the playlist. Can be a file path or an HTTP URL.
Examples:
- /path/to/playlist.m3u8
- http://example.com/playlist.m3u8
Options:
-s, --sort-stream-by <SORT_STREAM_BY>
Sort the #EXT-X-STREAM-INF elements by primary and secondary attributes (format: primary,secondary) [possible values: bandwidth, average-bandwidth, codecs, resolution, frame-rate, video-range, audio, closed-captions, uri]
-m, --sort-media-by <SORT_MEDIA_BY>
Sort the #EXT-X-MEDIA elements by primary and secondary attributes (format: primary,secondary) [possible values: type, group-id, name, language, default, auto-select, channels, uri]
-i, --sort-iframe-by <SORT_IFRAME_BY>
Sort the #EXT-X-I-FRAME-STREAM-INF elements by primary and secondary attributes (format: primary,secondary) [possible values: bandwidth, codecs, resolution, video-range, uri]
-h, --help
Print help
To fetch a playlist from a URL and sort the streams by bandwidth and resolution:
m3u8-parse-sort http://example.com/playlist.m3u8 --sort-stream-by bandwidth,resolution
To sort a playlist from a local file by audio and bandwidth:
m3u8-parse-sort /path/to/playlist.m3u8 --sort-stream-by audio,bandwidth
m3u8-parse-sort /path/to/playlist.m3u8 --sort-media-by group-id,channels
m3u8-parse-sort /path/to/playlist.m3u8 --sort-iframe-by bandwidth,resolution
To build the project, you will need to have Rust installed. You can follow the instructions here to install Rust.
Once Rust is installed, you can build the project using cargo
:
cargo build --release
This will create an optimized executable in the target/release/
directory.
To run the tests, use the following command:
cargo test
This will run all the unit tests for the library and CLI application.
You can also use the HLS Playlist Sorter as a library in your own Rust project.
use m3u8_parse_sort::{
fetch::fetch_playlist,
sort::{get_sort_order, SortStreamBy}
};
use tokio;
#[tokio::main]
async fn main() {
let playlist_url = "http://example.com/playlist.m3u8";
match fetch_playlist(playlist_url).await {
Ok(mut playlist) => {
// Sort stream variants by bandwidth and resolution
let sort_order = (SortStreamBy::Bandwidth, SortStreamBy::Resolution);
playlist.sort_stream(sort_order);
println!("Playlist sorted successfully!");
}
Err(err) => eprintln!("Error: {}", err),
}
}