Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 Video"):
|
14 |
+
YouTubeDownloader.cleanup()
|
15 |
+
file_path = YouTubeDownloader.download_video(url)
|
16 |
+
st.success("Downloaded")
|
17 |
+
st.download_button(
|
18 |
+
label="Download Here",
|
19 |
+
key="download_button",
|
20 |
+
on_click=YouTubeDownloader.download_button_callback,
|
21 |
+
args=(file_path,),
|
22 |
+
)
|
23 |
+
YouTubeDownloader.helper_message()
|
24 |
+
st.markdown("YouTube Video Download help")
|
25 |
+
|
26 |
+
@staticmethod
|
27 |
+
def download_video(url):
|
28 |
+
with st.spinner("Downloading..."):
|
29 |
+
local_file = (
|
30 |
+
YouTube(url)
|
31 |
+
.streams.filter(progressive=True, file_extension="mp4")
|
32 |
+
.first()
|
33 |
+
.download()
|
34 |
+
)
|
35 |
+
return local_file
|
36 |
+
|
37 |
+
@staticmethod
|
38 |
+
def download_button_callback(file_path):
|
39 |
+
with open(file_path, "rb") as file:
|
40 |
+
file_data = file.read()
|
41 |
+
st.download_button(
|
42 |
+
label="Click to Download",
|
43 |
+
data=file_data,
|
44 |
+
key="file_download",
|
45 |
+
file_name="downloaded_video.mp4",
|
46 |
+
mime="video/mp4",
|
47 |
+
)
|
48 |
+
|
49 |
+
@staticmethod
|
50 |
+
def validate_url(url):
|
51 |
+
import validators
|
52 |
+
|
53 |
+
if not validators.url(url):
|
54 |
+
st.error("Hi there 👋 URL seems invalid 👽")
|
55 |
+
st.stop()
|
56 |
+
|
57 |
+
@classmethod
|
58 |
+
def cleanup(cls):
|
59 |
+
import pathlib
|
60 |
+
import glob
|
61 |
+
|
62 |
+
junks = glob.glob("*.mp4")
|
63 |
+
for junk in junks:
|
64 |
+
pathlib.Path(junk).unlink()
|
65 |
+
|
66 |
+
@classmethod
|
67 |
+
def helper_message(cls):
|
68 |
+
st.write(
|
69 |
+
"> To save the video to local computer, "
|
70 |
+
"click the vertical ... icon (aka hamburger button) in the bottom-right corner (in the video above) and click download."
|
71 |
+
)
|
72 |
+
|
73 |
+
|
74 |
+
if __name__ == "__main__":
|
75 |
+
YouTubeDownloader.run()
|