import gradio as gr import subprocess import os def download_media(url): # Determine if the URL is for audio or video ydl_opts = { 'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best', # Prefer mp4 format 'outtmpl': 'downloaded_media.%(ext)s', # Output file name } # Download using yt-dlp subprocess.run(['yt-dlp', url, '--merge-output-format', 'mp4', '--recode-video', 'mp4']) # Check if downloaded file exists if os.path.exists('downloaded_media.mp4'): return gr.outputs.Video("downloaded_media.mp4") elif os.path.exists('downloaded_media.m4a'): return gr.outputs.Audio("downloaded_media.m4a") else: return "Media could not be downloaded or unsupported format." # Create Gradio interface iface = gr.Interface( fn=download_media, inputs="text", outputs="auto", title="YouTube Downloader", description="Enter a YouTube video URL to download its audio or video." ) # Launch the interface iface.launch()