import streamlit as st from youtube_transcript_api import YouTubeTranscriptApi from youtube_transcript_api.formatters import SRTFormatter # Function to get captions using youtube-transcript-api def get_youtube_captions(youtube_url): """ Retrieves YouTube video captions using youtube-transcript-api. Parameters: - youtube_url: The URL of the YouTube video. Returns: - The captions of the video in SRT format, or a message if captions are not available. """ # Extract video ID from YouTube URL (for both regular videos and Shorts) if "shorts" in youtube_url: video_id = youtube_url.split("shorts/")[-1].split("?")[0] else: video_id = youtube_url.split("v=")[-1].split("&")[0] try: # Fetch transcript for the video transcript = YouTubeTranscriptApi.get_transcript(video_id) # Format the transcript in SRT format formatter = SRTFormatter() formatted_transcript = formatter.format_transcript(transcript) return formatted_transcript except Exception as e: return f"Error: {str(e)}" # YouTube video URL input youtube_url = st.text_input( "Enter the YouTube video link", placeholder="Paste the YouTube video URL here", help="Provide the link to the YouTube video you want to summarize." ) if youtube_url: try: with st.spinner("Fetching and processing the YouTube video..."): # Get captions using youtube-transcript-api captions = get_youtube_captions(youtube_url) # Check if captions are available if "Error" not in captions: # Display captions st.subheader("Video Captions:") st.text(captions) else: st.error(captions) except Exception as e: st.error(f"An error occurred: {e}") else: st.info("Enter a YouTube video link to begin analysis.")