pub struct YouTubeTranscriptApi { /* private fields */ }
Expand description
§YouTubeTranscriptApi
The main interface for retrieving YouTube video transcripts and metadata.
This API provides methods to:
- Fetch transcripts from YouTube videos in various languages
- List all available transcript languages for a video
- Retrieve detailed video metadata
The API supports advanced features like:
- Custom HTTP clients and proxies for handling geo-restrictions
- Cookie management for accessing restricted content
- Preserving text formatting in transcripts
§Simple Usage Example
use yt_transcript_rs::api::YouTubeTranscriptApi;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a new API instance with default settings
let api = YouTubeTranscriptApi::new(None, None, None)?;
// Fetch an English transcript
let transcript = api.fetch_transcript(
"dQw4w9WgXcQ", // Video ID
&["en"], // Preferred languages
false // Don't preserve formatting
).await?;
// Print each snippet of the transcript
for snippet in transcript.parts() {
println!("[{:.1}s]: {}", snippet.start, snippet.text);
}
Ok(())
}
Implementations§
Source§impl YouTubeTranscriptApi
impl YouTubeTranscriptApi
Sourcepub fn new(
cookie_path: Option<&Path>,
proxy_config: Option<Box<dyn ProxyConfig + Send + Sync>>,
http_client: Option<Client>,
) -> Result<Self, CookieError>
pub fn new( cookie_path: Option<&Path>, proxy_config: Option<Box<dyn ProxyConfig + Send + Sync>>, http_client: Option<Client>, ) -> Result<Self, CookieError>
Creates a new YouTube Transcript API instance.
This method initializes an API instance with optional customizations for cookies, proxies, and HTTP client settings.
§Parameters
cookie_path
- Optional path to a Netscape-format cookie file for authenticated requestsproxy_config
- Optional proxy configuration for routing requests through a proxy servicehttp_client
- Optional pre-configured HTTP client to use instead of the default one
§Returns
Result<Self, CookieError>
- A new API instance or a cookie-related error
§Errors
This function will return an error if:
- The cookie file exists but cannot be read or parsed
- The cookie file is not in the expected Netscape format
§Examples
§Basic usage with default settings
let api = YouTubeTranscriptApi::new(None, None, None)?;
§Using a cookie file for authenticated access
let cookie_path = Path::new("path/to/cookies.txt");
let api = YouTubeTranscriptApi::new(Some(&cookie_path), None, None)?;
§Using a proxy to bypass geographical restrictions
// Create a proxy configuration
let proxy = GenericProxyConfig::new(
Some("http://proxy.example.com:8080".to_string()),
None
)?;
let api = YouTubeTranscriptApi::new(
None,
Some(Box::new(proxy)),
None
)?;
pub async fn fetch_transcript( &self, video_id: &str, languages: &[&str], preserve_formatting: bool, ) -> Result<FetchedTranscript, CouldNotRetrieveTranscript>
pub async fn list_transcripts( &self, video_id: &str, ) -> Result<TranscriptList, CouldNotRetrieveTranscript>
pub async fn fetch_video_details( &self, video_id: &str, ) -> Result<VideoDetails, CouldNotRetrieveTranscript>
pub async fn fetch_microformat( &self, video_id: &str, ) -> Result<MicroformatData, CouldNotRetrieveTranscript>
pub async fn fetch_streaming_data( &self, video_id: &str, ) -> Result<StreamingData, CouldNotRetrieveTranscript>
Sourcepub async fn fetch_video_infos(
&self,
video_id: &str,
) -> Result<VideoInfos, CouldNotRetrieveTranscript>
pub async fn fetch_video_infos( &self, video_id: &str, ) -> Result<VideoInfos, CouldNotRetrieveTranscript>
Fetches all available information about a YouTube video in a single request.
This method retrieves comprehensive information about a video in one network call, including:
- Video details (title, author, etc.)
- Microformat data (category, available countries, etc.)
- Streaming data (available formats, qualities, etc.)
- Transcript list (available caption languages)
This is more efficient than calling individual methods separately when multiple types of information are needed, as it avoids multiple HTTP requests.
§Parameters
video_id
- The YouTube video ID (e.g., “dQw4w9WgXcQ”)
§Returns
Result<VideoInfos, CouldNotRetrieveTranscript>
- Combined video information on success, or an error
§Errors
This method will return a CouldNotRetrieveTranscript
error if:
- The video doesn’t exist or is private
- The video has geo-restrictions that prevent access
- Network errors occur during the request
§Examples
let api = YouTubeTranscriptApi::new(None, None, None)?;
let video_id = "dQw4w9WgXcQ";
// Fetch all information in a single request
let infos = api.fetch_video_infos(video_id).await?;
// Access combined information
println!("Title: {}", infos.video_details.title);
println!("Author: {}", infos.video_details.author);
if let Some(category) = &infos.microformat.category {
println!("Category: {}", category);
}
println!("Available formats: {}", infos.streaming_data.formats.len());
println!("Available transcripts: {}", infos.transcript_list.transcripts().count());
Trait Implementations§
Source§impl Clone for YouTubeTranscriptApi
impl Clone for YouTubeTranscriptApi
Source§fn clone(&self) -> YouTubeTranscriptApi
fn clone(&self) -> YouTubeTranscriptApi
Returns a duplicate of the value. Read more
1.0.0 · Source§const fn clone_from(&mut self, source: &Self)
const fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreAuto Trait Implementations§
impl Freeze for YouTubeTranscriptApi
impl !RefUnwindSafe for YouTubeTranscriptApi
impl Send for YouTubeTranscriptApi
impl Sync for YouTubeTranscriptApi
impl Unpin for YouTubeTranscriptApi
impl !UnwindSafe for YouTubeTranscriptApi
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more