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
impl Transcript
Sourcepub fn new(
video_id: String,
url: String,
language: String,
language_code: String,
is_generated: bool,
translation_languages: Vec<TranslationLanguage>,
) -> Self
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 IDurl
- URL to fetch the transcript contentlanguage
- Human-readable language name (e.g., “English”)language_code
- Language code (e.g., “en”, “en-US”)is_generated
- Whether this transcript was automatically generatedtranslation_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()
}
]
);
Sourcepub async fn fetch(
&self,
client: &Client,
preserve_formatting: bool,
) -> Result<FetchedTranscript, CouldNotRetrieveTranscript>
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 YouTubepreserve_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);
}
Sourcepub fn is_translatable(&self) -> bool
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");
}
Sourcepub fn translate(
&self,
language_code: &str,
) -> Result<Self, CouldNotRetrieveTranscript>
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());
}
Sourcepub async fn translate_and_fetch(
&self,
client: &Client,
language_code: &str,
preserve_formatting: bool,
) -> Result<FetchedTranscript, CouldNotRetrieveTranscript>
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 YouTubelanguage_code
- The target language code to translate topreserve_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());
}
Sourcepub fn language(&self) -> &str
pub fn language(&self) -> &str
Returns the human-readable language name of this transcript.
§Returns
&str
- The language name (e.g., “English”, “Español”)
Sourcepub fn language_code(&self) -> &str
pub fn language_code(&self) -> &str
Returns the language code of this transcript.
§Returns
&str
- The language code (e.g., “en”, “es”, “fr-CA”)
Sourcepub fn is_generated(&self) -> bool
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
impl Clone for Transcript
Source§fn clone(&self) -> Transcript
fn clone(&self) -> Transcript
1.0.0 · Source§const fn clone_from(&mut self, source: &Self)
const fn clone_from(&mut self, source: &Self)
source
. Read more