Hev832's picture
Update app.py
61941bf verified
raw
history blame
1.86 kB
import gradio as gr
import yt_dlp
def download_media(url, download_video):
if download_video:
ydl_opts = {
'format': 'bestvideo+bestaudio/best',
'outtmpl': 'downloads/%(title)s.%(ext)s',
}
else:
ydl_opts = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
'outtmpl': 'downloads/%(title)s.%(ext)s',
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info_dict = ydl.extract_info(url, download=True)
file_title = ydl.prepare_filename(info_dict)
if download_video:
output_file = file_title
else:
output_file = file_title.rsplit('.', 1)[0] + '.mp3'
return output_file, download_video
def get_output_component(download_video):
if download_video:
return gr.File(label="Downloaded Media")
else:
return gr.Audio(label="Downloaded Media")
# Create the Gradio interface
with gr.Blocks() as demo:
gr.Markdown("# YouTube Media Downloader")
with gr.Row():
url_input = gr.Textbox(label="YouTube URL")
download_video_checkbox = gr.Checkbox(label="Download Video", value=False)
with gr.Row():
download_button = gr.Button("Download")
with gr.Row():
dynamic_output = gr.State()
def update_output_component(download_video):
return get_output_component(download_video)
dynamic_output = download_video_checkbox.change(
update_output_component, inputs=download_video_checkbox, outputs=dynamic_output
)
download_button.click(download_media, inputs=[url_input, download_video_checkbox], outputs=[dynamic_output, download_video_checkbox])
demo.launch()