File size: 1,072 Bytes
efcbeaf
81927b7
efcbeaf
81927b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from youtube_transcript_api import YouTubeTranscriptApi

def get_video_id(url):
    from urllib.parse import urlparse, parse_qs
    query = urlparse(url)
    if query.hostname == 'youtu.be':
        return query.path[1:]
    if query.hostname in ('www.youtube.com', 'youtube.com'):
        if query.path == '/watch':
            p = parse_qs(query.query)
            return p['v'][0]
        if query.path[:7] == '/embed/':
            return query.path.split('/')[2]
        if query.path[:3] == '/v/':
            return query.path.split('/')[2]
    return None

def get_transcript(video_id):
    try:
        transcript = YouTubeTranscriptApi.get_transcript(video_id)
        return ' '.join([entry['text'] for entry in transcript])
    except Exception as e:
        return str(e)

st.title('YouTube Transcript Reader')

url = st.text_input('Enter YouTube URL')
if url:
    video_id = get_video_id(url)
    if video_id:
        transcript = get_transcript(video_id)
        st.write(transcript)
    else:
        st.write('Invalid YouTube URL')