Upload 2 files
Browse files- app.py +52 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from pytube import YouTube
|
3 |
+
import validators
|
4 |
+
import pathlib
|
5 |
+
import glob
|
6 |
+
|
7 |
+
def download_video(url):
|
8 |
+
with gr.spinner("Downloading..."):
|
9 |
+
local_file = (
|
10 |
+
YouTube(url)
|
11 |
+
.streams.filter(progressive=True, file_extension="mp4")
|
12 |
+
.first()
|
13 |
+
.download()
|
14 |
+
)
|
15 |
+
gr.success("Downloaded")
|
16 |
+
return local_file
|
17 |
+
|
18 |
+
def validate_url(url):
|
19 |
+
if not validators.url(url):
|
20 |
+
gr.error("Hi there 👋 URL seems invalid 👽")
|
21 |
+
gr.stop()
|
22 |
+
|
23 |
+
def cleanup():
|
24 |
+
junks = glob.glob("*.mp4")
|
25 |
+
for junk in junks:
|
26 |
+
pathlib.Path(junk).unlink()
|
27 |
+
|
28 |
+
def helper_message():
|
29 |
+
gr.write(
|
30 |
+
"> To save the video to the local computer, "
|
31 |
+
"click the vertical ... icon (aka hamburger button) in the bottom-right corner (in the video above) and click download."
|
32 |
+
)
|
33 |
+
|
34 |
+
def youtube_downloader(url):
|
35 |
+
gr.header("YouTube Video Downloader")
|
36 |
+
url = gr.text_input("Enter YouTube URL to download:")
|
37 |
+
start_button = gr.button("Start Download")
|
38 |
+
|
39 |
+
if start_button:
|
40 |
+
if url:
|
41 |
+
validate_url(url)
|
42 |
+
with gr.expander("preview video"):
|
43 |
+
gr.video(url)
|
44 |
+
cleanup()
|
45 |
+
file_ = download_video(url)
|
46 |
+
gr.video(file_)
|
47 |
+
helper_message()
|
48 |
+
|
49 |
+
gr.markdown("YouTube Video Download Help")
|
50 |
+
|
51 |
+
iface = gr.Interface(fn=youtube_downloader, live=True)
|
52 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
pytube
|
2 |
+
gradio
|
3 |
+
validators
|