Spaces:
Running
Running
import gradio as gr | |
import yt_dlp | |
import os | |
def download_media(url, output_format): | |
ydl_opts = {} | |
if output_format == "Audio": | |
ydl_opts = { | |
'format': 'bestaudio/best', | |
'outtmpl': 'downloads/%(title)s.%(ext)s', | |
'postprocessors': [{ | |
'key': 'FFmpegExtractAudio', | |
'preferredcodec': 'mp3', | |
'preferredquality': '192', | |
}], | |
} | |
elif output_format == "Video": | |
ydl_opts = { | |
'format': 'bestvideo+bestaudio/best', | |
'outtmpl': 'downloads/%(title)s.%(ext)s', | |
} | |
try: | |
with yt_dlp.YoutubeDL(ydl_opts) as ydl: | |
ydl.download([url]) | |
return "Download successful!" | |
except Exception as e: | |
return f"Error: {str(e)}" | |
# Gradio Blocks UI | |
with gr.Blocks() as demo: | |
gr.Markdown("# Video and Audio Downloader using yt-dlp") | |
with gr.Row(): | |
url_input = gr.Textbox(label="Video/Audio URL") | |
format_dropdown = gr.Dropdown(choices=["Audio", "Video"], label="Output Format") | |
download_button = gr.Button("Download") | |
output_text = gr.Textbox(label="Output Message") | |
download_button.click(download_media, inputs=[url_input, format_dropdown], outputs=output_text) | |
demo.launch() | |