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
impl Transcript
Sourcepub fn new(
client: Client,
video_id: String,
url: String,
language: String,
language_code: String,
is_generated: bool,
translation_languages: Vec<TranslationLanguage>,
) -> Self
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 YouTubevideo_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)
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()
}
]
);
Sourcepub async fn fetch(
&self,
preserve_formatting: bool,
) -> Result<FetchedTranscript, CouldNotRetrieveTranscript>
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);
}
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 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());
}
Sourcepub fn language(&self) -> &str
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”)
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§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more