Hev832's picture
Update run.py
c0ce2a0 verified
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"<h1><center> Media Downloader")
gr.Markdown("please `❤` this spaces 🤗 if helpfull")
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()