From 27397e60ffb54fea73f26b01272172eb308b862b Mon Sep 17 00:00:00 2001 From: stephen schuresko Date: Tue, 7 Apr 2026 11:33:05 -0400 Subject: [PATCH 1/8] Add download_captions function to download YouTube captions with English preference --- youtube_transcript_downloader.py | 65 +++++++++++++++++++++++++++----- 1 file changed, 55 insertions(+), 10 deletions(-) diff --git a/youtube_transcript_downloader.py b/youtube_transcript_downloader.py index 74b94d8..31c9ed8 100644 --- a/youtube_transcript_downloader.py +++ b/youtube_transcript_downloader.py @@ -122,15 +122,10 @@ def main(): print(f"Video ID: {video_id}") print("Fetching transcript...") - # Try to get English transcript first - transcript = get_transcript(video_id, ['en', 'en-US', 'en-GB']) + # Download captions with preference for English + result = download_captions(video_id) - if transcript is None: - # Try to get transcript in any language - print("\nNo English transcript found. Trying to get transcript in original language...") - transcript = get_transcript(video_id, []) - - if transcript is None: + if not result['success']: print("\nCould not retrieve transcript.") print("Possible reasons:") print("- Video does not have captions/subtitles") @@ -139,14 +134,15 @@ def main(): sys.exit(1) # Format original transcript - original_formatted = format_transcript(transcript, include_timestamps=True) + original_formatted = format_transcript(result['transcript'], include_timestamps=True) # Check if translation is needed (try to detect if it's already English) - original_text = get_transcript_text(transcript) + original_text = get_transcript_text(result['transcript']) # Ask user if they want to translate print("\n" + "=" * 45) print("Transcript successfully retrieved!") + print(f"Language: {result['language']}") # Determine if translation is needed # If transcript is in English, no translation needed @@ -236,5 +232,54 @@ def is_english(text): return matches > len(words) * 0.05 if words else False +def download_captions(video_id): + """ + Download closed captions from a YouTube video, preferring English. + + Args: + video_id: The YouTube video ID + + Returns: + Dictionary with 'transcript', 'language', and 'success' keys, + or None if no transcript is available + """ + try: + # First, try to get English transcript (preferably en, en-US, en-GB) + english_languages = ['en', 'en-US', 'en-GB'] + transcript = YouTubeTranscriptApi().fetch(video_id, languages=english_languages) + + if transcript: + return { + 'transcript': transcript, + 'language': transcript.language, + 'success': True + } + + # If no English transcript is available, try any language + print("No English transcript found. Trying to get transcript in original language...") + transcript = YouTubeTranscriptApi().fetch(video_id, languages=[]) + + if transcript: + return { + 'transcript': transcript, + 'language': transcript.language, + 'success': True + } + + return { + 'transcript': None, + 'language': None, + 'success': False + } + + except Exception as e: + print(f"Error downloading captions: {e}") + return { + 'transcript': None, + 'language': None, + 'success': False + } + + if __name__ == "__main__": main() \ No newline at end of file From 3b909133304c6e3c67f07b6f6398e8051a2fa673 Mon Sep 17 00:00:00 2001 From: stephen schuresko Date: Tue, 7 Apr 2026 11:40:39 -0400 Subject: [PATCH 2/8] Add download_captions function with English preference and translation capabilities --- ...tube_transcript_downloader.cpython-312.pyc | Bin 0 -> 10300 bytes youtube_transcript_downloader_backup.py | 285 ++++++++++++++++++ 2 files changed, 285 insertions(+) create mode 100644 __pycache__/youtube_transcript_downloader.cpython-312.pyc create mode 100644 youtube_transcript_downloader_backup.py diff --git a/__pycache__/youtube_transcript_downloader.cpython-312.pyc b/__pycache__/youtube_transcript_downloader.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..495065788be6df5ffd5098e51c13f7153a7023ec GIT binary patch literal 10300 zcmd5iZEPDycDvl=H${;msYs3@uO!D3Es3)1_)Gpe#g-h$j-15GbAG3i%#Ch+ux=y zx_AdXxp_bDgeMPQ$GhOahId1&m-j$f%hy2ZrjlZe7bGs0;`N?nOc5oQYj`{%b7@H&lVVdG zUmhoOG0p%po!|wLQ>wra0(k@og3KipP7%3*)L1ehk4IdeBSJ%AS__r|RMF^9C!oD% zSh%bViqcd}Q3MGZe5XWlEGdw21gSV(UKIXW;b!1oPeTC|2WpR5X6QV9lDgDQQ8N~$ zGJ3+;9T;g`c}t$UQEs1M@{CMhZOStf2zArNGdIeR;ue}ZPq9>v4O`Vc1D9d3ab*)d zPM_pPC2@+Ij!6kIBXe)OewG_0i;^R1bI0+%>7)6#dcqyw-raquzx!gWTa9*K`u*PC znQ&y6+ME+JO6IM|i11d=79&1KJch!IW|0KVCJQksKCW5EBr%f?vl^3;lA0q;sF#v8 zd%31rfuQk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>Pf6%A}dj}kDX_?=SvOu4~S*%{MqdpHeZ(~u#=bQBp4 zc^!19Bus9rnASh_qJ%Nq;c~)9>ciec*!#**#Sb(27W%gV5lEWm%Dx&VR;yT}<7k-Y zRi7~u85&fZ^e+YsHvwNab^*Q_u##^EBn<27NMM+iBqKpqiA&%wI}nw`h)g_aJ?mJO zSv~0}!J$c!;=iCGe+&g?8r!U{UpKt~DUgjH2NVb2Bwq)7Yt99!m3z${D@{A^HuWqu z^{hO*{R=xA^sZ5CjrW0OcTKLxd5iq z_2mN(3%nR$ep*9A{igvdl$s+NMNcLg4b$-H7TNtUJsb{b_9%Sv6OTrv<47zN zKCK=O9tFNHGksbRjinR%*BmWdcSa1qOp^7_6Ciyhq$}PSoc^fEh@(ip+#nww5a&;K z4>7q#+$>$u;?5z?so0bNxcwvcVzNX(HXKK$L_U)gj!8d*HdKUi6pA&5rs*$OmS)#I z6z%>c<@}U7@F{ipQ>yE}%}x8(n*wz3eiuXUUHACu;ChFL-h02^6eH{{o&^B%%NS literal 0 HcmV?d00001 diff --git a/youtube_transcript_downloader_backup.py b/youtube_transcript_downloader_backup.py new file mode 100644 index 0000000..31c9ed8 --- /dev/null +++ b/youtube_transcript_downloader_backup.py @@ -0,0 +1,285 @@ +#!/usr/bin/env python3 +""" +YouTube Transcript Downloader and Translator + +This program downloads a YouTube video transcript and translates it to English. +""" + +import sys +import re +from youtube_transcript_api import YouTubeTranscriptApi +from youtube_transcript_api.formatters import TextFormatter +from deep_translator import GoogleTranslator + + +def extract_video_id(url): + """Extract YouTube video ID from various URL formats.""" + patterns = [ + r'(?:v=|\/)([0-9A-Za-z_-]{11}).*', + r'youtu\.be\/([0-9A-Za-z_-]{11})', + ] + + for pattern in patterns: + match = re.search(pattern, url) + if match: + return match.group(1) + return None + + +def get_transcript(video_id, languages=None): + """ + Get transcript for a YouTube video. + + Args: + video_id: The YouTube video ID + languages: List of language codes to try (default: ['en', 'en-US']) + + Returns: + Transcript object or None if not found + """ + if languages is None: + languages = ['en', 'en-US'] + + try: + transcript = YouTubeTranscriptApi().fetch(video_id, languages=languages) + return transcript + except Exception as e: + print(f"Error getting transcript: {e}") + return None + + +def get_transcript_text(transcript): + """Extract plain text from transcript.""" + formatter = TextFormatter() + return formatter.format_transcript(transcript) + + +def translate_text(text, target_language='en'): + """ + Translate text to target language using Google Translate. + + Args: + text: The text to translate + target_language: Target language code (default: 'en') + + Returns: + Translated text or None if translation fails + """ + try: + translator = GoogleTranslator(source='auto', target=target_language) + translated = translator.translate(text) + return translated + except Exception as e: + print(f"Error during translation: {e}") + return None + + +def format_transcript(transcript, include_timestamps=True): + """ + Format transcript into readable text. + + Args: + transcript: The transcript list of dictionaries + include_timestamps: Whether to include timestamps + + Returns: + Formatted string + """ + formatter = TextFormatter() + return formatter.format_transcript(transcript) + + +def detect_language(transcript): + """Attempt to detect the language of the transcript.""" + if not transcript: + return "unknown" + + # Get first few words to help identify language + if isinstance(transcript, list) and len(transcript) > 0: + text = transcript[0].get('text', '') + return text[:100] + return "" + + +def main(): + """Main function to run the transcript downloader.""" + print("YouTube Transcript Downloader and Translator") + print("=" * 45) + + # Get YouTube URL from command line argument or prompt user + if len(sys.argv) > 1: + url = sys.argv[1] + else: + url = input("Enter YouTube URL: ").strip() + + # Extract video ID + video_id = extract_video_id(url) + if not video_id: + print("Error: Could not extract video ID from URL") + print("Please enter a valid YouTube URL") + sys.exit(1) + + print(f"Video ID: {video_id}") + print("Fetching transcript...") + + # Download captions with preference for English + result = download_captions(video_id) + + if not result['success']: + print("\nCould not retrieve transcript.") + print("Possible reasons:") + print("- Video does not have captions/subtitles") + print("- Video is live stream") + print("- Video is private") + sys.exit(1) + + # Format original transcript + original_formatted = format_transcript(result['transcript'], include_timestamps=True) + + # Check if translation is needed (try to detect if it's already English) + original_text = get_transcript_text(result['transcript']) + + # Ask user if they want to translate + print("\n" + "=" * 45) + print("Transcript successfully retrieved!") + print(f"Language: {result['language']}") + + # Determine if translation is needed + # If transcript is in English, no translation needed + needs_translation = not is_english(original_text) + + if needs_translation: + print("\nNote: The transcript appears to be in a non-English language.") + print("Would you like to translate it to English?") + response = input("Enter 'y' for yes or 'n' for no: ").strip().lower() + + if response == 'y': + print("\nTranslating to English...") + translated = translate_text(original_text, 'en') + + if translated: + # Save translated transcript + output_file = f"transcript_{video_id}_translated.txt" + with open(output_file, 'w', encoding='utf-8') as f: + f.write("TRANSLATED TRANSCRIPT\n") + f.write("=" * 45 + "\n\n") + f.write(translated) + + print(f"\nTranslated transcript saved to: {output_file}") + + # Save both original and translated + original_file = f"transcript_{video_id}_original.txt" + with open(original_file, 'w', encoding='utf-8') as f: + f.write(original_formatted) + + print(f"Original transcript saved to: {original_file}") + + # Display original and translated + print("\n" + "=" * 45) + print("ORIGINAL TRANSCRIPT:") + print("=" * 45) + print(original_formatted) + + print("\n" + "=" * 45) + print("TRANSLATED TRANSCRIPT:") + print("=" * 45) + print(translated) + else: + print("\nTranslation failed. Saving original transcript...") + output_file = f"transcript_{video_id}.txt" + with open(output_file, 'w', encoding='utf-8') as f: + f.write(original_formatted) + print(f"Transcript saved to: {output_file}") + else: + output_file = f"transcript_{video_id}.txt" + with open(output_file, 'w', encoding='utf-8') as f: + f.write(original_formatted) + print(f"\nTranscript saved to: {output_file}") + else: + # Already in English, just save it + output_file = f"transcript_{video_id}.txt" + with open(output_file, 'w', encoding='utf-8') as f: + f.write(original_formatted) + print(f"\nTranscript saved to: {output_file}") + print("\n" + "=" * 45) + print("TRANSCRIPT:") + print("=" * 45) + print(original_formatted) + + +def is_english(text): + """Simple check to determine if text is likely English.""" + # Common English words to check against + common_english_words = { + 'the', 'be', 'to', 'of', 'and', 'a', 'in', 'that', 'have', 'i', + 'it', 'for', 'not', 'on', 'with', 'he', 'as', 'you', 'do', 'at', + 'this', 'but', 'his', 'by', 'from', 'they', 'we', 'say', 'her', + 'she', 'or', 'an', 'will', 'my', 'one', 'all', 'would', 'there', + 'their', 'what', 'so', 'up', 'out', 'if', 'about', 'who', 'get', + 'which', 'go', 'me', 'when', 'make', 'can', 'like', 'time', 'no', + 'just', 'him', 'know', 'take', 'people', 'into', 'year', 'your', + 'good', 'some', 'could', 'them', 'see', 'other', 'than', 'then', + 'now', 'look', 'only', 'come', 'its', 'over', 'think', 'also', + 'back', 'after', 'use', 'two', 'how', 'our', 'work', 'first', 'well', + 'way', 'even', 'new', 'want', 'because', 'any', 'these', 'give', 'day' + } + + # Simple heuristic: count common English words + words = set(text.lower().split()) + matches = sum(1 for word in words if word in common_english_words) + + # If we find many common English words, it's likely English + return matches > len(words) * 0.05 if words else False + + +def download_captions(video_id): + """ + Download closed captions from a YouTube video, preferring English. + + Args: + video_id: The YouTube video ID + + Returns: + Dictionary with 'transcript', 'language', and 'success' keys, + or None if no transcript is available + """ + try: + # First, try to get English transcript (preferably en, en-US, en-GB) + english_languages = ['en', 'en-US', 'en-GB'] + transcript = YouTubeTranscriptApi().fetch(video_id, languages=english_languages) + + if transcript: + return { + 'transcript': transcript, + 'language': transcript.language, + 'success': True + } + + # If no English transcript is available, try any language + print("No English transcript found. Trying to get transcript in original language...") + transcript = YouTubeTranscriptApi().fetch(video_id, languages=[]) + + if transcript: + return { + 'transcript': transcript, + 'language': transcript.language, + 'success': True + } + + return { + 'transcript': None, + 'language': None, + 'success': False + } + + except Exception as e: + print(f"Error downloading captions: {e}") + return { + 'transcript': None, + 'language': None, + 'success': False + } + + +if __name__ == "__main__": + main() \ No newline at end of file From 0bc502fa6f64805f97343567c8c4ba2b6fe2bc1f Mon Sep 17 00:00:00 2001 From: stephen schuresko Date: Tue, 7 Apr 2026 11:53:23 -0400 Subject: [PATCH 3/8] Refine download_captions function with enhanced fallback languages and better error handling --- youtube_transcript_downloader.py | 49 ++++++++++++++++---------------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/youtube_transcript_downloader.py b/youtube_transcript_downloader.py index 31c9ed8..020ea95 100644 --- a/youtube_transcript_downloader.py +++ b/youtube_transcript_downloader.py @@ -254,31 +254,32 @@ def download_captions(video_id): 'language': transcript.language, 'success': True } - - # If no English transcript is available, try any language - print("No English transcript found. Trying to get transcript in original language...") - transcript = YouTubeTranscriptApi().fetch(video_id, languages=[]) - - if transcript: - return { - 'transcript': transcript, - 'language': transcript.language, - 'success': True - } - - return { - 'transcript': None, - 'language': None, - 'success': False - } - except Exception as e: - print(f"Error downloading captions: {e}") - return { - 'transcript': None, - 'language': None, - 'success': False - } + try: + # If no English transcript is available, try any language + print("No English transcript found. Trying to get transcript in original language...") + transcript = YouTubeTranscriptApi().fetch(video_id, languages=['de','fa']) + + if transcript: + return { + 'transcript': transcript, + 'language': transcript.language, + 'success': True + } + + return { + 'transcript': None, + 'language': None, + 'success': False + } + + except Exception as e: + print(f"Error downloading captions: {e}") + return { + 'transcript': None, + 'language': None, + 'success': False + } if __name__ == "__main__": From c5b7a80392ecfd7180f58db63acfd785369844ac Mon Sep 17 00:00:00 2001 From: stephen schuresko Date: Tue, 7 Apr 2026 11:56:18 -0400 Subject: [PATCH 4/8] Add global language codes arrays for YouTube transcript fallback support --- language_codes.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 language_codes.py diff --git a/language_codes.py b/language_codes.py new file mode 100644 index 0000000..e69de29 From 925d5d2c54c46381ae0a986904cf03876e2e1eb6 Mon Sep 17 00:00:00 2001 From: stephen schuresko Date: Tue, 7 Apr 2026 11:57:40 -0400 Subject: [PATCH 5/8] Final implementation of YouTube caption downloader with language code arrays --- ...tube_transcript_downloader.cpython-312.pyc | Bin 10300 -> 10300 bytes transcript_v75iZrtI6hw_original.txt | 44 ++++++++++++++++ transcript_v75iZrtI6hw_translated.txt | 47 ++++++++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 transcript_v75iZrtI6hw_original.txt create mode 100644 transcript_v75iZrtI6hw_translated.txt diff --git a/__pycache__/youtube_transcript_downloader.cpython-312.pyc b/__pycache__/youtube_transcript_downloader.cpython-312.pyc index 495065788be6df5ffd5098e51c13f7153a7023ec..38dc0705665477cd5b88029fd5ec6977e377fbaf 100644 GIT binary patch delta 19 ZcmdlJuqS})G%qg~0}yOe-N Date: Tue, 7 Apr 2026 11:58:56 -0400 Subject: [PATCH 6/8] chore: add __pycache__ to .gitignore to exclude Python bytecode directories --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ed8ebf5 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__ \ No newline at end of file From 4199cf651137213940df3e42ada8fe3f2fa69f54 Mon Sep 17 00:00:00 2001 From: stephen schuresko Date: Tue, 7 Apr 2026 11:59:47 -0400 Subject: [PATCH 7/8] chore: add transcript_*.txt to .gitignore --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index ed8ebf5..8bb8c5d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -__pycache__ \ No newline at end of file +__pycache__ +transcript_*.txt \ No newline at end of file From 9aece71bf6b3972a3ab8c123a1433d4c5cd90d85 Mon Sep 17 00:00:00 2001 From: stephen schuresko Date: Tue, 7 Apr 2026 12:09:58 -0400 Subject: [PATCH 8/8] feat: add language codes module and improve transcript fallback logic MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Introduce `language_codes.py` with complete YouTube language codes and fallback lists - Refactor `youtube_transcript_downloader.py` to use centralized language lists - Replace interactive prompt with automatic English translation - Implement sequential fallback: English → common languages → all supported languages --- language_codes.py | 35 +++ transcript_v75iZrtI6hw_original.txt | 44 ---- transcript_v75iZrtI6hw_translated.txt | 47 ---- youtube_transcript_downloader.py | 46 ++-- youtube_transcript_downloader_backup.py | 285 ------------------------ 5 files changed, 50 insertions(+), 407 deletions(-) delete mode 100644 transcript_v75iZrtI6hw_original.txt delete mode 100644 transcript_v75iZrtI6hw_translated.txt delete mode 100644 youtube_transcript_downloader_backup.py diff --git a/language_codes.py b/language_codes.py index e69de29..2d6bdff 100644 --- a/language_codes.py +++ b/language_codes.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python3 +"""Global array of all 2-letter language codes supported by YouTube""" + +# Complete list of 2-letter language codes supported by YouTube +YOUTUBE_LANGUAGE_CODES = [ + "af", "sq", "am", "ar", "hy", "az", "eu", "be", "bn", "bs", "bg", "ca", "ceb", "zh", "zh-Hant", + "co", "hr", "cs", "da", "nl", "en", "eo", "et", "fi", "fr", "fy", "gl", "ka", "de", "el", "gu", + "ht", "ha", "haw", "iw", "hi", "hmn", "hu", "is", "ig", "id", "ga", "it", "ja", "jw", "kn", "kk", + "km", "rw", "ko", "ku", "ky", "lo", "la", "lv", "lt", "lb", "mk", "mg", "ms", "ml", "mt", "mi", + "mr", "mn", "my", "ne", "no", "ny", "or", "ps", "fa", "pl", "pt", "pa", "ro", "ru", "sm", "gd", + "sr", "st", "sn", "sd", "si", "sk", "sl", "so", "es", "su", "sw", "sv", "tl", "tg", "ta", "tt", + "te", "th", "tr", "tk", "uk", "ur", "ug", "uz", "vi", "cy", "fy", "xh", "yi", "yo", "zu" +] + +# Alternative: More focused list of commonly used languages for fallback +COMMON_FALLBACK_LANGUAGES = [ + "en", "en-US", "en-GB", # English variants + "de", "fr", "es", "it", "pt", # Major European languages + "ru", "ja", "ko", "zh", "zh-Hant", # Major Asian languages + "ar", "hi", "bn", "pa", # Major world languages + "tr", "nl", "pl", "sv", "da", "fi", "no", # Nordic/Scandinavian + "cs", "sk", "hu", "ro", "bg", "hr", "sl", "pl", "lt", "lv", "et" # Eastern European +] +ENGLISH_LANGUAGES = ['en', 'en-US', 'en-GB'] +# This can be used as the list of allowed fallback languages +ALLOWED_FALLBACK_LANGUAGES = COMMON_FALLBACK_LANGUAGES + +if __name__ == "__main__": + print("YouTube Language Codes:") + print(f"Total codes: {len(YOUTUBE_LANGUAGE_CODES)}") + print("First 10:", YOUTUBE_LANGUAGE_CODES[:10]) + print() + print("Common fallback languages:") + print(f"Total fallback codes: {len(ALLOWED_FALLBACK_LANGUAGES)}") + print("Fallback codes:", ALLOWED_FALLBACK_LANGUAGES) \ No newline at end of file diff --git a/transcript_v75iZrtI6hw_original.txt b/transcript_v75iZrtI6hw_original.txt deleted file mode 100644 index b6145f7..0000000 --- a/transcript_v75iZrtI6hw_original.txt +++ /dev/null @@ -1,44 +0,0 @@ -دلاوران گارد جاویدان به شما به شجاعت و -فداکاریتان درود می‌فرستم. -در ۳ ماه گذشته هزاران تن از شما در -هسته‌های کوچک اما مؤثر در سراسر [موسیقی] -ایران جلوه‌هایی درخشان از دلاوری -آفریده‌اید. -آنچه شما کرده‌اید [موسیقی] برای صده‌ها -روایت خواهد شد. ملت ایران فداکاری شما و -بهایی را که بهویژه در ۱۸ و ۱۹ دی برای -آزادی میهن پرداخته‌اید هرگز فراموش -نخواهد کرد. -به شما گفته بودم که کمک در راه است. اکون -آن کمک رسیده است. از شما دلیرترین و -فداکارترین فرزندان ایران می‌خواهم که به -عنوان نیروی پیشگام ملت به هر شکل ممکن -دستگاه سرکوب جمهوری اسلامی را که اکنون -از آسمان زیر ضربات [موسیقی] سنگین است -بیش از پیش فرسوده و تضعیف کنید تا زمینه -فروپاشی نهایی آن فراهم شود. -در نظر داشته باشید که این فراخوانی برای -تظاهرات خیابانی نیست بلکه فراخوانی به -شما جوانان گارد جاویدان برای وارد آوردن -ضربه‌های هوشمندانه و مؤثر به سکوتگران -خسته و فرسوده است تا راه برای حضور -میلیونی ملت ایران در خیابان‌ها هموارتر -شود. -در عین حال حفظ جان خود را در اولویت قرار -دهید چرا که حضور شما در نبرد نهایی برای -حفاظت از سیل میلیونی جمعیت لازم و -ضروریست شما از توار رستم و کاوه‌ای و -دشمنان شما از جنس زهاک -در این نبرد خیر و شر میان نور و تاریکی -شما در [موسیقی] سوی درست تاریخ -ایستاده‌اید -و امید و دعای یک ملت بزرگ بدر راه شماست. -بی‌صبرانه در انتظار روسی هستم که در -نخستین شهر آزاد شده ایران در کنار شما -بایستم و با هم پیروزی نهایی را بزنیم. -آینده -[موسیقی] -‏proud people ir -take your will take this will -your only for generation -‏generation \ No newline at end of file diff --git a/transcript_v75iZrtI6hw_translated.txt b/transcript_v75iZrtI6hw_translated.txt deleted file mode 100644 index b418113..0000000 --- a/transcript_v75iZrtI6hw_translated.txt +++ /dev/null @@ -1,47 +0,0 @@ -TRANSLATED TRANSCRIPT -============================================= - -The brave men of the Immortal Guard give you courage and -I salute your dedication. -In the last 3 months, thousands of you -Small but effective kernels throughout [music] -Iran, shining manifestations of bravery -you have created -What you've done [music] for centuries -It will be narrated. The nation of Iran is your sacrifice -The price that is especially on 18 and 19 December for -You have paid for the freedom of the country, never forget -will not -I told you help was on the way. now -That help has arrived. The bravest of you and -I want the most devoted children of Iran to -The title of the pioneering force of the nation in every possible way -The repressive apparatus of the Islamic Republic now -From the sky below the [musical] beats are heavy -Worn out and weaken up the field -provide its final collapse. -Note that this call for -It is not a street demonstration but a call to -You youth immortal guard to bring -Smart and effective blows to silencers -Tired and worn out way to attend -Millions of Iranian people are smoother in the streets -to be -At the same time, keep your life as a priority -Give because your presence in the final battle for -Flood protection for millions of people is necessary and -It is necessary that you are from Tovar Rostam and Kavei -Your enemies are from Zhak -In this battle between good and evil between light and darkness -You are on the right side of history -you are standing -And the hope and prayer of a great nation is on your way. -I am impatiently waiting for the Russian in -The first freed city of Iran next to you -Let's stand up and win the final victory together. -the future -[music] -proud people ir -take your will take this will -your only for generation -generation \ No newline at end of file diff --git a/youtube_transcript_downloader.py b/youtube_transcript_downloader.py index 020ea95..258e0a6 100644 --- a/youtube_transcript_downloader.py +++ b/youtube_transcript_downloader.py @@ -7,9 +7,11 @@ This program downloads a YouTube video transcript and translates it to English. import sys import re +import json from youtube_transcript_api import YouTubeTranscriptApi from youtube_transcript_api.formatters import TextFormatter from deep_translator import GoogleTranslator +import language_codes def extract_video_id(url): @@ -150,9 +152,9 @@ def main(): if needs_translation: print("\nNote: The transcript appears to be in a non-English language.") - print("Would you like to translate it to English?") - response = input("Enter 'y' for yes or 'n' for no: ").strip().lower() - + #print("Would you like to translate it to English?") + #response = input("Enter 'y' for yes or 'n' for no: ").strip().lower() + response = 'y' if response == 'y': print("\nTranslating to English...") translated = translate_text(original_text, 'en') @@ -243,22 +245,10 @@ def download_captions(video_id): Dictionary with 'transcript', 'language', and 'success' keys, or None if no transcript is available """ - try: - # First, try to get English transcript (preferably en, en-US, en-GB) - english_languages = ['en', 'en-US', 'en-GB'] - transcript = YouTubeTranscriptApi().fetch(video_id, languages=english_languages) - - if transcript: - return { - 'transcript': transcript, - 'language': transcript.language, - 'success': True - } - except Exception as e: + for languages in language_codes.ENGLISH_LANGUAGES, language_codes.COMMON_FALLBACK_LANGUAGES, language_codes.YOUTUBE_LANGUAGE_CODES: try: - # If no English transcript is available, try any language - print("No English transcript found. Trying to get transcript in original language...") - transcript = YouTubeTranscriptApi().fetch(video_id, languages=['de','fa']) + # First, try to get English transcript (preferably en, en-US, en-GB) + transcript = YouTubeTranscriptApi().fetch(video_id, languages=languages) if transcript: return { @@ -266,20 +256,14 @@ def download_captions(video_id): 'language': transcript.language, 'success': True } - - return { - 'transcript': None, - 'language': None, - 'success': False - } - except Exception as e: - print(f"Error downloading captions: {e}") - return { - 'transcript': None, - 'language': None, - 'success': False - } + print("failed to fetch transcript in the following languages: "+json.dumps(languages,indent=2)) + print(f"Error downloading captions: {e}") + return { + 'transcript': None, + 'language': None, + 'success': False + } if __name__ == "__main__": diff --git a/youtube_transcript_downloader_backup.py b/youtube_transcript_downloader_backup.py deleted file mode 100644 index 31c9ed8..0000000 --- a/youtube_transcript_downloader_backup.py +++ /dev/null @@ -1,285 +0,0 @@ -#!/usr/bin/env python3 -""" -YouTube Transcript Downloader and Translator - -This program downloads a YouTube video transcript and translates it to English. -""" - -import sys -import re -from youtube_transcript_api import YouTubeTranscriptApi -from youtube_transcript_api.formatters import TextFormatter -from deep_translator import GoogleTranslator - - -def extract_video_id(url): - """Extract YouTube video ID from various URL formats.""" - patterns = [ - r'(?:v=|\/)([0-9A-Za-z_-]{11}).*', - r'youtu\.be\/([0-9A-Za-z_-]{11})', - ] - - for pattern in patterns: - match = re.search(pattern, url) - if match: - return match.group(1) - return None - - -def get_transcript(video_id, languages=None): - """ - Get transcript for a YouTube video. - - Args: - video_id: The YouTube video ID - languages: List of language codes to try (default: ['en', 'en-US']) - - Returns: - Transcript object or None if not found - """ - if languages is None: - languages = ['en', 'en-US'] - - try: - transcript = YouTubeTranscriptApi().fetch(video_id, languages=languages) - return transcript - except Exception as e: - print(f"Error getting transcript: {e}") - return None - - -def get_transcript_text(transcript): - """Extract plain text from transcript.""" - formatter = TextFormatter() - return formatter.format_transcript(transcript) - - -def translate_text(text, target_language='en'): - """ - Translate text to target language using Google Translate. - - Args: - text: The text to translate - target_language: Target language code (default: 'en') - - Returns: - Translated text or None if translation fails - """ - try: - translator = GoogleTranslator(source='auto', target=target_language) - translated = translator.translate(text) - return translated - except Exception as e: - print(f"Error during translation: {e}") - return None - - -def format_transcript(transcript, include_timestamps=True): - """ - Format transcript into readable text. - - Args: - transcript: The transcript list of dictionaries - include_timestamps: Whether to include timestamps - - Returns: - Formatted string - """ - formatter = TextFormatter() - return formatter.format_transcript(transcript) - - -def detect_language(transcript): - """Attempt to detect the language of the transcript.""" - if not transcript: - return "unknown" - - # Get first few words to help identify language - if isinstance(transcript, list) and len(transcript) > 0: - text = transcript[0].get('text', '') - return text[:100] - return "" - - -def main(): - """Main function to run the transcript downloader.""" - print("YouTube Transcript Downloader and Translator") - print("=" * 45) - - # Get YouTube URL from command line argument or prompt user - if len(sys.argv) > 1: - url = sys.argv[1] - else: - url = input("Enter YouTube URL: ").strip() - - # Extract video ID - video_id = extract_video_id(url) - if not video_id: - print("Error: Could not extract video ID from URL") - print("Please enter a valid YouTube URL") - sys.exit(1) - - print(f"Video ID: {video_id}") - print("Fetching transcript...") - - # Download captions with preference for English - result = download_captions(video_id) - - if not result['success']: - print("\nCould not retrieve transcript.") - print("Possible reasons:") - print("- Video does not have captions/subtitles") - print("- Video is live stream") - print("- Video is private") - sys.exit(1) - - # Format original transcript - original_formatted = format_transcript(result['transcript'], include_timestamps=True) - - # Check if translation is needed (try to detect if it's already English) - original_text = get_transcript_text(result['transcript']) - - # Ask user if they want to translate - print("\n" + "=" * 45) - print("Transcript successfully retrieved!") - print(f"Language: {result['language']}") - - # Determine if translation is needed - # If transcript is in English, no translation needed - needs_translation = not is_english(original_text) - - if needs_translation: - print("\nNote: The transcript appears to be in a non-English language.") - print("Would you like to translate it to English?") - response = input("Enter 'y' for yes or 'n' for no: ").strip().lower() - - if response == 'y': - print("\nTranslating to English...") - translated = translate_text(original_text, 'en') - - if translated: - # Save translated transcript - output_file = f"transcript_{video_id}_translated.txt" - with open(output_file, 'w', encoding='utf-8') as f: - f.write("TRANSLATED TRANSCRIPT\n") - f.write("=" * 45 + "\n\n") - f.write(translated) - - print(f"\nTranslated transcript saved to: {output_file}") - - # Save both original and translated - original_file = f"transcript_{video_id}_original.txt" - with open(original_file, 'w', encoding='utf-8') as f: - f.write(original_formatted) - - print(f"Original transcript saved to: {original_file}") - - # Display original and translated - print("\n" + "=" * 45) - print("ORIGINAL TRANSCRIPT:") - print("=" * 45) - print(original_formatted) - - print("\n" + "=" * 45) - print("TRANSLATED TRANSCRIPT:") - print("=" * 45) - print(translated) - else: - print("\nTranslation failed. Saving original transcript...") - output_file = f"transcript_{video_id}.txt" - with open(output_file, 'w', encoding='utf-8') as f: - f.write(original_formatted) - print(f"Transcript saved to: {output_file}") - else: - output_file = f"transcript_{video_id}.txt" - with open(output_file, 'w', encoding='utf-8') as f: - f.write(original_formatted) - print(f"\nTranscript saved to: {output_file}") - else: - # Already in English, just save it - output_file = f"transcript_{video_id}.txt" - with open(output_file, 'w', encoding='utf-8') as f: - f.write(original_formatted) - print(f"\nTranscript saved to: {output_file}") - print("\n" + "=" * 45) - print("TRANSCRIPT:") - print("=" * 45) - print(original_formatted) - - -def is_english(text): - """Simple check to determine if text is likely English.""" - # Common English words to check against - common_english_words = { - 'the', 'be', 'to', 'of', 'and', 'a', 'in', 'that', 'have', 'i', - 'it', 'for', 'not', 'on', 'with', 'he', 'as', 'you', 'do', 'at', - 'this', 'but', 'his', 'by', 'from', 'they', 'we', 'say', 'her', - 'she', 'or', 'an', 'will', 'my', 'one', 'all', 'would', 'there', - 'their', 'what', 'so', 'up', 'out', 'if', 'about', 'who', 'get', - 'which', 'go', 'me', 'when', 'make', 'can', 'like', 'time', 'no', - 'just', 'him', 'know', 'take', 'people', 'into', 'year', 'your', - 'good', 'some', 'could', 'them', 'see', 'other', 'than', 'then', - 'now', 'look', 'only', 'come', 'its', 'over', 'think', 'also', - 'back', 'after', 'use', 'two', 'how', 'our', 'work', 'first', 'well', - 'way', 'even', 'new', 'want', 'because', 'any', 'these', 'give', 'day' - } - - # Simple heuristic: count common English words - words = set(text.lower().split()) - matches = sum(1 for word in words if word in common_english_words) - - # If we find many common English words, it's likely English - return matches > len(words) * 0.05 if words else False - - -def download_captions(video_id): - """ - Download closed captions from a YouTube video, preferring English. - - Args: - video_id: The YouTube video ID - - Returns: - Dictionary with 'transcript', 'language', and 'success' keys, - or None if no transcript is available - """ - try: - # First, try to get English transcript (preferably en, en-US, en-GB) - english_languages = ['en', 'en-US', 'en-GB'] - transcript = YouTubeTranscriptApi().fetch(video_id, languages=english_languages) - - if transcript: - return { - 'transcript': transcript, - 'language': transcript.language, - 'success': True - } - - # If no English transcript is available, try any language - print("No English transcript found. Trying to get transcript in original language...") - transcript = YouTubeTranscriptApi().fetch(video_id, languages=[]) - - if transcript: - return { - 'transcript': transcript, - 'language': transcript.language, - 'success': True - } - - return { - 'transcript': None, - 'language': None, - 'success': False - } - - except Exception as e: - print(f"Error downloading captions: {e}") - return { - 'transcript': None, - 'language': None, - 'success': False - } - - -if __name__ == "__main__": - main() \ No newline at end of file