| import gradio as gr |
| from yt_dlp import YoutubeDL |
| import os |
| from pydub import AudioSegment |
|
|
| DOWNLOADS_FOLDER = "downloads" |
| os.makedirs(DOWNLOADS_FOLDER, exist_ok=True) |
|
|
| def download_soundcloud(url, file_format): |
| |
| ydl_opts = { |
| 'format': 'bestaudio/best', |
| 'outtmpl': os.path.join(DOWNLOADS_FOLDER, '%(title)s.%(ext)s') |
| } |
| |
| with YoutubeDL(ydl_opts) as ydl: |
| info = ydl.extract_info(url, download=True) |
| |
| original_file = os.path.join(DOWNLOADS_FOLDER, f"{info['title']}.{info['ext']}") |
| |
| |
| if file_format.lower() == "mp3" and not original_file.endswith(".mp3"): |
| mp3_file = os.path.splitext(original_file)[0] + ".mp3" |
| AudioSegment.from_file(original_file).export(mp3_file, format="mp3") |
| return mp3_file |
| |
| |
| return original_file |
|
|
| |
| with gr.Blocks() as iface: |
| url_input = gr.Textbox(label="SoundCloud URL") |
| format_choice = gr.Dropdown(choices=["mp3", "m4a", "opus"], value="mp3", label="Select format") |
| download_button = gr.Button("Download") |
| download_file = gr.File(label="Download your track") |
| |
| download_button.click( |
| fn=download_soundcloud, |
| inputs=[url_input, format_choice], |
| outputs=download_file |
| ) |
|
|
| iface.launch(show_error=True ) |
|
|