File size: 718 Bytes
5a46645
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from youtube_transcript_api import YouTubeTranscriptApi

def get_youtube_transcript(video_id: str) -> str:
    """
    Fetches the transcript for a given YouTube video ID.

    Args:
        video_id: The ID of the YouTube video.

    Returns:
        The transcript of the video as a string, or an error message if the transcript cannot be fetched.
    """
    try:
        transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
        transcript = transcript_list.find_generated_transcript(['en'])
        fetched_transcript = transcript.fetch()
        return " ".join([segment['text'] for segment in fetched_transcript])
    except Exception as e:
        return f"Error fetching transcript: {str(e)}"