#download #youtube-dl #livestream

bin+lib yta-rs

Helper crate to download YouTube live streams

1 unstable release

0.0.1 Jun 28, 2023

#13 in #youtube-dl

MIT license

465KB
1K SLoC

yta-rs

Minimal implementation of Kethsar/ytarchive in Rust.

⚠️ This crate is still very new. The API is not yet finalized and may change at any moment. Use at your own discretion.

Usage

This crate is meant to be used as a library. Currently, the executable only has one mode, which is to download the highest quality audio and video fragments, and compose a HLS playlist.

# Start downloading
cargo run https://www.youtube.com/watch?v=Io7ucwiaONc

# Run a webserver
cd yta_dl
python3 -m http.server 8080

lib.rs:

yta-rs

This crate provides a library for downloading YouTube DASH live streams. It is based on Kethsar/ytarchive, but is more stripped down and geared towards being a library.

Usage

yta-rs is a low-level library, and so you'll need to write your own logic to download and handle segments. The following example shows how to fetch the initial player response and start downloading segments using the worker module.

use yta_rs::{player_response::InitialPlayerResponse, util, worker};

#[tokio::main]
async fn main() {
    // Create HttpClient, a wrapper around reqwest::Client but includes a
    // middleware for retrying transient errors
    let client = util::HttpClient::new().unwrap();

    // Fetch the video page
    let html = client.fetch_text("https://www.youtube.com/watch?v=...").await.unwrap();

    // Parse the initial player response
    let ipr = InitialPlayerResponse::from_html(html.as_str()).unwrap();

    // Get the status of the stream
    if ipr.is_usable() {
        println!("Video is live");
    } else {
        println!("Video is not live");
        return;
    }

    // Start the worker
    let workdir = std::path::Path::new(".");
    worker::start(&client, &ipr, workdir).await.unwrap();
}

The worker module provides a start function that will download segments and write them to disk. It will also write an index.m3u8 file that can be used to play the stream.

Dependencies

~14–29MB
~444K SLoC