Geek7 commited on
Commit
6be5e98
·
1 Parent(s): 711084d

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -0
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from pytube import YouTube
3
+
4
+ class YouTubeDownloader:
5
+ @staticmethod
6
+ def run():
7
+ st.header("YouTube Video Downloader")
8
+ url = st.text_input("Enter YouTube URL to download:")
9
+ go_button = st.button("Go")
10
+
11
+ if go_button:
12
+ YouTubeDownloader.validate_url(url)
13
+ with st.expander("preview video"):
14
+ st.video(url)
15
+ if st.button("Download"):
16
+ YouTubeDownloader.cleanup()
17
+ file_ = YouTubeDownloader.download_video(url)
18
+ st.video(file_)
19
+ YouTubeDownloader.helper_message()
20
+
21
+ st.markdown("YouTube Video Download Help")
22
+
23
+ @staticmethod
24
+ def download_video(url):
25
+ with st.spinner("Downloading..."):
26
+ local_file = (
27
+ YouTube(url)
28
+ .streams.filter(progressive=True, file_extension="mp4")
29
+ .first()
30
+ .download()
31
+ )
32
+ st.success("Downloaded")
33
+ return local_file
34
+
35
+ @staticmethod
36
+ def validate_url(url):
37
+ import validators
38
+
39
+ if not validators.url(url):
40
+ st.error("Hi there 👋 URL seems invalid 👽")
41
+ st.stop()
42
+
43
+ @classmethod
44
+ def cleanup(cls):
45
+ import pathlib
46
+ import glob
47
+
48
+ junks = glob.glob("*.mp4")
49
+ for junk in junks:
50
+ pathlib.Path(junk).unlink()
51
+
52
+ @classmethod
53
+ def helper_message(cls):
54
+ st.write(
55
+ "> To save the video to local computer, "
56
+ "click the vertical ... icon (aka hamburger button) in the bottom-right corner (in the video above) and click download."
57
+ )
58
+
59
+
60
+ if __name__ == "__main__":
61
+ YouTubeDownloader.run()