### Installation Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/quick-start.md Installs the youtube-transcript-api package using pip. ```bash pip install youtube-transcript-api ``` -------------------------------- ### Webshare Complete Example Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/cli-reference.md A comprehensive example demonstrating Webshare proxy setup with language and format options. ```bash youtube_transcript_api VIDEO_ID \ --webshare-proxy-username "proxy-user" \ --webshare-proxy-password "proxy-pass" \ --languages de en \ --format json > transcript.json ``` -------------------------------- ### Fetch Transcript for a Video Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/quick-start.md Fetches the transcript for a given YouTube video ID and prints each snippet with its start time. ```python from youtube_transcript_api import YouTubeTranscriptApi api = YouTubeTranscriptApi() transcript = api.fetch("dQw4w9WgXcQ") # Print all snippets for snippet in transcript: print(f"{snippet.start:.2f}s: {snippet.text}") ``` -------------------------------- ### Save as Plain Text Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/quick-start.md Fetches a transcript and saves it to a plain text file. ```python from youtube_transcript_api import YouTubeTranscriptApi from youtube_transcript_api.formatters import TextFormatter api = YouTubeTranscriptApi() transcript = api.fetch("dQw4w9WgXcQ") formatter = TextFormatter() text_output = formatter.format_transcript(transcript) with open("transcript.txt", "w", encoding="utf-8") as f: f.write(text_output) print("Saved to transcript.txt") ``` -------------------------------- ### Use Proxies for Production Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/quick-start.md Python code showing how to configure and use a proxy for fetching transcripts, specifically using `WebshareProxyConfig`. ```python from youtube_transcript_api.proxies import WebshareProxyConfig proxy_config = WebshareProxyConfig( proxy_username="...", proxy_password="...", ) api = YouTubeTranscriptApi(proxy_config=proxy_config) ``` -------------------------------- ### Work Around IP Blocking (Webshare) Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/quick-start.md Demonstrates how to use Webshare proxies to fetch transcripts, bypassing IP blocks. ```python from youtube_transcript_api import YouTubeTranscriptApi from youtube_transcript_api.proxies import WebshareProxyConfig proxy_config = WebshareProxyConfig( proxy_username="your-webshare-username", proxy_password="your-webshare-password", ) api = YouTubeTranscriptApi(proxy_config=proxy_config) transcript = api.fetch("dQw4w9WgXcQ") print(f"Fetched via proxy: {transcript.language}") ``` -------------------------------- ### Print results Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/quick-start.md Iterates through fetched results and prints the status for each video ID. ```python for video_id, result in results.items(): print(f"{video_id}: {result['status']}") ``` -------------------------------- ### GenericProxyConfig Examples Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/api-reference/ProxyConfig.md Examples of using GenericProxyConfig for different proxy setups. ```python from youtube_transcript_api import YouTubeTranscriptApi from youtube_transcript_api.proxies import GenericProxyConfig # HTTP and HTTPS proxies proxy_config = GenericProxyConfig( http_url="http://user:pass@proxy.example.com:8080", https_url="https://user:pass@proxy.example.com:8080", ) api = YouTubeTranscriptApi(proxy_config=proxy_config) transcript = api.fetch("dQw4w9WgXcQ") # Only HTTP proxy (will be used for both) proxy_config = GenericProxyConfig( http_url="http://user:pass@proxy.example.com:8080" ) # SOCKS proxy proxy_config = GenericProxyConfig( http_url="socks5://user:pass@socks.example.com:1080" ) ``` -------------------------------- ### List Translation Options Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/quick-start.md Lists the languages to which a transcript can be translated. ```python from youtube_transcript_api import YouTubeTranscriptApi api = YouTubeTranscriptApi() transcript_list = api.list("dQw4w9WgXcQ") transcript = transcript_list.find_transcript(["en"]) if transcript.is_translatable: print("Can translate to:") for lang in transcript.translation_languages: print(f" {lang.language_code}: {lang.language}") ``` -------------------------------- ### Get Full Transcript Text Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/quick-start.md Fetches the transcript for a video and joins all text snippets into a single string. ```python from youtube_transcript_api import YouTubeTranscriptApi api = YouTubeTranscriptApi() transcript = api.fetch("dQw4w9WgXcQ") # Join all text together full_text = " ".join(snippet.text for snippet in transcript) print(full_text) ``` -------------------------------- ### Save as JSON Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/quick-start.md Fetches a transcript and saves it to a JSON file with indentation. ```python from youtube_transcript_api import YouTubeTranscriptApi from youtube_transcript_api.formatters import JSONFormatter import json api = YouTubeTranscriptApi() transcript = api.fetch("dQw4w9WgXcQ") formatter = JSONFormatter() json_output = formatter.format_transcript(transcript, indent=2) with open("transcript.json", "w", encoding="utf-8") as f: f.write(json_output) print("Saved to transcript.json") ``` -------------------------------- ### Work Around IP Blocking (Generic Proxy) Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/quick-start.md Shows how to use generic proxy configurations (HTTP/HTTPS) to fetch transcripts. ```python from youtube_transcript_api import YouTubeTranscriptApi from youtube_transcript_api.proxies import GenericProxyConfig proxy_config = GenericProxyConfig( http_url="http://user:pass@proxy.example.com:8080", https_url="https://user:pass@proxy.example.com:8080", ) api = YouTubeTranscriptApi(proxy_config=proxy_config) transcript = api.fetch("dQw4w9WgXcQ") print(f"Fetched via proxy: {transcript.language}") ``` -------------------------------- ### Fetch Method Examples Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/configuration.md Examples of using the fetch method with different configurations. ```python api = YouTubeTranscriptApi() # Default English transcript = api.fetch("dQw4w9WgXcQ") # Multiple languages with fallback transcript = api.fetch("dQw4w9WgXcQ", languages=["de", "en", "fr"]) # Preserve formatting transcript = api.fetch("dQw4w9WgXcQ", preserve_formatting=True) ``` -------------------------------- ### HTTP Proxy Configuration Example Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/cli-reference.md Example of configuring a custom HTTP proxy. ```bash youtube_transcript_api VIDEO_ID --http-proxy "http://proxy.example.com:8080" youtube_transcript_api VIDEO_ID --http-proxy "http://user:pass@proxy.example.com:3128" ``` -------------------------------- ### WebshareProxyConfig Examples Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/configuration.md Examples demonstrating how to set up WebshareProxyConfig with different options. ```python # Basic setup config = WebshareProxyConfig( proxy_username="your-username", proxy_password="your-password", ) ``` ```python # With IP location filtering config = WebshareProxyConfig( proxy_username="your-username", proxy_password="your-password", filter_ip_locations=["de", "us"], # German or US IPs only ) ``` ```python # More aggressive retries config = WebshareProxyConfig( proxy_username="your-username", proxy_password="your-password", retries_when_blocked=20, ) ``` -------------------------------- ### Webshare Proxy Configuration Example Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/cli-reference.md Example of configuring Webshare proxy credentials. ```bash youtube_transcript_api VIDEO_ID \ --webshare-proxy-username "your-username" \ --webshare-proxy-password "your-password" ``` -------------------------------- ### List Available Transcripts Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/quick-start.md Command-line usage to list all available transcript languages for a given video ID. ```bash youtube_transcript_api dQw4w9WgXcQ --list-transcripts ``` -------------------------------- ### Constructor Examples Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/api-reference/YouTubeTranscriptApi.md Examples of initializing the YouTubeTranscriptApi class with different configurations. ```python from youtube_transcript_api import YouTubeTranscriptApi from youtube_transcript_api.proxies import WebshareProxyConfig # Basic usage api = YouTubeTranscriptApi() # With proxy configuration proxy_config = WebshareProxyConfig( proxy_username="your-username", proxy_password="your-password", ) api = YouTubeTranscriptApi(proxy_config=proxy_config) # With custom session from requests import Session session = Session() session.headers.update({"Accept-Encoding": "gzip, deflate"}) api = YouTubeTranscriptApi(http_client=session) ``` -------------------------------- ### Generic Proxy Complete Example Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/cli-reference.md Example combining HTTP and HTTPS proxy configurations. ```bash youtube_transcript_api VIDEO_ID \ --http-proxy "http://user:pass@proxy.example.com:8080" \ --https-proxy "https://user:pass@proxy.example.com:8080" \ --languages en ``` -------------------------------- ### With Proxy Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/quick-start.md Command-line usage to fetch a transcript using a proxy server, providing username and password. ```bash youtube_transcript_api dQw4w9WgXcQ \ --webshare-proxy-username "username" \ --webshare-proxy-password "password" \ --format json ``` -------------------------------- ### Process Multiple Videos Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/quick-start.md Demonstrates how to process multiple video IDs in a batch, handling potential errors for each video and storing the results. ```python from youtube_transcript_api import YouTubeTranscriptApi, YouTubeTranscriptApiException api = YouTubeTranscriptApi() video_ids = [ "dQw4w9WgXcQ", "jNQXAC9IVRw", "invalid_video", ] results = {} for video_id in video_ids: try: transcript = api.fetch(video_id) results[video_id] = { "status": "success", "language": transcript.language, "snippet_count": len(transcript), } except YouTubeTranscriptApiException as e: results[video_id] = { "status": "error", "error": str(e), } ``` -------------------------------- ### HTTPS Proxy Configuration Example Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/cli-reference.md Example of configuring a custom HTTPS proxy. ```bash youtube_transcript_api VIDEO_ID --https-proxy "https://proxy.example.com:8080" ``` -------------------------------- ### GenericProxyConfig Examples Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/configuration.md Examples demonstrating how to configure GenericProxyConfig for different proxy types. ```python # HTTP and HTTPS proxies config = GenericProxyConfig( http_url="http://user:pass@proxy.example.com:8080", https_url="https://user:pass@proxy.example.com:8080", ) # Only HTTP (used for both) config = GenericProxyConfig( http_url="http://proxy.example.com:3128" ) # SOCKS proxy config = GenericProxyConfig( http_url="socks5://user:pass@socks.example.com:1080" ) ``` -------------------------------- ### FormatterLoader Example Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/configuration.md Example demonstrating how to load and use formatters with FormatterLoader. ```python from youtube_transcript_api import YouTubeTranscriptApi from youtube_transcript_api.formatters import FormatterLoader api = YouTubeTranscriptApi() transcript = api.fetch("dQw4w9WgXcQ") loader = FormatterLoader() # Load by type json_formatter = loader.load("json") output = json_formatter.format_transcript(transcript, indent=2) # Default is "pretty" default_formatter = loader.load() ``` -------------------------------- ### Basic Setup (Python) Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/configuration.md Initializes the YouTubeTranscriptApi and fetches a transcript for a given video ID. ```python from youtube_transcript_api import YouTubeTranscriptApi api = YouTubeTranscriptApi() transcript = api.fetch("dQw4w9WgXcQ") ``` -------------------------------- ### Combined Filtering Example (Valid) Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/cli-reference.md Example of valid combined filtering to get specific transcript types. ```bash # Valid: only get manually created German or auto-generated English youtube_transcript_api VIDEO_ID --languages de en --exclude-generated ``` -------------------------------- ### Custom HTTP Session Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/quick-start.md Illustrates how to use a custom `requests.Session` object for making transcript requests, allowing for custom headers and SSL verification. ```python from youtube_transcript_api import YouTubeTranscriptApi from requests import Session # Create custom session with specific headers and certificates session = Session() session.headers.update({ "User-Agent": "My Custom App", "Accept-Language": "de-DE", }) session.verify = "/path/to/ca-bundle.crt" # Custom SSL certificate api = YouTubeTranscriptApi(http_client=session) transcript = api.fetch("dQw4w9WgXcQ") ``` -------------------------------- ### Export to Markdown Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/quick-start.md Fetches a transcript and formats it into a Markdown file, including video ID, language, and content snippets with timestamps. ```python from youtube_transcript_api import YouTubeTranscriptApi api = YouTubeTranscriptApi() transcript = api.fetch("dQw4w9WgXcQ") output = f"# Transcript for {transcript.video_id}\n\n" output += f"**Language:** {transcript.language}\n" output += f"**Generated:** {'Yes' if transcript.is_generated else 'No'}\n\n" output += "## Content\n\n" for snippet in transcript: output += f"**{snippet.start:.2f}s** - {snippet.text}\n\n" with open("transcript.md", "w", encoding="utf-8") as f: f.write(output) print("Saved to transcript.md") ``` -------------------------------- ### Fetch and Save as JSON Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/quick-start.md Command-line usage to fetch a transcript for a given video ID and save it as a JSON file. ```bash youtube_transcript_api dQw4w9WgXcQ --format json > transcript.json ``` -------------------------------- ### Check Translatability Before Translating Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/quick-start.md Python code demonstrating how to check if a transcript is translatable before attempting translation. ```python if transcript.is_translatable: translated = transcript.translate("de") else: print("Cannot translate this transcript") ``` -------------------------------- ### Always Check for Errors Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/quick-start.md Python code demonstrating how to fetch a transcript and handle potential `YouTubeTranscriptApiException`. ```python from youtube_transcript_api import YouTubeTranscriptApi, YouTubeTranscriptApiException api = YouTubeTranscriptApi() try: transcript = api.fetch(video_id) except YouTubeTranscriptApiException as e: print(f"Failed to fetch transcript: {e}") ``` -------------------------------- ### Save as WebVTT Subtitles Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/quick-start.md Fetches a YouTube transcript and saves it to a file in WebVTT format. ```python from youtube_transcript_api import YouTubeTranscriptApi from youtube_transcript_api.formatters import WebVTTFormatter api = YouTubeTranscriptApi() transcript = api.fetch("dQw4w9WgXcQ") formatter = WebVTTFormatter() webvtt_output = formatter.format_transcript(transcript) with open("transcript.vtt", "w", encoding="utf-8") as f: f.write(webvtt_output) print("Saved to transcript.vtt") ``` -------------------------------- ### Graceful Degradation Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/quick-start.md Shows how to implement graceful degradation by trying to fetch a transcript in a primary language and falling back to a secondary language if the primary is not available. ```python from youtube_transcript_api import YouTubeTranscriptApi, NoTranscriptFound api = YouTubeTranscriptApi() # Try primary language, then fallback try: transcript = api.fetch("dQw4w9WgXcQ", languages=["de"]) except NoTranscriptFound: print("German not available, trying English...") try: transcript = api.fetch("dQw4w9WgXcQ", languages=["en"]) except NoTranscriptFound: print("English not available either") transcript = None if transcript: print(f"Successfully fetched: {transcript.language}") ``` -------------------------------- ### WebVTT Output Example Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/cli-reference.md Example output when using the WebVTT format. ```vtt WEBVTT 00:00:00.000 --> 00:00:01.540 Hey there 00:00:01.540 --> 00:00:05.700 how are you 00:00:05.700 --> 00:00:07.200 and welcome ``` -------------------------------- ### TextFormatter Example Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/api-reference/Formatters.md Demonstrates how to use the TextFormatter to get a plain text output of the transcript. ```python from youtube_transcript_api import YouTubeTranscriptApi from youtube_transcript_api.formatters import TextFormatter api = YouTubeTranscriptApi() transcript = api.fetch("dQw4w9WgXcQ") formatter = TextFormatter() text_output = formatter.format_transcript(transcript) print(text_output) ``` -------------------------------- ### Rate Limit Requests Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/quick-start.md Python code demonstrating a strategy for rate limiting requests by introducing a delay between fetching transcripts for multiple videos. ```python import time video_ids = ["id1", "id2", "id3"] api = YouTubeTranscriptApi() for video_id in video_ids: transcript = api.fetch(video_id) # Process... time.sleep(1) # Wait 1 second between requests ``` -------------------------------- ### Translate and Save Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/quick-start.md Command-line usage to fetch a transcript in English, translate it to German, and save it as a JSON file. ```bash youtube_transcript_api dQw4w9WgXcQ \ --languages en \ --translate de \ --format json > transcript_german.json ``` -------------------------------- ### Fetch with Language Selection Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/quick-start.md Command-line usage to fetch transcripts in specified languages (German and English) and save them as an SRT file. ```bash youtube_transcript_api dQw4w9WgXcQ --languages de en --format srt > transcript.srt ``` -------------------------------- ### Save as SRT Subtitles Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/quick-start.md Fetches a transcript and saves it to an SRT file, compatible with video players. ```python from youtube_transcript_api import YouTubeTranscriptApi from youtube_transcript_api.formatters import SRTFormatter api = YouTubeTranscriptApi() transcript = api.fetch("dQw4w9WgXcQ") formatter = SRTFormatter() srt_output = formatter.format_transcript(transcript) with open("transcript.srt", "w", encoding="utf-8") as f: f.write(srt_output) print("Saved to transcript.srt (compatible with video players)") ``` -------------------------------- ### Proxy Configuration Example Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/configuration.md Example of configuring a Webshare residential proxy for the YouTubeTranscriptApi. ```python from youtube_transcript_api import YouTubeTranscriptApi from youtube_transcript_api.proxies import WebshareProxyConfig proxy_config = WebshareProxyConfig( proxy_username="your-username", proxy_password="your-password", ) api = YouTubeTranscriptApi(proxy_config=proxy_config) ``` -------------------------------- ### Text Output Example Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/cli-reference.md Example output when using the text format. ```text Hey there how are you and welcome to my channel ``` -------------------------------- ### Pretty-Print Output Example Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/cli-reference.md Example output when using the pretty-print format. ```python [{'duration': 1.54, 'start': 0.0, 'text': 'Hey there'}, {'duration': 4.16, 'start': 1.54, 'text': 'how are you'}, {'duration': 1.5, 'start': 5.7, 'text': 'and welcome'}] ``` -------------------------------- ### Install project dependencies Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/README.md Installs the project dependencies locally using poetry, including test and development extras. ```shell poetry install --with test,dev ``` -------------------------------- ### Handle Common Errors Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/quick-start.md Demonstrates how to handle common exceptions that can occur when fetching transcripts, such as `VideoUnavailable`, `TranscriptsDisabled`, `NoTranscriptFound`, and `RequestBlocked`. ```python from youtube_transcript_api import ( YouTubeTranscriptApi, VideoUnavailable, TranscriptsDisabled, NoTranscriptFound, RequestBlocked, ) api = YouTubeTranscriptApi() video_id = "dQw4w9WgXcQ" try: transcript = api.fetch(video_id) except VideoUnavailable: print("Error: Video is no longer available") except TranscriptsDisabled: print("Error: Transcripts are disabled for this video") except NoTranscriptFound as e: print(f"Error: Transcript not found in requested language") except RequestBlocked: print("Error: YouTube is blocking requests. Try using proxies.") ``` -------------------------------- ### Fetch with Fallback Languages Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/quick-start.md Fetches a transcript, trying specified languages in order (e.g., German, then English, then French). ```python from youtube_transcript_api import YouTubeTranscriptApi api = YouTubeTranscriptApi() # Try German first, then English, then French transcript = api.fetch("dQw4w9WgXcQ", languages=["de", "en", "fr"]) print(f"Language: {transcript.language}") print(f"Code: {transcript.language_code}") ``` -------------------------------- ### Preserve Formatting When Needed Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/quick-start.md Python code snippet showing how to fetch transcripts while preserving HTML formatting tags like ``, ``, ``. ```python # With HTML formatting preserved transcript = api.fetch(video_id, preserve_formatting=True) # Some snippets may contain , , , etc. ``` -------------------------------- ### Display Installed Version Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/cli-reference.md Command to display the installed version of the youtube-transcript-api CLI. ```bash youtube_transcript_api --version ``` -------------------------------- ### Shell Script Example Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/cli-reference.md A complete shell script example demonstrating how to fetch transcripts in both JSON and SRT formats and save them to a directory. ```bash #!/bin/bash VIDEO_ID="dQw4w9WgXcQ" OUTPUT_DIR="./transcripts" mkdir -p "$OUTPUT_DIR" # Fetch as JSON youtube_transcript_api "$VIDEO_ID" \ --languages de en \ --format json > "$OUTPUT_DIR/$VIDEO_ID.json" # Fetch as SRT youtube_transcript_api "$VIDEO_ID" \ --languages de en \ --format srt > "$OUTPUT_DIR/$VIDEO_ID.srt" echo "Transcripts saved to $OUTPUT_DIR" ``` -------------------------------- ### HTTP Client Configuration Example Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/configuration.md Example of configuring a custom requests.Session object for the YouTubeTranscriptApi. ```python from youtube_transcript_api import YouTubeTranscriptApi from requests import Session # Create custom session session = Session() session.headers.update({ "User-Agent": "Custom User Agent", "Accept-Encoding": "gzip, deflate", }) session.verify = "/path/to/certfile.pem" # Custom CA bundle # Pass to API api = YouTubeTranscriptApi(http_client=session) ``` -------------------------------- ### Using Webshare Proxy Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/cli-reference.md Example of how to use the youtube-transcript-api CLI with Webshare proxy credentials. ```bash youtube_transcript_api dQw4w9WgXcQ \ --webshare-proxy-username "username" \ --webshare-proxy-password "password" \ --languages de en \ --format json ``` -------------------------------- ### Use Language Fallback Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/quick-start.md Python code illustrating how to use the `languages` parameter to try fetching transcripts in multiple languages, falling back if the primary language is not available. ```python # Instead of assuming English exists: transcript = api.fetch(video_id) # May fail # Use fallback: transcript = api.fetch(video_id, languages=["de", "en", "fr"]) # Tries all ``` -------------------------------- ### JSON Output Example Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/cli-reference.md Example output when using the JSON format. ```json [ { "text": "Hey there", "start": 0.0, "duration": 1.54 }, { "text": "how are you", "start": 1.54, "duration": 4.16 } ] ``` -------------------------------- ### Search in Transcript Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/quick-start.md Fetches a transcript and searches for specific text within the transcript snippets, returning the time and text of matches. ```python from youtube_transcript_api import YouTubeTranscriptApi api = YouTubeTranscriptApi() transcript = api.fetch("dQw4w9WgXcQ") search_term = "welcome" matches = [] for snippet in transcript: if search_term.lower() in snippet.text.lower(): matches.append({ "time": snippet.start, "text": snippet.text, }) print(f"Found {len(matches)} matches for '{search_term}':") for match in matches: print(f" {match['time']:.2f}s: {match['text']}") ``` -------------------------------- ### VideoUnavailable Example Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/errors.md Example demonstrating how to catch and handle VideoUnavailable exceptions. ```python from youtube_transcript_api import YouTubeTranscriptApi, VideoUnavailable api = YouTubeTranscriptApi() try: transcript = api.fetch("video_id_that_no_longer_exists") except VideoUnavailable as e: print(f"Video no longer available: {e}") ``` -------------------------------- ### Complete Example: Converting and Saving Transcripts Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/api-reference/Formatters.md A comprehensive example demonstrating how to fetch a transcript and save it in various formats (JSON, text, WebVTT, SRT). ```python from youtube_transcript_api import YouTubeTranscriptApi from youtube_transcript_api.formatters import ( JSONFormatter, TextFormatter, WebVTTFormatter, SRTFormatter, ) api = YouTubeTranscriptApi() transcript = api.fetch("dQw4w9WgXcQ") # Save as JSON with open("transcript.json", "w") as f: f.write(JSONFormatter().format_transcript(transcript, indent=2)) # Save as plain text with open("transcript.txt", "w") as f: f.write(TextFormatter().format_transcript(transcript)) # Save as WebVTT with open("transcript.vtt", "w") as f: f.write(WebVTTFormatter().format_transcript(transcript)) # Save as SRT with open("transcript.srt", "w") as f: f.write(SRTFormatter().format_transcript(transcript)) ``` -------------------------------- ### Save Multiple Videos Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/quick-start.md Fetches transcripts for multiple video IDs, formats them as JSON, and saves them to individual files in a 'transcripts' directory. ```python from youtube_transcript_api import YouTubeTranscriptApi from youtube_transcript_api.formatters import JSONFormatter import json import os api = YouTubeTranscriptApi() video_ids = ["dQw4w9WgXcQ", "jNQXAC9IVRw"] os.makedirs("transcripts", exist_ok=True) formatter = JSONFormatter() for video_id in video_ids: try: transcript = api.fetch(video_id) output = formatter.format_transcript(transcript, indent=2) with open(f"transcripts/{video_id}.json", "w") as f: f.write(output) print(f"✓ Saved {video_id}") except Exception as e: print(f"✗ Failed {video_id}: {e}") ``` -------------------------------- ### SRT Output Example Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/cli-reference.md Example output when using the SRT format. ```srt 1 00:00:00,000 --> 00:00:01,540 Hey there 2 00:00:01,540 --> 00:00:05,700 how are you 3 00:00:05,700 --> 00:00:07,200 and welcome ``` -------------------------------- ### Translate Transcript Example Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/cli-reference.md Example of fetching a transcript in one language and translating it to another. ```bash # Fetch English, translate to German youtube_transcript_api VIDEO_ID --languages en --translate de # Fetch German, translate to Spanish youtube_transcript_api VIDEO_ID --languages de --translate es ``` -------------------------------- ### Search for Specific Language Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/quick-start.md Searches for a transcript in specified languages (e.g., German or English), preferring manual over auto-generated ones, and fetches it. Includes error handling for when no transcript is found. ```python from youtube_transcript_api import YouTubeTranscriptApi, NoTranscriptFound api = YouTubeTranscriptApi() transcript_list = api.list("dQw4w9WgXcQ") try: # Find German or English, prefer manual over auto-generated transcript = transcript_list.find_transcript(["de", "en"]) data = transcript.fetch() print(f"Found: {transcript.language}") except NoTranscriptFound: print("Transcript not available in requested languages") ``` -------------------------------- ### Find Available Transcripts Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/quick-start.md Lists all available transcripts for a given YouTube video ID, including language code, name, and whether they are generated or translatable. ```python from youtube_transcript_api import YouTubeTranscriptApi api = YouTubeTranscriptApi() transcript_list = api.list("dQw4w9WgXcQ") print("Available transcripts:") for transcript in transcript_list: print(f" {transcript.language_code}: {transcript.language}") print(f" Generated: {transcript.is_generated}") print(f" Translatable: {transcript.is_translatable}") ``` -------------------------------- ### CLI Argument: VIDEO_ID Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/configuration.md Example of providing multiple video IDs to the CLI. ```bash youtube_transcript_api dQw4w9WgXcQ jNQXAC9IVRw ``` -------------------------------- ### fetch Method Examples Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/api-reference/YouTubeTranscriptApi.md Examples of using the fetch method to retrieve transcripts with different options. ```python from youtube_transcript_api import YouTubeTranscriptApi api = YouTubeTranscriptApi() # Fetch English transcript transcript = api.fetch("dQw4w9WgXcQ") # Fetch with language fallback transcript = api.fetch("dQw4w9WgXcQ", languages=["de", "en"]) # Iterate over snippets for snippet in transcript: print(f"{snippet.start}s: {snippet.text}") # Preserve HTML formatting transcript = api.fetch("dQw4w9WgXcQ", preserve_formatting=True) # Get raw data as list of dicts raw_data = transcript.to_raw_data() ``` -------------------------------- ### VideoUnplayable Example Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/errors.md Example of how to catch and handle a VideoUnplayable error. ```python from youtube_transcript_api import YouTubeTranscriptApi, VideoUnplayable api = YouTubeTranscriptApi() try: transcript = api.fetch("unplayable_video_id") except VideoUnplayable as e: print(f"Video unplayable: {e}") ``` -------------------------------- ### Default Language (English) Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/cli-reference.md Example of fetching the transcript in the default English language. ```bash youtube_transcript_api VIDEO_ID ``` -------------------------------- ### Combined Filtering Example (Error) Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/cli-reference.md Example showing an error when both exclude flags are used. ```bash # Error: both excluded, nothing to fetch youtube_transcript_api VIDEO_ID --exclude-generated --exclude-manually-created ``` -------------------------------- ### list Method Examples Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/api-reference/YouTubeTranscriptApi.md Examples of using the list method to retrieve available transcript languages for a video. ```python from youtube_transcript_api import YouTubeTranscriptApi api = YouTubeTranscriptApi() transcript_list = api.list("dQw4w9WgXcQ") # Iterate over all available transcripts for transcript in transcript_list: print(f"{transcript.language_code}: {transcript.language} (Generated: {transcript.is_generated})") # Find a specific language (manual transcripts preferred) transcript = transcript_list.find_transcript(["de", "en"]) ``` -------------------------------- ### Fetch Only Manually Created Transcripts Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/cli-reference.md Example command to retrieve only manually created transcripts. ```bash youtube_transcript_api dQw4w9WgXcQ --exclude-generated ``` -------------------------------- ### CLI Option: --format Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/configuration.md Examples of specifying the output format using the --format option, including redirection to files. ```bash youtube_transcript_api VIDEO_ID --format json ``` ```bash youtube_transcript_api VIDEO_ID --format srt > transcript.srt ``` ```bash youtube_transcript_api VIDEO_ID --format webvtt > transcript.vtt ``` -------------------------------- ### Preferred Languages Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/cli-reference.md Example of specifying preferred transcript languages in order. ```bash youtube_transcript_api VIDEO_ID --languages de en fr # Only German youtube_transcript_api VIDEO_ID --languages de ``` -------------------------------- ### Correct Usage of VIDEO_ID Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/cli-reference.md Examples of correct ways to provide video IDs to the command. ```bash youtube_transcript_api dQw4w9WgXcQ youtube_transcript_api dQw4w9WgXcQ jNQXAC9IVRw ``` -------------------------------- ### Translate to Another Language Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/cli-reference.md Example command to fetch transcripts in English and translate them to German. ```bash youtube_transcript_api dQw4w9WgXcQ --languages en --translate de --format json ``` -------------------------------- ### WebVTTFormatter Example Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/api-reference/Formatters.md Shows how to use the WebVTTFormatter to convert a transcript to WebVTT format and save it to a file. ```python from youtube_transcript_api import YouTubeTranscriptApi from youtube_transcript_api.formatters import WebVTTFormatter api = YouTubeTranscriptApi() transcript = api.fetch("dQw4w9WgXcQ") formatter = WebVTTFormatter() webvtt_output = formatter.format_transcript(transcript) # Save to file with open("transcript.vtt", "w") as f: f.write(webvtt_output) ``` -------------------------------- ### Fetch Only Auto-Generated Transcripts Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/cli-reference.md Example command to retrieve only automatically generated transcripts. ```bash youtube_transcript_api dQw4w9WgXcQ --exclude-manually-created ``` -------------------------------- ### NoTranscriptFound Error Example Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/errors.md Demonstrates how to catch the NoTranscriptFound error when a requested language is not available for a video. ```python from youtube_transcript_api import YouTubeTranscriptApi, NoTranscriptFound api = YouTubeTranscriptApi() try: # Request language that doesn't exist for this video transcript = api.fetch("dQw4w9WgXcQ", languages=["ja", "ko"]) except NoTranscriptFound as e: # The error message includes a list of available transcripts print(f"Transcript not found:\n{e}") ``` -------------------------------- ### Save as SRT Subtitles Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/cli-reference.md Example command to save a transcript as an SRT subtitle file. ```bash youtube_transcript_api dQw4w9WgXcQ --format srt > transcript.srt ``` -------------------------------- ### Using Generic HTTP/HTTPS Proxy Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/cli-reference.md Example of how to use the youtube-transcript-api CLI with generic HTTP and HTTPS proxy servers. ```bash youtube_transcript_api dQw4w9WgXcQ \ --http-proxy "http://proxy.example.com:8080" \ --https-proxy "https://proxy.example.com:8080" ``` -------------------------------- ### Proxy Configuration Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/README.md Example of configuring the API to use a proxy. ```python from youtube_transcript_api.proxies import WebshareProxyConfig proxy_config = WebshareProxyConfig( proxy_username="username", proxy_password="password", ) api = YouTubeTranscriptApi(proxy_config=proxy_config) ``` -------------------------------- ### Formatting Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/README.md Examples of using different formatters for transcript output. ```python from youtube_transcript_api.formatters import ( JSONFormatter, SRTFormatter, WebVTTFormatter, TextFormatter, ) formatter = JSONFormatter() output = formatter.format_transcript(transcript, indent=2) ``` -------------------------------- ### Fetch Multiple Videos Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/cli-reference.md Example command to fetch transcripts for multiple videos and save them to a single JSON file. ```bash youtube_transcript_api dQw4w9WgXcQ jNQXAC9IVRw --format json > all_transcripts.json ``` -------------------------------- ### Translate Transcript Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/quick-start.md Fetches an English transcript for a video, checks if it's translatable, and if so, translates it to German and prints the translated snippets. ```python from youtube_transcript_api import YouTubeTranscriptApi, NotTranslatable api = YouTubeTranscriptApi() transcript_list = api.list("dQw4w9WgXcQ") transcript = transcript_list.find_transcript(["en"]) try: if transcript.is_translatable: # Translate to German translated = transcript.translate("de") data = translated.fetch() # Print German transcript for snippet in data: print(f"{snippet.start:.2f}s: {snippet.text}") else: print("This transcript cannot be translated") except NotTranslatable: print("Transcript is not translatable") ``` -------------------------------- ### JSONFormatter Usage Example Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/api-reference/Formatters.md Demonstrates how to use JSONFormatter to get compact, pretty-printed, and sorted JSON output for single or multiple transcripts. ```python from youtube_transcript_api import YouTubeTranscriptApi from youtube_transcript_api.formatters import JSONFormatter api = YouTubeTranscriptApi() transcript = api.fetch("dQw4w9WgXcQ") formatter = JSONFormatter() # Compact JSON compact_json = formatter.format_transcript(transcript) # Pretty-printed JSON pretty_json = formatter.format_transcript(transcript, indent=2) # Sort keys sorted_json = formatter.format_transcript(transcript, indent=2, sort_keys=True) # Multiple transcripts transcripts = [ api.fetch("dQw4w9WgXcQ"), api.fetch("jNQXAC9IVRw"), ] json_output = formatter.format_transcripts(transcripts, indent=2) ``` -------------------------------- ### Save as WebVTT (for video players) Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/cli-reference.md Example command to save a transcript in WebVTT format for use with video players. ```bash youtube_transcript_api dQw4w9WgXcQ --format webvtt > transcript.vtt ``` -------------------------------- ### Basic WebshareProxyConfig setup Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/api-reference/ProxyConfig.md Demonstrates how to set up WebshareProxyConfig with basic credentials and fetch a transcript. ```python from youtube_transcript_api import YouTubeTranscriptApi from youtube_transcript_api.proxies import WebshareProxyConfig # Basic setup with default options proxy_config = WebshareProxyConfig( proxy_username="your-proxy-username", proxy_password="your-proxy-password", ) api = YouTubeTranscriptApi(proxy_config=proxy_config) transcript = api.fetch("dQw4w9WgXcQ") ``` -------------------------------- ### Multi-language Processing Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/configuration.md This example demonstrates fetching transcripts in multiple languages and translating them. It shows how to find a transcript in a preferred language with a fallback and then translate it to another language. ```python from youtube_transcript_api import YouTubeTranscriptApi api = YouTubeTranscriptApi() transcript_list = api.list("dQw4w9WgXcQ") # Get German transcript, fallback to English transcript = transcript_list.find_transcript(["de", "en"]) # Translate to French translated = transcript.translate("fr") # Fetch both german = transcript.fetch() french = translated.fetch() ``` -------------------------------- ### Enable Verbose Output Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/quick-start.md Python code for enabling verbose output during transcript fetching, including printing success messages, language, and snippet count, or detailed error information with a traceback. ```python from youtube_transcript_api import YouTubeTranscriptApi api = YouTubeTranscriptApi() try: transcript = api.fetch("dQw4w9WgXcQ") print(f"Success!") print(f" Language: {transcript.language}") print(f" Snippets: {len(transcript)}") except Exception as e: print(f"Error: {e}") import traceback traceback.print_exc() ``` -------------------------------- ### NotTranslatable Example Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/errors.md Example of how to catch and handle a NotTranslatable error. ```python from youtube_transcript_api import YouTubeTranscriptApi, NotTranslatable api = YouTubeTranscriptApi() transcript_list = api.list("dQw4w9WgXcQ") transcript = transcript_list.find_transcript(["en"]) try: if not transcript.is_translatable: print("This transcript cannot be translated") translated = transcript.translate("de") except NotTranslatable as e: print(f"Not translatable: {e}") ``` -------------------------------- ### YouTubeDataUnparsable Example Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/errors.md Example of how to catch and handle a YouTubeDataUnparsable error. ```python from youtube_transcript_api import YouTubeTranscriptApi, YouTubeDataUnparsable api = YouTubeTranscriptApi() try: transcript = api.fetch("dQw4w9WgXcQ") except YouTubeDataUnparsable as e: print(f"YouTube data unparsable: {e}") print("Please report this issue: https://github.com/jdepoix/youtube-transcript-api/issues") ``` -------------------------------- ### YouTubeRequestFailed Example Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/errors.md Example of how to catch and handle a YouTubeRequestFailed error. ```python from youtube_transcript_api import YouTubeTranscriptApi, YouTubeRequestFailed api = YouTubeTranscriptApi() try: transcript = api.fetch("dQw4w9WgXcQ") except YouTubeRequestFailed as e: print(f"YouTube request failed: {e.reason}") ``` -------------------------------- ### Troubleshooting: Invalid Video ID Solution Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/cli-reference.md Demonstrates the correct way to provide a video ID versus an incorrect URL. ```bash # Wrong youtube_transcript_api https://www.youtube.com/watch?v=dQw4w9WgXcQ # Correct youtube_transcript_api dQw4w9WgXcQ ``` -------------------------------- ### TranscriptsDisabled Example Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/errors.md Example demonstrating how to catch and handle TranscriptsDisabled exceptions. ```python from youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled api = YouTubeTranscriptApi() try: transcript = api.fetch("video_without_captions") except TranscriptsDisabled as e: print(f"Transcripts disabled: {e}") ``` -------------------------------- ### Command Syntax Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/cli-reference.md The basic syntax for using the youtube_transcript_api command-line tool. ```bash youtube_transcript_api VIDEO_ID [VIDEO_ID ...] [OPTIONS] ``` -------------------------------- ### InvalidVideoId Example Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/errors.md Example demonstrating how to catch and handle InvalidVideoId exceptions. ```python from youtube_transcript_api import YouTubeTranscriptApi, InvalidVideoId api = YouTubeTranscriptApi() try: # Wrong: passing full URL transcript = api.fetch("https://www.youtube.com/watch?v=dQw4w9WgXcQ") except InvalidVideoId as e: print(f"Error: {e}") # Correct: passing only the video ID transcript = api.fetch("dQw4w9WgXcQ") ``` -------------------------------- ### Fetch multiple videos (CLI) Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/configuration.md Fetches transcripts for multiple video IDs and saves them to a JSON file. ```bash youtube_transcript_api VIDEO_ID_1 VIDEO_ID_2 VIDEO_ID_3 --format json > all_transcripts.json ``` -------------------------------- ### CLI Option: --languages Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/configuration.md Example of specifying preferred transcript languages using the --languages option. ```bash youtube_transcript_api VIDEO_ID --languages de en fr ``` -------------------------------- ### Fetch German, Fallback to English Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/cli-reference.md Example command to fetch transcripts in German, falling back to English if German is not available. ```bash youtube_transcript_api dQw4w9WgXcQ --languages de en --format text ``` -------------------------------- ### With Proxy Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/INDEX.md Demonstrates how to use the CLI with proxy authentication details (username and password). ```bash youtube_transcript_api VIDEO_ID \ --webshare-proxy-username "username" \ --webshare-proxy-password "password" ``` -------------------------------- ### Fetch with language preference Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/configuration.md Fetches transcripts for a video, prioritizing specified languages in order. ```bash youtube_transcript_api dQw4w9WgXcQ --languages de en ``` -------------------------------- ### Constructor Signature Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/configuration.md The YouTubeTranscriptApi class accepts configuration through its constructor. ```python class YouTubeTranscriptApi: def __init__( self, proxy_config: Optional[ProxyConfig] = None, http_client: Optional[Session] = None, ) ``` -------------------------------- ### Escaping Video IDs Starting with Hyphen Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/cli-reference.md How to escape a video ID if it starts with a hyphen. ```bash youtube_transcript_api "\-abc123" ``` -------------------------------- ### Troubleshooting: Language Not Found Solution Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/cli-reference.md Solution for 'No transcripts were found for any of the requested language codes' error, showing how to list available languages and then fetch a specific one. ```bash youtube_transcript_api VIDEO_ID --list-transcripts # Then use an available language: youtube_transcript_api VIDEO_ID --languages de en ``` -------------------------------- ### Formatter Example: Saving Transcript to JSON Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/README.md Example demonstrating how to retrieve a transcript and save it to a JSON file using JSONFormatter. ```python # your_custom_script.py from youtube_transcript_api import YouTubeTranscriptApi from youtube_transcript_api.formatters import JSONFormatter ytt_api = YouTubeTranscriptApi() video_id = "some_video_id" transcript = ytt_api.fetch(video_id) formatter = JSONFormatter() # .format_transcript(transcript) turns the transcript into a JSON string. json_formatted = formatter.format_transcript(transcript) # Now we can write it out to a file. with open('your_filename.json', 'w', encoding='utf-8') as json_file: json_file.write(json_formatted) ``` -------------------------------- ### Creating Custom Formatters Example Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/api-reference/Formatters.md Example of creating a custom CSVFormatter by extending the base Formatter class and implementing the formatting logic. ```python from youtube_transcript_api.formatters import Formatter from youtube_transcript_api import FetchedTranscript from typing import List class CSVFormatter(Formatter): def format_transcript(self, transcript: FetchedTranscript, **kwargs) -> str: lines = ["text,start,duration"] for snippet in transcript: lines.append(f'"{snippet.text}",{snippet.start},{snippet.duration}') return "\n".join(lines) def format_transcripts(self, transcripts: List[FetchedTranscript], **kwargs) -> str: return "\n\n".join( self.format_transcript(t, **kwargs) for t in transcripts ) # Usage from youtube_transcript_api import YouTubeTranscriptApi api = YouTubeTranscriptApi() transcript = api.fetch("dQw4w9WgXcQ") csv_output = CSVFormatter().format_transcript(transcript) ``` -------------------------------- ### Using Generic Proxy Configuration Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/README.md Example of how to set up any generic HTTP/HTTPS/SOCKS proxy using the GenericProxyConfig class. ```python from youtube_transcript_api import YouTubeTranscriptApi from youtube_transcript_api.proxies import GenericProxyConfig ytt_api = YouTubeTranscriptApi( proxy_config=GenericProxyConfig( http_url="http://user:pass@my-custom-proxy.org:port", https_url="https://user:pass@my-custom-proxy.org:port", ) ) # all requests done by ytt_api will now be proxied using the defined proxy URLs video_id = "some_video_id" ytt_api.fetch(video_id) ``` -------------------------------- ### Display Version Information Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/cli-reference.md Command to display the version of the tool. ```bash youtube_transcript_api --version # Output: youtube_transcript_api, version 1.2.4 ``` -------------------------------- ### Transcript Object Metadata Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/README.md Example of accessing metadata from a Transcript object. ```python print( transcript.video_id, transcript.language, transcript.language_code, # whether it has been manually created or generated by YouTube transcript.is_generated, # whether this transcript can be translated or not transcript.is_translatable, # a list of languages the transcript can be translated to transcript.translation_languages, ) ``` -------------------------------- ### Find Generated Transcript Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/README.md Example of filtering for automatically generated transcripts. ```python transcript = transcript_list.find_generated_transcript(['de', 'en']) ``` -------------------------------- ### Preserve Formatting Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/README.md Example of fetching transcripts while preserving HTML formatting. ```python YouTubeTranscriptApi().fetch(video_ids, languages=['de', 'en'], preserve_formatting=True) ``` -------------------------------- ### Basic transcript fetch Source: https://github.com/jdepoix/youtube-transcript-api/blob/master/_autodocs/configuration.md Fetches the transcript for a given video ID using default settings. ```bash youtube_transcript_api dQw4w9WgXcQ ```