Struct Transcript

Source
pub struct Transcript {
    pub video_id: String,
    pub url: String,
    pub language: String,
    pub language_code: String,
    pub is_generated: bool,
    pub translation_languages: Vec<TranslationLanguage>,
    pub translation_languages_map: HashMap<String, String>,
}
Expand description

§Transcript

Represents a YouTube transcript that can be fetched or translated.

This struct contains the metadata and access URLs for a transcript but not the actual transcript text content. It serves as a handle to retrieve the full transcript text when needed.

A Transcript object can represent:

  • A native transcript in its original language
  • A translatable transcript that can be converted to other languages
  • A manually created transcript (more accurate, created by humans)
  • An automatically generated transcript (created by YouTube’s speech recognition)

§Usage Example

let api = YouTubeTranscriptApi::new(None, None, None)?;
let transcript_list = api.list_transcripts("dQw4w9WgXcQ").await?;

// Find an English transcript
let transcript = transcript_list.find_transcript(&["en"])?;

// Check if it can be translated
if transcript.is_translatable() {
    // Translate to Spanish
    let spanish = transcript.translate("es")?;
     
    // Fetch the translated content
    let client = reqwest::Client::new();
    let fetched = spanish.fetch(&client, false).await?;
    println!("Spanish transcript: {}", fetched.text());
}

// Or fetch the original transcript
let client = reqwest::Client::new();
let fetched = transcript.fetch(&client, false).await?;
println!("Original transcript: {}", fetched.text());

Fields§

§video_id: String

The YouTube video ID this transcript belongs to

§url: String

URL to fetch the transcript content from YouTube

§language: String

Full human-readable language name (e.g., “English”)

§language_code: String

Language code (e.g., “en”, “en-US”, “es”)

§is_generated: bool

Whether this transcript was automatically generated by YouTube

§translation_languages: Vec<TranslationLanguage>

List of languages this transcript can be translated to

§translation_languages_map: HashMap<String, String>

Mapping of language codes to language names for available translations

Implementations§

Source§

impl Transcript

Source

pub fn new( video_id: String, url: String, language: String, language_code: String, is_generated: bool, translation_languages: Vec<TranslationLanguage>, ) -> Self

Creates a new transcript instance.

This constructor creates a transcript object that can be used to fetch the actual transcript content or to generate translations.

§Parameters
  • video_id - YouTube video ID
  • url - URL to fetch the transcript content
  • language - Human-readable language name (e.g., “English”)
  • language_code - Language code (e.g., “en”, “en-US”)
  • is_generated - Whether this transcript was automatically generated
  • translation_languages - List of languages this transcript can be translated to
§Returns

A new Transcript instance

§Example (internal usage)
// Create a transcript for English
let transcript = Transcript::new(
    "dQw4w9WgXcQ".to_string(),
    "https://www.youtube.com/api/timedtext?...".to_string(),
    "English".to_string(),
    "en".to_string(),
    false, // Not automatically generated
    vec![
        TranslationLanguage {
            language: "Spanish".to_string(),
            language_code: "es".to_string()
        }
    ]
);
Source

pub async fn fetch( &self, client: &Client, preserve_formatting: bool, ) -> Result<FetchedTranscript, CouldNotRetrieveTranscript>

Fetches the actual transcript content from YouTube.

This method retrieves the transcript text and timing information from YouTube using YouTube’s internal InnerTube API, which provides reliable access to transcript data even when YouTube updates their external API requirements.

§Parameters
  • client - HTTP client for making requests to YouTube
  • preserve_formatting - Whether to preserve HTML formatting in the transcript (e.g., bold, italic, etc.)
§Returns
  • Result<FetchedTranscript, CouldNotRetrieveTranscript> - The fetched transcript or an error
§Errors

This method will return an error if:

  • The network request to YouTube fails
  • YouTube returns a non-OK status code
  • The transcript data cannot be parsed
§Example
let client = Client::new();
let api = YouTubeTranscriptApi::new(None, None, None)?;
let transcript_list = api.list_transcripts("dQw4w9WgXcQ").await?;
let transcript = transcript_list.find_transcript(&["en"])?;

// Fetch without preserving formatting
let plain_transcript = transcript.fetch(&client, false).await?;

