File size: 1,324 Bytes
f7b3397
91243d9
da8f353
087c136
cd512fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62bd18f
cd512fa
 
 
 
91243d9
cd512fa
051b0bd
 
25b6050
cd512fa
 
 
 
 
 
 
 
 
80cfb0f
051b0bd
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
import gradio as gr
import yt_dlp
import os

def download_media(url, media_type):
    ydl_opts = {
        'format': 'bestaudio/best' if media_type == 'audio' else 'bestvideo+bestaudio',
        'outtmpl': 'downloads/%(title)s.%(ext)s',
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '192',
        }] if media_type == 'audio' else []
    }
    
    with yt_dlp.YoutubeDL(ydl_opts) as ydl:
        info = ydl.extract_info(url, download=True)
        file_path = ydl.prepare_filename(info)
        
        if media_type == 'audio':
            file_path = file_path.rsplit('.', 1)[0] + '.mp3'
    
    return file_path

def show_media(url, media_type):
    file_path = download_media(url, media_type)
    if media_type == 'audio':
        return None, file_path
    else:
        return file_path, None

with gr.Blocks() as demo:
    gr.Markdown(f"<h1><center> Media Downloader")
    
    url = gr.Textbox(label="YouTube URL")
    media_type = gr.Radio(label="Media Type", choices=["audio", "video"])
    download_button = gr.Button("Download")
    
    video_output = gr.Video()
    audio_output = gr.Audio()
    
    download_button.click(show_media, inputs=[url, media_type], outputs=[video_output, audio_output])

demo.launch()