-
Notifications
You must be signed in to change notification settings - Fork 1
/
yt_downloader.py
66 lines (54 loc) · 2.12 KB
/
yt_downloader.py
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
import streamlit as st
import assemblyai as aai
from yt_dlp import YoutubeDL
@st.cache_data
def download_video_from_url(url):
videoinfo = YoutubeDL().extract_info(url=url, download=False)
filename = f"./youtube/{videoinfo['id']}.mp4"
options = {
'format': 'bestvideo/best',
'keepvideo': True,
'outtmpl': filename,
}
with YoutubeDL(options) as ydl:
ydl.download([videoinfo['webpage_url']])
return filename
@st.cache_data
def download_audio_from_url(url):
videoinfo = YoutubeDL().extract_info(url=url, download=False)
filename = f"./youtube/{videoinfo['id']}.mp3"
options = {
'format': 'bestaudio/best',
'keepvideo': False,
'outtmpl': filename,
}
with YoutubeDL(options) as ydl:
ydl.download([videoinfo['webpage_url']])
return filename
@st.cache_data
def generate_subtitles(filename):
aai.settings.api_key = "YOUR_API_KEY"
transcript = aai.Transcriber().transcribe(filename)
subtitles = transcript.export_subtitles_srt()
return subtitles
def main():
st.title("YouTube Video Downloader and Subtitle Generator")
video_url = st.text_input("Enter the YouTube video URL:")
if st.button("Generate Subtitles"):
with st.status("Processing video file...", expanded=True):
st.write("Downloading audio from YouTube video..")
filename_audio = download_audio_from_url(video_url)
st.write("Downloading video from YouTube video..")
filename_video = download_video_from_url(video_url)
st.write("Generating subtitles from audio file..")
subtitles = generate_subtitles(filename_audio)
col1, col2 = st.columns(2)
with col1:
st.video(filename_video)
with open(filename_video, "rb") as f:
st.download_button("Download Video", data=f, file_name="video.mp4")
with col2:
st.text_area("Subtitles", value=subtitles, height=200, label_visibility="collapsed")
st.download_button("Download Subtitles", data=subtitles, file_name="video.srt")
if __name__ == "__main__":
main()