// Fetch and preserve HTML formatting like <b>bold</b> text
let formatted_transcript = transcript.fetch(&client, true).await?;

// Access the full text
println!("Transcript: {}", plain_transcript.text());

// Or iterate through individual segments
for segment in plain_transcript.parts() {
    println!("[{:.1}s]: {}", segment.start, segment.text);
}
Source

pub fn is_translatable(&self) -> bool

Checks if this transcript can be translated to other languages.

This method determines whether YouTube offers translation capabilities for this transcript. Not all transcripts are translatable.

§Returns
  • bool - true if this transcript can be translated, false otherwise
§Example
let api = YouTubeTranscriptApi::new(None, None, None)?;
let transcript_list = api.list_transcripts("dQw4w9WgXcQ").await?;
let transcript = transcript_list.find_transcript(&["en"])?;

if transcript.is_translatable() {
    println!("This transcript can be translated to other languages");
     
    // Available translation languages
    for lang in &transcript.translation_languages {
        println!("- {} ({})", lang.language, lang.language_code);
    }
} else {
    println!("This transcript cannot be translated");
}
Source

pub fn translate( &self, language_code: &str, ) -> Result<Self, CouldNotRetrieveTranscript>

Creates a translated version of this transcript in the specified language.

This method creates a new Transcript instance representing the same content but translated to the requested language. Note that this doesn’t actually perform the translation yet - the translation happens when you call fetch() on the returned transcript.

§Parameters
  • language_code - The target language code to translate to (e.g., “es”, “fr”, “de”)
§Returns
  • Result<Self, CouldNotRetrieveTranscript> - A new transcript object representing the translation, or an error
§Errors

This method will return an error if:

  • The transcript is not translatable
  • The requested language is not available for translation
§Example
let client = Client::new();
let api = YouTubeTranscriptApi::new(None, None, None)?;
let transcript_list = api.list_transcripts("dQw4w9WgXcQ").await?;
let transcript = transcript_list.find_transcript(&["en"])?;

// Create Spanish translation
if transcript.is_translatable() {
    // Translate to Spanish
    let spanish_transcript = transcript.translate("es")?;
     
    // Fetch the translated content
    let spanish_content = spanish_transcript.fetch(&client, false).await?;
    println!("Spanish translation: {}", spanish_content.text());
}
Source

pub async fn translate_and_fetch( &self, client: &Client, language_code: &str, preserve_formatting: bool, ) -> Result<FetchedTranscript, CouldNotRetrieveTranscript>

Translates this transcript and fetches the result in a single operation.

This convenience method combines the translate and fetch operations.

§Parameters
  • client - HTTP client for making requests to YouTube
  • language_code - The target language code to translate to
  • preserve_formatting - Whether to preserve HTML formatting
§Returns
  • Result<FetchedTranscript, CouldNotRetrieveTranscript> - The fetched translated transcript or an error
§Example
let client = Client::new();
let api = YouTubeTranscriptApi::new(None, None, None)?;
let transcript_list = api.list_transcripts("dQw4w9WgXcQ").await?;
let transcript = transcript_list.find_transcript(&["en"])?;

if transcript.is_translatable() {
    // Translate to Spanish and fetch in one step
    let spanish_content = transcript.translate_and_fetch(&client, "es", false).await?;
    println!("Spanish translation: {}", spanish_content.text());
}
Source

pub fn language(&self) -> &str

Returns the human-readable language name of this transcript.

§Returns
  • &str - The language name (e.g., “English”, “Español”)
Source

pub fn language_code(&self) -> &str

Returns the language code of this transcript.

§Returns
  • &str - The language code (e.g., “en”, “es”, “fr-CA”)
Source

pub fn is_generated(&self) -> bool

Checks if this transcript was automatically generated by YouTube.

§Returns
  • bool - true if automatically generated, false if manually created

Trait Implementations§

Source§

impl Clone for Transcript

Source§

fn clone(&self) -> Transcript

Returns a duplicate of the value. Read more
1.0.0 · Source§

const fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Transcript

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Transcript

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Transcript

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Serialize for Transcript

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

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

Source§

impl<T> MaybeSendSync for T