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()