File size: 2,351 Bytes
eaa4026 17ce9b6 eaa4026 17ce9b6 a468187 841092b 17ce9b6 841092b a468187 841092b 85acead 0ce9a88 85acead 470bc98 85acead 470bc98 85acead 17ce9b6 2569351 17ce9b6 2569351 eaa4026 470bc98 eaa4026 17ce9b6 470bc98 eaa4026 17ce9b6 eaa4026 17ce9b6 eaa4026 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
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.")
|