mirror of
https://github.com/narrrl/ytd-rs.git
synced 2026-11-03 16:41:27 +01:00
f5d208324820c215aad606d74fe02456bd8811ad
ytd-rs
An async, feature-rich Rust wrapper for yt-dlp.
Features
- Async API: Built on top of
tokio. - Builder Pattern: Fluent API for configuring downloads.
- Structured Metadata: Extract video info as JSON via
serde. - Custom Arguments: Passthrough for any
yt-dlpflag.
Prerequisites
You must have yt-dlp installed and available in your PATH.
Usage
Simple Download
use ytd_rs::YtDlp;
use std::path::PathBuf;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let ytd = YtDlp::new("https://www.youtube.com/watch?v=uTO0KnDsVH0")
.output_dir(PathBuf::from("./downloads"))
.arg("--quiet");
let result = ytd.download().await?;
println!("Output: {}", result.output());
Ok(())
}
Extract Audio
use ytd_rs::YtDlp;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
YtDlp::new("https://www.youtube.com/watch?v=uTO0KnDsVH0")
.extract_audio(true)
.audio_format("mp3")
.download()
.await?;
Ok(())
}
Get Video Metadata
use ytd_rs::YtDlp;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let ytd = YtDlp::new("https://www.youtube.com/watch?v=uTO0KnDsVH0");
let infos = ytd.get_info().await?;
for info in infos {
println!("Title: {}", info.title);
println!("Duration: {:?}", info.duration);
}
Ok(())
}
Streaming Output (Progress Bars)
If you want to track the progress of a long-running download, you can stream the standard output line-by-line:
use ytd_rs::YtDlp;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut process = YtDlp::new("https://www.youtube.com/watch?v=uTO0KnDsVH0")
.download_process()
.await?;
while let Some(line) = process.next_line().await? {
// e.g. "[download] 10.0% of 50.00MiB at 1.00MiB/s ETA 00:50"
println!("yt-dlp output: {}", line);
}
process.wait().await?;
println!("Download finished successfully!");
Ok(())
}
License
MIT
Languages
Rust
100%