|
|
import gradio as gr |
|
|
import subprocess |
|
|
import tempfile |
|
|
import yt_dlp |
|
|
import os |
|
|
|
|
|
def get_soundcloud_clip(url, duration): |
|
|
try: |
|
|
|
|
|
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"] |
|
|
|
|
|
|
|
|
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") |
|
|
temp_file.close() |
|
|
|
|
|
|
|
|
cmd = [ |
|
|
"ffmpeg", |
|
|
"-y", |
|
|
"-i", audio_url, |
|
|
"-t", str(duration), |
|
|
"-c", "copy", |
|
|
temp_file.name |
|
|
] |
|
|
subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
|
|
|
|
|
return temp_file.name |
|
|
|
|
|
except Exception as e: |
|
|
return f"Error: {e}" |
|
|
|
|
|
|
|
|
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() |
|
|
|