Crate yt_live_rs

Crate yt_live_rs 

Source
Expand description

§yt-live-rs

A Rust library for downloading YouTube live streams.

This library provides the ability to download YouTube live streams as they are being broadcast, with support for quality selection, automatic URL refresh, and streaming fragment delivery.

§Features

  • Live stream downloading: Download audio and video fragments as they become available
  • Quality selection: Choose from various quality levels with automatic fallback
  • Automatic URL refresh: Handles expired URLs (403 errors) automatically
  • Streaming API: Receive fragments as an async stream
  • Cookie support: Access members-only or age-restricted streams

§Quick Start

use yt_live_rs::{Client, LiveStream, StreamOptions, QualitySelector, Quality};
use tokio_stream::StreamExt;
use std::sync::Arc;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create a client
    let client = Client::builder().build()?;
     
    // Get video info
    let info = client.get_video_info("https://youtube.com/watch?v=VIDEO_ID").await?;
    println!("Title: {}", info.title);
    println!("Channel: {}", info.channel);
     
    // Get available streams
    let streams = client.get_stream_info(&info.id).await?;
     
    // Select quality
    let selector = QualitySelector::new(Quality::P1080F60)
        .fallback(Quality::P1080)
        .fallback(Quality::P720);
    let video_format = selector.select(&streams);
     
    // Create download session
    let options = StreamOptions::builder()
        .audio(true)
        .video(true)
        .build();
     
    let live_stream = Arc::new(LiveStream::new(
        client,
        info.id,
        streams.audio.as_ref().map(|f| f.url.clone()),
        streams.audio.as_ref().map(|f| f.itag),
        video_format.as_ref().map(|f| f.url.clone()),
        video_format.as_ref().map(|f| f.itag),
        options,
    )?);
     
    // Download fragments
    let (mut fragments, _progress) = live_stream.fragments();
    while let Some(fragment) = fragments.next().await {
        println!("Got {} fragment #{}",
            if fragment.is_audio { "audio" } else { "video" },
            fragment.sequence
        );
    }
     
    Ok(())
}

§URL Refresh Behavior

YouTube stream URLs expire after some time (typically ~6 hours). This library handles this automatically:

  • Reactive refresh (default): On 403 errors, automatically fetches new URLs and retries
  • Periodic refresh (optional): Set with StreamOptions::builder().refresh_interval()
  • Manual refresh: Call live_stream.refresh_urls().await at any time

When both reactive and periodic refresh are enabled, the periodic timer resets after any refresh to avoid redundant refreshes.

Re-exports§

pub use client::Client;
pub use client::ClientBuilder;
pub use download::clean_mp4_fragment;
pub use download::Fragment;
pub use download::FragmentKind;
pub use download::LiveStream;
pub use download::Progress;
pub use download::StreamOptions;
pub use download::StreamOptionsBuilder;
pub use error::Error;
pub use error::Result;
pub use quality::CodecPreference;
pub use quality::Quality;
pub use quality::QualitySelector;
pub use types::Codec;
pub use types::StreamFormat;
pub use types::StreamInfo;
pub use types::VideoInfo;

Modules§

client
HTTP client for YouTube interactions.
download
Live stream downloading with streaming fragment delivery.
error
Error types for the yt-live-rs library.
quality
Video quality definitions and selection.
types
Shared types for the yt-live-rs library.
url
YouTube URL parsing utilities.