中文版 | English
A fast, reliable YouTube subtitle downloader library and CLI tool written in Rust. This project provides both a library (ydl) for programmatic access and a command-line interface (ydl-cli) for downloading YouTube subtitles in various formats.
- 🚀 Fast and efficient subtitle downloading
- 📝 Multiple subtitle formats support (SRT, VTT, TXT, JSON, Raw XML)
- 🌐 Language selection for subtitles
- 📊 Video metadata extraction
- 🎯 Automatic subtitle track selection
- 🔄 Retry logic with exponential backoff for reliability
- 📖 Blog post generation from video transcripts (using OpenAI)
- 📄 Automatic plain text extraction when downloading SRT files
- 🛠️ Both library and CLI interfaces
Add ydl to your Cargo.toml:
[dependencies]
ydl = "0.1.0"Install the CLI tool using cargo:
cargo install ydl-cli# Download subtitles in SRT format (default)
# Note: When downloading SRT, a plain text file is also automatically saved
ydl https://www.youtube.com/watch?v=VIDEO_ID
# Download subtitles in VTT format
ydl https://www.youtube.com/watch?v=VIDEO_ID --format vtt
# Download subtitles in a specific language
ydl https://www.youtube.com/watch?v=VIDEO_ID --language en
# Save to a specific file
ydl https://www.youtube.com/watch?v=VIDEO_ID --output my_subtitles.srt
# Save to a specific directory
ydl https://www.youtube.com/watch?v=VIDEO_ID --output-dir ./subtitles/Note: When downloading SRT format subtitles, a plain text file (
.txt) containing only the subtitle content (without timestamps) is automatically saved alongside the SRT file. This text file is used for blog generation and can be referenced for other purposes.
# List available subtitle tracks
ydl https://www.youtube.com/watch?v=VIDEO_ID --list
# Show video metadata
ydl https://www.youtube.com/watch?v=VIDEO_ID --info
# Generate a blog post from video transcript (requires OpenAI API key)
# This will use the existing .txt file if available, or download fresh subtitles
ydl https://www.youtube.com/watch?v=VIDEO_ID --generate-blog
# Enable verbose logging
ydl https://www.youtube.com/watch?v=VIDEO_ID -vuse ydl::{Ydl, YdlOptions, SubtitleType};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create options
let options = YdlOptions::builder()
.language("en")
.subtitle_type(SubtitleType::Srt)
.build();
// Create downloader
let downloader = Ydl::new("https://www.youtube.com/watch?v=VIDEO_ID", options)?;
// Download subtitles
let subtitles = downloader.download().await?;
// Process subtitles
println!("Downloaded {} subtitle entries", subtitles.entries.len());
for entry in &subtitles.entries {
println!("{} --> {}: {}", entry.start, entry.end, entry.text);
}
// Save to file
downloader.download_to_file("output.srt").await?;
Ok(())
}- SRT - SubRip subtitle format
- VTT - WebVTT format
- TXT - Plain text format
- JSON - Structured JSON format
- Raw - Original XML format from YouTube
OPENAI_API_KEY- Required for blog generation feature
ydl/- Core library for YouTube subtitle downloadingydl-cli/- Command-line interfaceexamples/- Example usage code
This project is distributed under the terms of MIT.
See LICENSE for details.
Copyright 2025 Tyr Chen