Merge branch 'new'

This commit is contained in:
2026-04-07 12:18:50 -04:00
3 changed files with 17 additions and 31 deletions

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
__pycache__
transcript_*.txt

View File

@@ -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__":