| import gradio as gr |
| from youtube_transcript_api import YouTubeTranscriptApi |
| import re |
|
|
| def get_video_id(url): |
| """Extract video ID from YouTube URL""" |
| try: |
| if "youtu.be" in url: |
| return url.split("/")[-1] |
| else: |
| return re.findall(r"v=([^&]+)", url)[0] |
| except Exception: |
| raise ValueError("Invalid YouTube URL") |
|
|
| def get_transcript(url): |
| """Get transcript for a YouTube video""" |
| try: |
| |
| video_id = get_video_id(url) |
| |
| |
| |
| transcript = YouTubeTranscriptApi.get_transcript(video_id) |
| |
| |
| |
| |
| full_transcript = "\n".join([item["text"] for item in transcript]) |
| |
| return full_transcript |
| |
| except ValueError as e: |
| return f"Error: {str(e)}" |
| except Exception as e: |
| return "Error: This video doesn't have captions available. Please try a different video." |
|
|
| |
| demo = gr.Interface( |
| fn=get_transcript, |
| inputs=gr.Textbox( |
| label="YouTube URL", |
| placeholder="Enter YouTube video URL here...", |
| ), |
| outputs=gr.Textbox( |
| label="Transcript", |
| lines=20, |
| placeholder="Transcript will appear here..." |
| ), |
| title="YouTube Transcript Extractor", |
| description="Enter a YouTube URL to get its transcript/captions.", |
| examples=[ |
| ["https://www.youtube.com/watch?v=dQw4w9WgXcQ"], |
| ["https://youtu.be/dQw4w9WgXcQ"] |
| ] |
| ) |
|
|
| if __name__ == "__main__": |
| demo.launch() |