Spaces:
Sleeping
Sleeping
File size: 1,049 Bytes
933dd53 e7e2b0a 933dd53 e7e2b0a 933dd53 e7e2b0a 933dd53 e7e2b0a 933dd53 71706d9 933dd53 |
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 |
import gradio as gr
import yt_dlp
def download_media(url, media_type):
ydl_opts = {
'format': 'bestaudio/best' if media_type == 'Audio' else 'bestvideo+bestaudio/best',
'outtmpl': '%(title)s.%(ext)s',
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
result = ydl.extract_info(url, download=True)
filename = ydl.prepare_filename(result)
return filename
def download_audio(url):
return download_media(url, 'Audio')
def download_video(url):
return download_media(url, 'Video')
with gr.Blocks() as demo:
gr.Markdown("# YouTube Audio/Video Downloader")
url_input = gr.Textbox(label="YouTube URL")
media_type = gr.Radio(label="Select Media Type", choices=["Audio", "Video"], value="Audio")
output = gr.Audio(label="Downloaded File")
download_button = gr.Button("Download")
download_button.click(
fn=lambda url, media: download_audio(url) if media == 'Audio' else download_video(url),
inputs=[url_input, media_type],
outputs=output
)
demo.launch()
|