|
import gradio as gr |
|
from pytube import YouTube |
|
import validators |
|
import pathlib |
|
import glob |
|
|
|
def download_video(url): |
|
with gr.spinner("Downloading..."): |
|
local_file = ( |
|
YouTube(url) |
|
.streams.filter(progressive=True, file_extension="mp4") |
|
.first() |
|
.download() |
|
) |
|
gr.success("Downloaded") |
|
return local_file |
|
|
|
def validate_url(url): |
|
if not validators.url(url): |
|
gr.error("Hi there π URL seems invalid π½") |
|
gr.stop() |
|
|
|
def cleanup(): |
|
junks = glob.glob("*.mp4") |
|
for junk in junks: |
|
pathlib.Path(junk).unlink() |
|
|
|
def helper_message(): |
|
gr.write( |
|
"> To save the video to the local computer, " |
|
"click the vertical ... icon (aka hamburger button) in the bottom-right corner (in the video above) and click download." |
|
) |
|
|
|
def youtube_downloader(url): |
|
gr.header("YouTube Video Downloader") |
|
url = gr.text_input("Enter YouTube URL to download:") |
|
start_button = gr.button("Start Download") |
|
|
|
if start_button: |
|
if url: |
|
validate_url(url) |
|
with gr.expander("preview video"): |
|
gr.video(url) |
|
cleanup() |
|
file_ = download_video(url) |
|
gr.video(file_) |
|
helper_message() |
|
|
|
gr.markdown("YouTube Video Download Help") |
|
|
|
iface = gr.Interface(fn=youtube_downloader, live=True) |
|
iface.launch() |
|
|