pub struct VideoDataFetcher {
pub client: Client,
/* private fields */
}Expand description
§VideoDataFetcher
Core component responsible for fetching transcript data and video details from YouTube.
This struct handles the low-level communication with YouTube’s web API to:
- Fetch available transcripts for a video
- Extract caption JSON data from YouTube pages
- Retrieve detailed information about videos, including metadata
The VideoDataFetcher works by parsing YouTube’s HTML and JavaScript variables to extract the necessary data, since YouTube doesn’t provide a public API for transcripts.
§Internal Architecture
This component uses several helper classes to process data:
YoutubePageFetcher: Handles HTTP requests to YouTube, including proxy supportJsVarParser: Extracts JavaScript variables from YouTube’s HTMLPlayabilityAsserter: Verifies video availability and access permissionsVideoDetailsExtractor: Extracts detailed information from video data
Fields§
§client: ClientHTTP client for making requests
Implementations§
Source§impl VideoDataFetcher
impl VideoDataFetcher
Sourcepub fn new(client: Client) -> Self
pub fn new(client: Client) -> Self
Creates a new VideoDataFetcher instance.
§Parameters
client- A configured reqwest HTTP client to use for requestsproxy_config- Optional proxy configuration for routing requests through a proxy
§Returns
A new VideoDataFetcher instance.
§Example (internal usage)
// Create a client
let client = Client::new();
// Create the fetcher
let fetcher = VideoDataFetcher::new(
client
);Sourcepub async fn fetch_transcript_list(
&self,
video_id: &str,
) -> Result<TranscriptList, CouldNotRetrieveTranscript>
pub async fn fetch_transcript_list( &self, video_id: &str, ) -> Result<TranscriptList, CouldNotRetrieveTranscript>
Fetches the list of available transcripts for a YouTube video.
This method:
- Retrieves the video page HTML
- Extracts the captions JSON data
- Builds a TranscriptList from the extracted data
§Parameters
video_id- The YouTube video ID (e.g., “dQw4w9WgXcQ”)
§Returns
Result<TranscriptList, CouldNotRetrieveTranscript>- A TranscriptList on success, or an error if retrieval fails
§Errors
This method can fail if:
- The video doesn’t exist or is private
- The video has no available transcripts
- YouTube’s HTML structure has changed and parsing fails
- Network errors occur during the request
§Example (internal usage)
let api = YouTubeTranscriptApi::new(None, None, None)?;
let video_id = "dQw4w9WgXcQ";
// This internally calls VideoDataFetcher::fetch_transcript_list
let transcript_list = api.list_transcripts(video_id).await?;Sourcepub async fn fetch_video_details(
&self,
video_id: &str,
) -> Result<VideoDetails, CouldNotRetrieveTranscript>
pub async fn fetch_video_details( &self, video_id: &str, ) -> Result<VideoDetails, CouldNotRetrieveTranscript>
Fetches detailed information about a YouTube video.
This method retrieves comprehensive metadata about a video, including:
- Title, author, channel ID
- View count and video length
- Thumbnails in various resolutions
- Keywords and description
§Parameters
video_id- The YouTube video ID
§Returns
Result<VideoDetails, CouldNotRetrieveTranscript>- Video details on success, or an error
§Errors
Similar to transcript fetching, this can fail if:
- The video doesn’t exist or is private
- YouTube’s HTML structure has changed and parsing fails
- Network errors occur during the request
§Example (internal usage)
let api = YouTubeTranscriptApi::new(None, None, None)?;
let video_id = "dQw4w9WgXcQ";
// This internally calls VideoDataFetcher::fetch_video_details
let details = api.fetch_video_details(video_id).await?;
println!("Video title: {}", details.title);
println!("Author: {}", details.author);Sourcepub async fn fetch_microformat(
&self,
video_id: &str,
) -> Result<MicroformatData, CouldNotRetrieveTranscript>
pub async fn fetch_microformat( &self, video_id: &str, ) -> Result<MicroformatData, CouldNotRetrieveTranscript>
Fetches microformat data for a YouTube video.
This method retrieves additional metadata about a video, including:
- Available countries
- Category
- Embed information
- Information about whether the video is unlisted, family-safe, etc.
§Parameters
video_id- The YouTube video ID
§Returns
Result<MicroformatData, CouldNotRetrieveTranscript>- Microformat data on success, or an error
§Errors
This method can fail if:
- The video doesn’t exist or is private
- YouTube’s HTML structure has changed and parsing fails
- Network errors occur during the request
§Example (internal usage)
let api = YouTubeTranscriptApi::new(None, None, None)?;
let video_id = "dQw4w9WgXcQ";
// This internally calls VideoDataFetcher::fetch_microformat
let microformat = api.fetch_microformat(video_id).await?;
if let Some(category) = µformat.category {
println!("Video category: {}", category);
}
if let Some(countries) = µformat.available_countries {
println!("Available in {} countries", countries.len());
}Sourcepub async fn fetch_streaming_data(
&self,
video_id: &str,
) -> Result<StreamingData, CouldNotRetrieveTranscript>
pub async fn fetch_streaming_data( &self, video_id: &str, ) -> Result<StreamingData, CouldNotRetrieveTranscript>
Fetches streaming data for a YouTube video.
This method retrieves information about available video and audio formats, including:
- URLs for different quality versions of the video
- Resolution, bitrate, and codec information
- Both combined formats (with audio and video) and separate adaptive formats
- Information about format expiration
§Parameters
video_id- The YouTube video ID
§Returns
Result<StreamingData, CouldNotRetrieveTranscript>- Streaming data on success, or an error
§Errors
This method can fail if:
- The video doesn’t exist or is private
- The video has geo-restrictions that prevent access
- YouTube’s HTML structure has changed and parsing fails
- Network errors occur during the request
§Example (internal usage)
let api = YouTubeTranscriptApi::new(None, None, None)?;
let video_id = "dQw4w9WgXcQ";
// This internally calls VideoDataFetcher::fetch_streaming_data
let streaming = api.fetch_streaming_data(video_id).await?;
// Print information about available formats
println!("Available formats: {}", streaming.formats.len());
println!("Adaptive formats: {}", streaming.adaptive_formats.len());
println!("Expires in: {} seconds", streaming.expires_in_seconds);
// Find highest quality video format
if let Some(best_format) = streaming.adaptive_formats.iter()
.filter(|f| f.width.is_some() && f.height.is_some())
.max_by_key(|f| f.height.unwrap_or(0)) {
println!("Highest quality: {}p", best_format.height.unwrap());
}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 the video page once and extracts all data, 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 the individual fetch methods separately when multiple types of information are needed, as it avoids multiple HTTP requests.
§Parameters
video_id- The YouTube video ID
§Returns
Result<VideoInfos, CouldNotRetrieveTranscript>- Combined video information on success, or an error
§Errors
This method can fail if:
- The video doesn’t exist or is private
- YouTube’s HTML structure has changed and parsing fails
- Network errors occur during the request
§Example (internal usage)
let api = YouTubeTranscriptApi::new(None, None, None)?;
let video_id = "dQw4w9WgXcQ";
// This internally calls VideoDataFetcher::fetch_video_infos
let infos = api.fetch_video_infos(video_id).await?;
println!("Title: {}", infos.video_details.title);
println!("Category: {}", infos.microformat.category.unwrap_or_default());
println!("Available transcripts: {}", infos.transcript_list.transcripts().count());