File size: 1,461 Bytes
5eb2405
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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):
    # Download best audio first (usually m4a)
    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 user wants MP3 and it isn't already mp3, convert
    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
    
    # If user chose the original format, just return it
    return original_file

# Gradio interface
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()