import gradio as gr import yt_dlp import os os.system("pip yt_dlp -U") 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"