Update app.py
Browse files
app.py
CHANGED
|
@@ -1,48 +1,49 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import subprocess
|
| 3 |
-
import tempfile
|
| 4 |
import yt_dlp
|
| 5 |
-
import
|
| 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 |
-
# Gradio
|
|
|
|
|
|
|
|
|
|
| 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,
|
| 44 |
-
output_audio = gr.Audio(
|
| 45 |
-
|
| 46 |
-
btn
|
|
|
|
| 47 |
|
| 48 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import yt_dlp
|
| 2 |
+
import ffmpeg
|
| 3 |
+
import io
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import requests
|
| 6 |
+
|
| 7 |
+
def get_snippet(url, duration_sec):
|
| 8 |
+
# Extract the direct audio URL (bypassing file download)
|
| 9 |
+
ydl_opts = {
|
| 10 |
+
"format": "bestaudio/best",
|
| 11 |
+
"quiet": True,
|
| 12 |
+
"no_warnings": True,
|
| 13 |
+
"skip_download": True,
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 17 |
+
info = ydl.extract_info(url, download=False)
|
| 18 |
+
audio_url = info['url']
|
| 19 |
+
|
| 20 |
+
# Download raw audio data
|
| 21 |
+
r = requests.get(audio_url, stream=True)
|
| 22 |
+
r.raise_for_status()
|
| 23 |
+
input_bytes = io.BytesIO(r.content)
|
| 24 |
+
|
| 25 |
+
# Cut the first N seconds with ffmpeg into memory
|
| 26 |
+
output_bytes = io.BytesIO()
|
| 27 |
+
(
|
| 28 |
+
ffmpeg
|
| 29 |
+
.input('pipe:0')
|
| 30 |
+
.output('pipe:1', format='mp3', t=duration_sec)
|
| 31 |
+
.run(input=input_bytes.read(), capture_stdout=True, capture_stderr=True)
|
| 32 |
+
)
|
| 33 |
+
# Note: ffmpeg.run returns bytes, so we wrap it in BytesIO
|
| 34 |
+
output_bytes = io.BytesIO(ffmpeg.run(input=input_bytes.read(), capture_stdout=True, capture_stderr=True))
|
| 35 |
+
|
| 36 |
+
# Return tuple: file object + filename (for download in Gradio)
|
| 37 |
+
output_bytes.name = "snippet.mp3"
|
| 38 |
+
return output_bytes
|
| 39 |
+
|
| 40 |
with gr.Blocks() as demo:
|
|
|
|
| 41 |
url_input = gr.Textbox(label="SoundCloud URL")
|
| 42 |
+
duration_slider = gr.Slider(1, 60, value=30, step=1, label="Snippet duration (sec)")
|
| 43 |
+
output_audio = gr.Audio(type="file", label="Playable & downloadable snippet")
|
| 44 |
+
|
| 45 |
+
btn = gr.Button("Generate Snippet")
|
| 46 |
+
btn.click(get_snippet, inputs=[url_input, duration_slider], outputs=[output_audio])
|
| 47 |
|
| 48 |
demo.launch()
|
| 49 |
+
|