Hev832's picture
Update run.py
6c7ed02 verified
raw
history blame
No virus
1.34 kB
import gradio as gr
import yt_dlp
import os
def downloader(video_url, audio_format, audio_name):
# Ensure the directory exists
os.makedirs('audios', exist_ok=True)
# Use a temporary placeholder for the output file
temp_output_path = f"audios/{audio_name}.%(ext)s"
ydl_opts = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': audio_format,
}],
'outtmpl': temp_output_path,
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
ydl.download([video_url])
# Find the downloaded file and rename it
temp_file = temp_output_path.replace('%(ext)s', audio_format)
final_output_path = f"audios/{audio_name}.{audio_format}"
os.rename(temp_file, final_output_path)
return final_output_path
with gr.Blocks() as demo:
gr.Markdown("# YouTube Downloader 2.0")
video_url = gr.Textbox(label="YouTube video link")
audio_name = gr.Textbox(label="Audio name of YouTube audio")
audio_format = gr.Radio(["wav", "flac", "mp3"], label="Select the output format")
output = gr.Audio(label="Output")
download_button = gr.Button("Download")
download_button.click(downloader, inputs=[video_url, audio_format, audio_name], outputs=output)
demo.launch()