VideoDataFetcher

Struct VideoDataFetcher 

Source
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 support
  • JsVarParser: Extracts JavaScript variables from YouTube’s HTML
  • PlayabilityAsserter: Verifies video availability and access permissions
  • VideoDetailsExtractor: Extracts detailed information from video data

Fields§

§client: Client

HTTP client for making requests

Implementations§

Source§

impl VideoDataFetcher

Source

pub fn new(client: Client) -> Self

Creates a new VideoDataFetcher instance.

§Parameters
  • client - A configured reqwest HTTP client to use for requests
  • proxy_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
);
Source

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:

  1. Retrieves the video page HTML
  2. Extracts the captions JSON data
  3. 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?;
Source

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);
Source

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) = &microformat.category {
    println!("Video category: {}", category);
}

if let Some(countries) = &microformat.available_countries {
    println!("Available in {} countries", countries.len());
}
Source

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());
}
Source

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());

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T