Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -1,52 +1,56 @@
|
|
1 |
import streamlit as st
|
2 |
from pytube import YouTube
|
3 |
-
import validators
|
4 |
-
from tqdm import tqdm
|
5 |
-
import os
|
6 |
|
7 |
class YouTubeDownloader:
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
os.makedirs(self.output_dir, exist_ok=True)
|
12 |
-
|
13 |
-
def run(self):
|
14 |
-
st.title("YouTube Video Downloader")
|
15 |
url = st.text_input("Enter YouTube URL to download:")
|
16 |
if url:
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
yt = YouTube(url)
|
35 |
-
stream = yt.streams.get_highest_resolution()
|
36 |
-
video = stream.download(output_path=self.output_dir, filename="video")
|
37 |
-
st.success("Downloaded successfully!")
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
|
44 |
-
|
45 |
-
|
|
|
46 |
|
47 |
-
|
48 |
-
|
|
|
|
|
|
|
|
|
49 |
|
50 |
if __name__ == "__main__":
|
51 |
-
|
52 |
-
downloader.run()
|
|
|
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 |
if url:
|
10 |
+
YouTubeDownloader.validate_url(url)
|
11 |
+
with st.expander("Preview video"):
|
12 |
+
st.video(url)
|
13 |
+
if st.button("Download"):
|
14 |
+
YouTubeDownloader.cleanup()
|
15 |
+
file_ = YouTubeDownloader.download_video(url)
|
16 |
+
st.video(file_)
|
17 |
+
YouTubeDownloader.helper_message()
|
18 |
+
|
19 |
+
@staticmethod
|
20 |
+
def download_video(url):
|
21 |
+
with st.spinner("Downloading..."):
|
22 |
+
local_file = (
|
23 |
+
YouTube(url)
|
24 |
+
.streams.filter(progressive=True, file_extension="mp4")
|
25 |
+
.first()
|
26 |
+
.download()
|
27 |
+
)
|
28 |
+
st.success("Downloaded")
|
29 |
+
return local_file
|
30 |
+
|
31 |
+
@staticmethod
|
32 |
+
def validate_url(url):
|
33 |
+
import validators
|
34 |
|
35 |
+
if not validators.url(url):
|
36 |
+
st.error("Hi there 👋 URL seems invalid 👽")
|
37 |
+
st.stop()
|
|
|
|
|
|
|
|
|
38 |
|
39 |
+
@classmethod
|
40 |
+
def cleanup(cls):
|
41 |
+
import pathlib
|
42 |
+
import glob
|
43 |
|
44 |
+
junks = glob.glob("*.mp4")
|
45 |
+
for junk in junks:
|
46 |
+
pathlib.Path(junk).unlink()
|
47 |
|
48 |
+
@classmethod
|
49 |
+
def helper_message(cls):
|
50 |
+
st.write(
|
51 |
+
"> To save the video to the local computer, "
|
52 |
+
"click the vertical ... icon (aka hamburger button) in the bottom-right corner (in the video above) and click download."
|
53 |
+
)
|
54 |
|
55 |
if __name__ == "__main__":
|
56 |
+
YouTubeDownloader.run()
|
|