File size: 1,032 Bytes
2ce5811
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from youtube_transcript_api import YouTubeTranscriptApi, NoTranscriptFound

def get_transcript(video_id):
    """Get the English transcript for a YouTube video, prioritizing manual over auto-generated transcripts."""
    print("Getting video transcript...")

    try:
        # Fetch all available transcripts for the video
        transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
        
        # Try to find a manual English transcript
        english_transcript = transcript_list.find_transcript(['en'])
        if not english_transcript.is_generated:
            # If a manual transcript is found, fetch it
            return english_transcript.fetch()

        # If no manual transcript, try to find an auto-generated English transcript
        english_generated_transcript = transcript_list.find_generated_transcript(['en'])
        return english_generated_transcript.fetch()
        
    except NoTranscriptFound:
        print(f"No English transcript found for video ID: {video_id}")
        return None