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>
Trait Implementations§
Source§impl Clone for YouTubeTranscriptApi
impl Clone for YouTubeTranscriptApi
Source§fn clone(&self) -> YouTubeTranscriptApi
fn clone(&self) -> YouTubeTranscriptApi
Returns a copy of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
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