π₯ How to get it
Add the following to your Cargo.toml file:
[dependencies]
yt-dlp = "latest version of the crate"
π Examples
use yt_dlp::Youtube;
use std::path::PathBuf;
#[tokio::main]
pub async fn main() {
let destination = PathBuf::from("bin");
let output_dir = PathBuf::from("output");
let url = "https://www.youtube.com/watch?v=dQw4w9WgXcQ".to_string();
let fetcher = Youtube::new_with_binaries(destination, url, output_dir).await.expect("Failed to install binaries");
}
- π¦ Installing the yt-dlp binary only:
use yt_dlp::Youtube;
use std::path::PathBuf;
#[tokio::main]
pub async fn main() {
let destination = PathBuf::from("bin");
Youtube::install_youtube(destination).await.expect("Failed to install yt-dlp");
}
- π¦ Installing the ffmpeg binary only:
use yt_dlp::Youtube;
use std::path::PathBuf;
#[tokio::main]
pub async fn main() {
let destination = PathBuf::from("bin");
Youtube::install_ffmpeg(destination).await.expect("Failed to install ffmpeg");
}
- π Updating the yt-dlp binary:
use yt_dlp::Youtube;
use std::path::PathBuf;
#[tokio::main]
pub async fn main() {
let ffmpeg = PathBuf::from("ffmpeg");
let executable = PathBuf::from("yt-dlp");
let output_dir = PathBuf::from("output");
let url = "https://www.youtube.com/watch?v=dQw4w9WgXcQ".to_string();
let mut fetcher = Youtube::new(executable, ffmpeg, url, output_dir).expect("Failed to create fetcher");
fetcher.update_downloader().await.expect("Failed to update yt-dlp");
}
- π₯ Fetching a video (with its audio) and downloading it:
use yt_dlp::Youtube;
use std::path::PathBuf;
#[tokio::main]
pub async fn main() {
let ffmpeg = PathBuf::from("ffmpeg");
let executable = PathBuf::from("yt-dlp");
let output_dir = PathBuf::from("output");
let url = "https://www.youtube.com/watch?v=dQw4w9WgXcQ".to_string();
let mut fetcher = Youtube::new(executable, ffmpeg, url, output_dir).expect("Failed to create fetcher");
let video = fetcher.fetch_infos().await.expect("Failed to fetch video infos");
println!("Video title: {}", video.title);
fetcher.download_video("video.mp4").await.expect("Failed to download video");
}
- π¬ Fetching a video (without its audio) and downloading it:
use yt_dlp::Youtube;
use std::path::PathBuf;
#[tokio::main]
pub async fn main() {
let ffmpeg = PathBuf::from("ffmpeg");
let executable = PathBuf::from("yt-dlp");
let output_dir = PathBuf::from("output");
let url = "https://www.youtube.com/watch?v=dQw4w9WgXcQ".to_string();
let mut fetcher = Youtube::new(executable, ffmpeg, url, output_dir).expect("Failed to create fetcher");
let video = fetcher.fetch_infos().await.expect("Failed to fetch video infos");
println!("Video title: {}", video.title);
fetcher.download_video_stream("video.mp4").await.expect("Failed to download video without audio");
}
- π΅ Fetching an audio and downloading it:
use yt_dlp::Youtube;
use std::path::PathBuf;
#[tokio::main]
pub async fn main() {
let ffmpeg = PathBuf::from("ffmpeg");
let executable = PathBuf::from("yt-dlp");
let output_dir = PathBuf::from("output");
let url = "https://www.youtube.com/watch?v=dQw4w9WgXcQ".to_string();
let mut fetcher = Youtube::new(executable, ffmpeg, url, output_dir).expect("Failed to create fetcher");
let audio = fetcher.fetch_audio_infos().await.expect("Failed to fetch audio infos");
println!("Audio title: {}", audio.title);
fetcher.download_audio_stream("audio.mp3").await.expect("Failed to download audio");
}
- π Fetching a specific format and downloading it:
use yt_dlp::Youtube;
use std::path::PathBuf;
#[tokio::main]
pub async fn main() {
let ffmpeg = PathBuf::from("ffmpeg");
let executable = PathBuf::from("yt-dlp");
let output_dir = PathBuf::from("output");
let url = "https://www.youtube.com/watch?v=dQw4w9WgXcQ".to_string();
let mut fetcher = Youtube::new(executable, ffmpeg, url, output_dir).expect("Failed to create fetcher");
let video = fetcher.fetch_infos().await.expect("Failed to fetch video infos");
println!("Video title: {}", video.title);
let format = video.best_video_format().unwrap(); fetcher.download_format(&format, "video.mp4").await.expect("Failed to download video format");
}
- βοΈ Combining an audio and a video file into a single file:
use yt_dlp::Youtube;
use std::path::PathBuf;
#[tokio::main]
pub async fn main() {
let ffmpeg = PathBuf::from("ffmpeg");
let executable = PathBuf::from("yt-dlp");
let output_dir = PathBuf::from("output");
let url = "https://www.youtube.com/watch?v=dQw4w9WgXcQ".to_string();
let mut fetcher = Youtube::new(executable, ffmpeg, url, output_dir).expect("Failed to create fetcher");
let video = fetcher.fetch_infos().await.expect("Failed to fetch video infos");
println!("Video title: {}", video.title);
fetcher.download_video_stream("video.mp4").await.expect("Failed to download video");
fetcher.download_audio_stream("audio.mp3").await.expect("Failed to download audio");
fetcher.combine_audio_and_video("video.mp4", "audio.mp3", "output.mp4").await.expect("Failed to combine audio and video");
}
- πΈ Fetching a thumbnail and downloading it:
use yt_dlp::Youtube;
use std::path::PathBuf;
#[tokio::main]
pub async fn main() {
let ffmpeg = PathBuf::from("ffmpeg");
let executable = PathBuf::from("yt-dlp");
let output_dir = PathBuf::from("output");
let url = "https://www.youtube.com/watch?v=dQw4w9WgXcQ".to_string();
let mut fetcher = Youtube::new(executable, ffmpeg, url, output_dir).expect("Failed to create fetcher");
let video = fetcher.fetch_infos().await.expect("Failed to fetch video infos");
println!("Video title: {}", video.title);
fetcher.download_thumbnail("thumbnail.jpg").await.expect("Failed to download thumbnail");
}
π‘Support coming soon