Add download_captions function to download YouTube captions with English preference
This commit is contained in:
@@ -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()
|
||||
Reference in New Issue
Block a user