File size: 1,500 Bytes
46c1af9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
45
46
47
48
49
import gradio as gr
import subprocess
import tempfile
import yt_dlp
import os

def get_soundcloud_clip(url, duration):
    try:
        # Get the direct audio URL from SoundCloud
        ydl_opts = {
            "format": "bestaudio/best",
            "quiet": True,
            "skip_download": True,
        }
        with yt_dlp.YoutubeDL(ydl_opts) as ydl:
            info = ydl.extract_info(url, download=False)
            audio_url = info["url"]

        # Create a temporary file for the clipped audio
        temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
        temp_file.close()

        # Use ffmpeg to download and trim the audio
        cmd = [
            "ffmpeg",
            "-y",
            "-i", audio_url,
            "-t", str(duration),
            "-c", "copy",  # keeps original AAC without re-encoding
            temp_file.name
        ]
        subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

        return temp_file.name

    except Exception as e:
        return f"Error: {e}"

# Gradio UI
with gr.Blocks() as demo:
    gr.Markdown("## SoundCloud 30-second Clip Cutter")
    url_input = gr.Textbox(label="SoundCloud URL")
    duration_slider = gr.Slider(1, 300, value=30, step=1, label="Clip Duration (seconds)")
    output_audio = gr.Audio(label="Clipped Audio", type="filepath")
    btn = gr.Button("Get Clip")
    btn.click(get_soundcloud_clip, inputs=[url_input, duration_slider], outputs=[output_audio])

demo.launch()