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