yt_live_rs/
lib.rs

1//! # yt-live-rs
2//!
3//! A Rust library for downloading YouTube live streams.
4//!
5//! This library provides the ability to download YouTube live streams
6//! as they are being broadcast, with support for quality selection,
7//! automatic URL refresh, and streaming fragment delivery.
8//!
9//! ## Features
10//!
11//! - **Live stream downloading**: Download audio and video fragments as they become available
12//! - **Quality selection**: Choose from various quality levels with automatic fallback
13//! - **Automatic URL refresh**: Handles expired URLs (403 errors) automatically
14//! - **Streaming API**: Receive fragments as an async stream
15//! - **Cookie support**: Access members-only or age-restricted streams
16//!
17//! ## Quick Start
18//!
19//! ```no_run
20//! use yt_live_rs::{Client, LiveStream, StreamOptions, QualitySelector, Quality};
21//! use tokio_stream::StreamExt;
22//! use std::sync::Arc;
23//!
24//! #[tokio::main]
25//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
26//!     // Create a client
27//!     let client = Client::builder().build()?;
28//!     
29//!     // Get video info
30//!     let info = client.get_video_info("https://youtube.com/watch?v=VIDEO_ID").await?;
31//!     println!("Title: {}", info.title);
32//!     println!("Channel: {}", info.channel);
33//!     
34//!     // Get available streams
35//!     let streams = client.get_stream_info(&info.id).await?;
36//!     
37//!     // Select quality
38//!     let selector = QualitySelector::new(Quality::P1080F60)
39//!         .fallback(Quality::P1080)
40//!         .fallback(Quality::P720);
41//!     let video_format = selector.select(&streams);
42//!     
43//!     // Create download session
44//!     let options = StreamOptions::builder()
45//!         .audio(true)
46//!         .video(true)
47//!         .build();
48//!     
49//!     let live_stream = Arc::new(LiveStream::new(
50//!         client,
51//!         info.id,
52//!         streams.audio.as_ref().map(|f| f.url.clone()),
53//!         streams.audio.as_ref().map(|f| f.itag),
54//!         video_format.as_ref().map(|f| f.url.clone()),
55//!         video_format.as_ref().map(|f| f.itag),
56//!         options,
57//!     )?);
58
59//!     
60//!     // Download fragments
61//!     let (mut fragments, _progress) = live_stream.fragments();
62//!     while let Some(fragment) = fragments.next().await {
63//!         println!("Got {} fragment #{}",
64//!             if fragment.is_audio { "audio" } else { "video" },
65//!             fragment.sequence
66//!         );
67//!     }
68//!     
69//!     Ok(())
70//! }
71//! ```
72//!
73//! ## URL Refresh Behavior
74//!
75//! YouTube stream URLs expire after some time (typically ~6 hours). This library
76//! handles this automatically:
77//!
78//! - **Reactive refresh (default)**: On 403 errors, automatically fetches new URLs and retries
79//! - **Periodic refresh (optional)**: Set with `StreamOptions::builder().refresh_interval()`
80//! - **Manual refresh**: Call `live_stream.refresh_urls().await` at any time
81//!
82//! When both reactive and periodic refresh are enabled, the periodic timer resets
83//! after any refresh to avoid redundant refreshes.
84
85// Public modules with user-facing APIs
86pub mod client;
87pub mod download;
88pub mod error;
89pub mod quality;
90pub mod types;
91
92// Internal modules (implementation details)
93pub(crate) mod cookies;
94pub(crate) mod manifest;
95pub(crate) mod player_response;
96pub mod url;
97pub(crate) mod ytcfg;
98
99// Re-exports for convenient access
100pub use client::{Client, ClientBuilder};
101pub use download::{
102    clean_mp4_fragment, Fragment, FragmentKind, LiveStream, Progress, StreamOptions,
103    StreamOptionsBuilder,
104};
105pub use error::{Error, Result};
106pub use quality::{CodecPreference, Quality, QualitySelector};
107pub use types::{Codec, StreamFormat, StreamInfo, VideoInfo};