mirror of
https://github.com/narrrl/ytd-rs.git
synced 2026-11-04 00:47:02 +01:00
91 lines
2.4 KiB
Markdown
91 lines
2.4 KiB
Markdown
# ytd-rs
|
|
[](https://github.com/nirusu99/ytd-rs/actions)
|
|
[](https://crates.io/crates/ytd-rs)
|
|
[](https://docs.rs/ytd-rs)
|
|
|
|
An async, feature-rich Rust wrapper for [yt-dlp](https://github.com/yt-dlp/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-dlp` flag.
|
|
|
|
## Prerequisites
|
|
You must have `yt-dlp` installed and available in your `PATH`.
|
|
|
|
## Usage
|
|
|
|
### Simple Download
|
|
```rust
|
|
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
|
|
```rust
|
|
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_only("mp3")
|
|
.download()
|
|
.await?;
|
|
Ok(())
|
|
}
|
|
```
|
|
|
|
### Get Video Metadata
|
|
```rust
|
|
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:
|
|
```rust
|
|
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
|