File size: 1,406 Bytes
2ea0bfc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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()