File size: 2,636 Bytes
cb8512f
 
73ce86c
 
 
 
d861f36
 
 
 
 
 
 
 
 
 
 
 
73ce86c
 
 
 
 
d861f36
 
 
 
73ce86c
d861f36
 
 
 
 
73ce86c
61941bf
 
 
 
 
 
 
10c5e6b
d861f36
 
61941bf
73ce86c
d861f36
b1596a8
 
2c706dc
 
 
 
 
61941bf
114d786
 
 
 
 
 
d861f36
114d786
 
 
 
61941bf
 
d861f36
 
73ce86c
d861f36
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
print("please wait...")

import gradio as gr
import yt_dlp

def download_media(url, download_video):
    ydl_opts = {
        'format': 'bestvideo+bestaudio/best' if download_video else 'bestaudio/best',
        'outtmpl': 'downloads/%(title)s.%(ext)s',
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '192',
        }] if not download_video else [],
        'noplaylist': True,
        'quiet': True,
        'progress_hooks': [progress_hook],
    }

    with yt_dlp.YoutubeDL(ydl_opts) as ydl:
        info_dict = ydl.extract_info(url, download=True)
        file_title = ydl.prepare_filename(info_dict)
        
        if not download_video:
            file_title = file_title.rsplit('.', 1)[0] + '.mp3'

    return file_title

def progress_hook(d):
    if d['status'] == 'downloading':
        print(f"Downloading: {d['_percent_str']} of {d['_total_bytes_str']}")
    elif d['status'] == 'finished':
        print("Download complete, now converting...")

def get_output_component(download_video):
    if download_video:
        return gr.File(label="Downloaded Media")
    else:
        return gr.Audio(label="Downloaded Media")

# Create the Gradio interface
with gr.Blocks(theme=gr.themes.Soft(primary_hue="orange", secondary_hue="orange")) as demo:
    gr.Markdown(f"# <div style='text-align: center;'>YOUTUBE Downloader</div>")
    gr.Markdown(f"## <div style='text-align: center;'>Download MP3/MP4 from YouTube URL</div>")
    
    with gr.Row():
        url_input = gr.Textbox(label="YouTube URL", placeholder="Enter YouTube video URL here")
    with gr.Row():
        download_video_checkbox = gr.Checkbox(label="Download Video", value=False)
    with gr.Row():    
        download_button = gr.Button("Download")
    with gr.Row():
        output_audio = gr.Audio(label="Downloaded Media", visible=False)
        output_file = gr.File(label="Downloaded Media", visible=False)

    def handle_download(url, download_video):
        output_file = download_media(url, download_video)
        if download_video:
            return gr.update(value=output_file, visible=True), gr.update(visible=False)
        else:
            return gr.update(visible=False), gr.update(value=output_file, visible=True)

    download_button.click(
        handle_download, 
        inputs=[url_input, download_video_checkbox], 
        outputs=[output_file, output_audio]
    )

    gr.Markdown(f"### <div style='text-align: center;'>Made with ❤ by <a href='https://huggingface.co/Gradio-Blocks'>Gradio-Blocks-Party-Member</a></div>")

demo.launch()