ytranscript
ytranscript
is a Rust crate that provides functionality to fetch YouTube video transcripts. It supports fetching transcripts in different languages and handles various error scenarios that might occur while retrieving the transcripts.
Features
- Extracts YouTube video IDs from URLs or strings.
- Fetches transcripts for YouTube videos.
- Supports fetching transcripts in specific languages.
- Handles common errors such as video unavailability, transcript unavailability, and too many requests.
Installation
Add ytranscript
to your Cargo.toml
:
Usage
Here is an example of how to use the ytranscript
crate in a binary crate:
use YoutubeTranscript;
use env;
async
Functionality
YoutubeTranscript::fetch_transcript
Fetches the transcript for a given YouTube video ID or URL.
-
Arguments:
video_id
: A string slice representing the YouTube video URL or ID.config
: An optionalTranscriptConfig
specifying the desired language for the transcript.
-
Returns:
Ok(Vec<TranscriptResponse>)
: A vector ofTranscriptResponse
if the transcript is successfully fetched.Err(YoutubeTranscriptError)
: An error if the transcript cannot be fetched.
Error Handling
The crate defines a set of errors that might occur while fetching transcripts:
use Error;
Regex Patterns
The crate uses regex patterns to extract YouTube video IDs and parse XML transcripts:
pub const RE_YOUTUBE: &str =
r#"(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([^"&?\/\s]{11})"#;
pub const USER_AGENT: &str = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36,gzip(gfe)";
pub const RE_XML_TRANSCRIPT: &str = r#"<text start="([^"]*)" dur="([^"]*)">([^<]*)<\/text>"#;
Types
The crate defines the following types:
Testing
You can test the functionality of the ytranscript
crate by running the following command:
License
This project is licensed under the MIT License. See the LICENSE file for details