File size: 1,901 Bytes
e17a939
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17a3316
e17a939
 
 
 
 
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
import os
import gradio as gr
import yt_dlp

def download_media(url, download_video):
    download_dir = 'downloads'
    os.makedirs(download_dir, exist_ok=True)
    
    if download_video:
        ydl_opts = {
            'format': 'bestvideo+bestaudio/best',
            'outtmpl': f'{download_dir}/%(title)s.%(ext)s',
        }
    else:
        ydl_opts = {
            'format': 'bestaudio/best',
            'postprocessors': [{
                'key': 'FFmpegExtractAudio',
                'preferredcodec': 'mp3',
                'preferredquality': '192',
            }],
            'outtmpl': f'{download_dir}/%(title)s.%(ext)s',
        }

    with yt_dlp.YoutubeDL(ydl_opts) as ydl:
        info_dict = ydl.extract_info(url, download=True)
        file_title = ydl.prepare_filename(info_dict)

        if download_video:
            output_file = file_title
        else:
            output_file = file_title.rsplit('.', 1)[0] + '.mp3'

    return output_file

def download_handler(url, download_video):
    if url:
        output_file = download_media(url, download_video)
        return "Download completed!", output_file
    else:
        return "Please enter a YouTube URL", None

with gr.Blocks() as demo:
    gr.Markdown("# Download From YOUTUBE")
    gr.Markdown("## Download MP3/MP4 from YouTube URL")
    
    url = gr.Textbox(label="YouTube URL")
    download_video = gr.Checkbox(label="Download Video", value=False)
    download_btn = gr.Button("Download")
    
    output_text = gr.Markdown()
    output_file = gr.File()

    download_btn.click(download_handler, inputs=[url, download_video], outputs=[output_text, output_file])


        gr.Image("profile.jpg", label="Profile Image", width=200)
        gr.Markdown("Build AI with Haseeb")
        gr.Markdown("### Made with ❤ by [Haseeb Ahmed](https://www.linkedin.com/in/muhammad-haseeb-ahmed-1954b5230/)")

demo.launch()