Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import subprocess
|
| 3 |
+
import tempfile
|
| 4 |
+
import yt_dlp
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
def get_soundcloud_clip(url, duration):
|
| 8 |
+
try:
|
| 9 |
+
# Get the direct audio URL from SoundCloud
|
| 10 |
+
ydl_opts = {
|
| 11 |
+
"format": "bestaudio/best",
|
| 12 |
+
"quiet": True,
|
| 13 |
+
"skip_download": True,
|
| 14 |
+
}
|
| 15 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 16 |
+
info = ydl.extract_info(url, download=False)
|
| 17 |
+
audio_url = info["url"]
|
| 18 |
+
|
| 19 |
+
# Create a temporary file for the clipped audio
|
| 20 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
| 21 |
+
temp_file.close()
|
| 22 |
+
|
| 23 |
+
# Use ffmpeg to download and trim the audio
|
| 24 |
+
cmd = [
|
| 25 |
+
"ffmpeg",
|
| 26 |
+
"-y",
|
| 27 |
+
"-i", audio_url,
|
| 28 |
+
"-t", str(duration),
|
| 29 |
+
"-c", "copy", # keeps original AAC without re-encoding
|
| 30 |
+
temp_file.name
|
| 31 |
+
]
|
| 32 |
+
subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
| 33 |
+
|
| 34 |
+
return temp_file.name
|
| 35 |
+
|
| 36 |
+
except Exception as e:
|
| 37 |
+
return f"Error: {e}"
|
| 38 |
+
|
| 39 |
+
# Gradio UI
|
| 40 |
+
with gr.Blocks() as demo:
|
| 41 |
+
gr.Markdown("## SoundCloud 30-second Clip Cutter")
|
| 42 |
+
url_input = gr.Textbox(label="SoundCloud URL")
|
| 43 |
+
duration_slider = gr.Slider(1, 300, value=30, step=1, label="Clip Duration (seconds)")
|
| 44 |
+
output_audio = gr.Audio(label="Clipped Audio", type="filepath")
|
| 45 |
+
btn = gr.Button("Get Clip")
|
| 46 |
+
btn.click(get_soundcloud_clip, inputs=[url_input, duration_slider], outputs=[output_audio])
|
| 47 |
+
|
| 48 |
+
demo.launch()
|