|
import streamlit as st |
|
from youtube_transcript_api import YouTubeTranscriptApi |
|
from youtube_transcript_api.formatters import SRTFormatter |
|
|
|
|
|
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. |
|
""" |
|
|
|
if "si=" in youtube_url: |
|
|
|
video_id = youtube_url.split("si=")[-1].split("?")[0] |
|
else: |
|
|
|
video_id = youtube_url.split("v=")[-1].split("&")[0] |
|
|
|
|
|
print(f"Extracted Video ID: {video_id}") |
|
|
|
try: |
|
|
|
transcript = YouTubeTranscriptApi.get_transcript(video_id) |
|
|
|
|
|
formatter = SRTFormatter() |
|
formatted_transcript = formatter.format_transcript(transcript) |
|
return formatted_transcript |
|
|
|
except Exception as e: |
|
return f"Error: {str(e)}" |
|
|
|
|
|
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: |
|
|
|
youtube_url_cleaned = youtube_url.split("?")[0] |
|
|
|
try: |
|
with st.spinner("Fetching and processing the YouTube video..."): |
|
|
|
captions = get_youtube_captions(youtube_url_cleaned) |
|
|
|
|
|
if "Error" not in 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.") |
|
|