Geek7 commited on
Commit
d6cf517
·
1 Parent(s): 7cca4ee

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +93 -0
app.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from pytube import YouTube
3
+
4
+ class YouTubeDownloader:
5
+ @staticmethod
6
+ def run():
7
+ st.markdown(
8
+ """
9
+ <style>
10
+ body {
11
+ margin: 0;
12
+ overflow: hidden;
13
+ background: linear-gradient(to right, #ffcccb, #b5e7a0);
14
+ animation: gradientAnimation 10s infinite alternate;
15
+ display: flex;
16
+ justify-content: center;
17
+ align-items: center;
18
+ height: 100vh;
19
+ }
20
+
21
+ @keyframes gradientAnimation {
22
+ 0% {
23
+ background-position: 0% 50%;
24
+ }
25
+ 100% {
26
+ background-position: 100% 50%;
27
+ }
28
+ }
29
+
30
+ .container {
31
+ background-color: rgba(255, 255, 255, 0.8);
32
+ padding: 20px;
33
+ border-radius: 10px;
34
+ text-align: center;
35
+ }
36
+ </style>
37
+ """
38
+ )
39
+
40
+ st.write('<div class="container">')
41
+ st.header("YouTube Video Downloader")
42
+ url = st.text_input("Enter YouTube URL to download:")
43
+ if url:
44
+ YouTubeDownloader.validate_url(url)
45
+ with st.expander("Preview Video"):
46
+ st.video(url)
47
+ if st.button("Download"):
48
+ YouTubeDownloader.cleanup()
49
+ file_ = YouTubeDownloader.download_video(url)
50
+ st.video(file_)
51
+ YouTubeDownloader.helper_message()
52
+ st.markdown("YouTube Video Download help")
53
+ st.write("</div>")
54
+
55
+ @staticmethod
56
+ def download_video(url):
57
+ with st.spinner("Downloading..."):
58
+ local_file = (
59
+ YouTube(url)
60
+ .streams.filter(progressive=True, file_extension="mp4")
61
+ .first()
62
+ .download()
63
+ )
64
+ st.success("Downloaded")
65
+ return local_file
66
+
67
+ @staticmethod
68
+ def validate_url(url):
69
+ import validators
70
+
71
+ if not validators.url(url):
72
+ st.error("Hi there 👋 URL seems invalid 👽")
73
+ st.stop()
74
+
75
+ @classmethod
76
+ def cleanup(cls):
77
+ import pathlib
78
+ import glob
79
+
80
+ junks = glob.glob("*.mp4")
81
+ for junk in junks:
82
+ pathlib.Path(junk).unlink()
83
+
84
+ @classmethod
85
+ def helper_message(cls):
86
+ st.write(
87
+ "> To save the video to the local computer, "
88
+ "click the vertical ... icon (aka hamburger button) in the bottom-right corner (in the video above) and click download."
89
+ )
90
+
91
+
92
+ if __name__ == "__main__":
93
+ YouTubeDownloader.run()