Struct Transcript

Source
pub struct Transcript {
    pub client: Client,
    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 fetched = spanish.fetch(false).await?;
    println!("Spanish transcript: {}", fetched.text());
}

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

Fields§

§client: Client

HTTP client for making requests to YouTube

§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( client: Client, 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
  • client - HTTP client for making requests to YouTube
  • 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)
let client = Client::new();

// Create a transcript for English
let transcript = Transcript::new(
    client,
    "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, preserve_formatting: bool, ) -> Result<FetchedTranscript, CouldNotRetrieveTranscript>

Fetches the actual transcript content from YouTube.

This method retrieves the transcript text and timing information from YouTube and returns it as a structured FetchedTranscript object.

§Parameters
  • 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 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(false).await?;

// Fetch and preserve HTML formatting like <b>bold</b> text
let formatted_transcript = transcript.fetch(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 language code to translate to (e.g., “es” for Spanish)
§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 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() {
    let spanish = transcript.translate("es")?;
     
    // Now fetch the Spanish translation
    let spanish_content = spanish.fetch(false).await?;
    println!("Spanish: {}", spanish_content.text());
     
    // Create Japanese translation
    let japanese = transcript.translate("ja")?;
    let japanese_content = japanese.fetch(false).await?;
    println!("Japanese: {}", japanese_content.text());
}
Source

pub fn language(&self) -> &str

Returns the full 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 copy of the value. Read more
1.0.0 · Source§

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 Display for Transcript

Source§

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

Formats the value using the given formatter. 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> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T