Final_Assignment_Template / youtube_tools.py
Giustino98's picture
first submission
c4b829b
raw
history blame
1.14 kB
from langchain.tools import Tool
from youtube_transcript_api import YouTubeTranscriptApi, NoTranscriptFound, TranscriptsDisabled
def extract_youtube_transcript(youtube_url: str) -> str:
"""
Extracts the transcript from a given YouTube video URL.
Returns the transcript as a single string or an error message if not found.
"""
try:
video_id = youtube_url.split("v=")[1].split("&")[0]
transcript_list = YouTubeTranscriptApi.get_transcript(video_id)
transcript = " ".join([item['text'] for item in transcript_list])
return transcript
except NoTranscriptFound:
return "Error: No transcript found for this video. It might be disabled or not available in English."
except TranscriptsDisabled:
return "Error: Transcripts are disabled for this video."
except Exception as e:
return f"Error extracting transcript: {str(e)}"
youtube_transcript_tool = Tool(
name="youtube_transcript_extractor",
func=extract_youtube_transcript,
description="Extracts the full transcript from a YouTube video given its URL. Input should be a valid YouTube video URL."
)