pub struct TranscriptList {
pub video_id: String,
pub manually_created_transcripts: HashMap<String, Transcript>,
pub generated_transcripts: HashMap<String, Transcript>,
pub translation_languages: Vec<TranslationLanguage>,
}
Expand description
§TranscriptList
A collection of available transcripts for a YouTube video.
This struct provides access to all transcripts available for a video, including:
- Manually created transcripts (by the video owner or contributors)
- Automatically generated transcripts (created by YouTube’s speech recognition)
- Available translation languages for translatable transcripts
The TranscriptList
differentiates between manually created and automatically generated
transcripts, as the manually created ones tend to be more accurate. This allows you
to prioritize manually created transcripts over automatically generated ones.
§Usage Example
let api = YouTubeTranscriptApi::new(None, None, None)?;
// Get a list of all available transcripts for a video
let transcript_list = api.list_transcripts("dQw4w9WgXcQ").await?;
// Print all available transcripts
println!("Available transcripts: {}", transcript_list);
// Find a transcript in a specific language (prioritizing English)
let transcript = transcript_list.find_transcript(&["en", "en-US"])?;
// Or specifically find a manually created transcript
let manual_transcript = transcript_list.find_manually_created_transcript(&["en"])?;
// Or retrieve an automatically generated transcript
let auto_transcript = transcript_list.find_generated_transcript(&["en"])?;
Fields§
§video_id: String
The YouTube video ID this transcript list belongs to
manually_created_transcripts: HashMap<String, Transcript>
Map of language codes to manually created transcripts
generated_transcripts: HashMap<String, Transcript>
Map of language codes to automatically generated transcripts
translation_languages: Vec<TranslationLanguage>
List of languages available for translation
Implementations§
Source§impl TranscriptList
impl TranscriptList
Sourcepub fn new(
video_id: String,
manually_created_transcripts: HashMap<String, Transcript>,
generated_transcripts: HashMap<String, Transcript>,
translation_languages: Vec<TranslationLanguage>,
) -> Self
pub fn new( video_id: String, manually_created_transcripts: HashMap<String, Transcript>, generated_transcripts: HashMap<String, Transcript>, translation_languages: Vec<TranslationLanguage>, ) -> Self
Creates a new TranscriptList with the provided components.
§Parameters
video_id
- The YouTube video ID this transcript list belongs tomanually_created_transcripts
- Map of language codes to manually created transcriptsgenerated_transcripts
- Map of language codes to automatically generated transcriptstranslation_languages
- List of languages available for translation
§Returns
A new TranscriptList
instance
Sourcepub fn build(
video_id: String,
video_page_html: &Value,
) -> Result<Self, CouldNotRetrieveTranscript>
pub fn build( video_id: String, video_page_html: &Value, ) -> Result<Self, CouldNotRetrieveTranscript>
Creates a TranscriptList from YouTube’s caption JSON data.
This method parses YouTube’s internal caption data structure to extract:
- Available transcripts (both manual and automatic)
- Their respective language codes and names
- Information about available translation languages
§Parameters
video_id
- The YouTube video IDvideo_page_html
- JSON data extracted from YouTube’s page containing caption information
§Returns
Result<Self, CouldNotRetrieveTranscript>
- A transcript list or an error
§Errors
Returns an error if the caption data cannot be properly parsed.
Sourcepub fn build_without_client(
video_id: String,
video_page_html: &Value,
) -> Result<Self, CouldNotRetrieveTranscript>
pub fn build_without_client( video_id: String, video_page_html: &Value, ) -> Result<Self, CouldNotRetrieveTranscript>
Creates a TranscriptList from YouTube’s caption JSON data without requiring a client.
This method is similar to build
but doesn’t take a client parameter, making it
suitable for use in serialization/deserialization contexts.
§Parameters
video_id
- The YouTube video IDvideo_page_html
- JSON data extracted from YouTube’s page containing caption information
§Returns
Result<Self, CouldNotRetrieveTranscript>
- A transcript list or an error
§Errors
Returns an error if the caption data cannot be properly parsed.
Sourcepub fn find_transcript(
&self,
language_codes: &[&str],
) -> Result<Transcript, CouldNotRetrieveTranscript>
pub fn find_transcript( &self, language_codes: &[&str], ) -> Result<Transcript, CouldNotRetrieveTranscript>
Finds a transcript matching one of the specified language codes.
This method searches for transcripts in the order of priority:
- Manually created transcripts with the specified language codes (in order)
- Automatically generated transcripts with the specified language codes (in order)
§Parameters
language_codes
- Array of language codes to search for, in order of preference
§Returns
Result<Transcript, CouldNotRetrieveTranscript>
- Matching transcript or an error
§Errors
Returns an error if no transcript is found for any of the specified language codes.
§Example
let api = YouTubeTranscriptApi::new(None, None, None)?;
let transcript_list = api.list_transcripts("dQw4w9WgXcQ").await?;
// Try to find English, fall back to Spanish, then auto-generated English
let transcript = transcript_list.find_transcript(&["en", "es", "en-US"])?;
Sourcepub fn find_manually_created_transcript(
&self,
language_codes: &[&str],
) -> Result<Transcript, CouldNotRetrieveTranscript>
pub fn find_manually_created_transcript( &self, language_codes: &[&str], ) -> Result<Transcript, CouldNotRetrieveTranscript>
Finds a manually created transcript matching one of the specified language codes.
This method only searches the manually created transcripts, skipping any automatically generated ones. This is useful when you want to ensure you’re getting a human-created transcript for better accuracy.
§Parameters
language_codes
- Array of language codes to search for, in order of preference
§Returns
Result<Transcript, CouldNotRetrieveTranscript>
- Matching transcript or an error
§Errors
Returns an error if no manually created transcript is found for any of the specified language codes.
§Example
let api = YouTubeTranscriptApi::new(None, None, None)?;
let transcript_list = api.list_transcripts("dQw4w9WgXcQ").await?;
// Only look for manually created transcripts
match transcript_list.find_manually_created_transcript(&["en"]) {
Ok(transcript) => {
println!("Found manual transcript!");
},
Err(_) => {
println!("No manual transcript available, falling back to auto-generated");
let auto_transcript = transcript_list.find_generated_transcript(&["en"])?;
}
}
Sourcepub fn find_generated_transcript(
&self,
language_codes: &[&str],
) -> Result<Transcript, CouldNotRetrieveTranscript>
pub fn find_generated_transcript( &self, language_codes: &[&str], ) -> Result<Transcript, CouldNotRetrieveTranscript>
Finds an automatically generated transcript matching one of the specified language codes.
This method only searches the automatically generated transcripts, skipping any manually created ones. This might be useful in rare cases where you specifically want the auto-generated version.
§Parameters
language_codes
- Array of language codes to search for, in order of preference
§Returns
Result<Transcript, CouldNotRetrieveTranscript>
- Matching transcript or an error
§Errors
Returns an error if no automatically generated transcript is found for any of the specified language codes.
Sourcepub fn transcripts(&self) -> impl Iterator<Item = &Transcript>
pub fn transcripts(&self) -> impl Iterator<Item = &Transcript>
Returns a reference to all available transcripts.
This method provides access to both manually created and automatically generated transcripts as an iterator.
§Returns
An iterator over references to all available transcripts.
§Example
let api = YouTubeTranscriptApi::new(None, None, None)?;
let transcript_list = api.list_transcripts("dQw4w9WgXcQ").await?;
// Print info about all available transcripts
for transcript in transcript_list.transcripts() {
println!("Language: {} ({}), Auto-generated: {}",
transcript.language(),
transcript.language_code(),
transcript.is_generated());
}
Trait Implementations§
Source§impl Clone for TranscriptList
impl Clone for TranscriptList
Source§fn clone(&self) -> TranscriptList
fn clone(&self) -> TranscriptList
1.0.0 · Source§const fn clone_from(&mut self, source: &Self)
const fn clone_from(&mut self, source: &Self)
source
. Read more