File size: 1,953 Bytes
eaa4026
17ce9b6
 
eaa4026
17ce9b6
a468187
841092b
17ce9b6
841092b
 
 
 
 
a468187
841092b
85acead
 
 
 
 
 
17ce9b6
 
 
2569351
17ce9b6
 
 
 
 
 
 
2569351
eaa4026
 
 
 
 
 
 
 
 
17ce9b6
 
a468187
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
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.")