testing / app.py
Manasa1's picture
Update app.py
0ce9a88 verified
raw
history blame
2.35 kB
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 "si=" in youtube_url:
# For YouTube Shorts or URLs with "si=", extract the video ID after "si="
video_id = youtube_url.split("si=")[-1].split("?")[0]
else:
# For regular YouTube videos, extract the video ID after "v="
video_id = youtube_url.split("v=")[-1].split("&")[0]
# Print extracted video ID for debugging
print(f"Extracted Video ID: {video_id}")
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:
# Clean URL before passing to the function (remove extra parts)
youtube_url_cleaned = youtube_url.split("?")[0] # Remove URL parameters
try:
with st.spinner("Fetching and processing the YouTube video..."):
# Get captions using youtube-transcript-api
captions = get_youtube_captions(youtube_url_cleaned)
# 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.")