Update app.py
Browse files
app.py
CHANGED
|
@@ -1,49 +1,53 @@
|
|
| 1 |
-
import yt_dlp
|
| 2 |
-
import ffmpeg
|
| 3 |
-
import io
|
| 4 |
import gradio as gr
|
| 5 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
def get_snippet(url, duration_sec):
|
| 8 |
-
# Extract the direct audio URL (bypassing file download)
|
| 9 |
ydl_opts = {
|
| 10 |
-
"format": "bestaudio
|
|
|
|
|
|
|
|
|
|
| 11 |
"quiet": True,
|
| 12 |
"no_warnings": True,
|
| 13 |
-
"skip_download": True,
|
| 14 |
}
|
| 15 |
|
| 16 |
-
with
|
| 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 |
-
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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from yt_dlp import YoutubeDL
|
| 3 |
+
import tempfile
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
def download_snippet(url, duration_sec):
|
| 7 |
+
# Temporary output file
|
| 8 |
+
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
| 9 |
+
output_path = tmp.name
|
| 10 |
+
tmp.close()
|
| 11 |
|
|
|
|
|
|
|
| 12 |
ydl_opts = {
|
| 13 |
+
"format": "bestaudio",
|
| 14 |
+
"outtmpl": output_path,
|
| 15 |
+
"external_downloader": "ffmpeg",
|
| 16 |
+
"external_downloader_args": ["-t", str(int(duration_sec))],
|
| 17 |
"quiet": True,
|
| 18 |
"no_warnings": True,
|
|
|
|
| 19 |
}
|
| 20 |
|
| 21 |
+
with YoutubeDL(ydl_opts) as ydl:
|
| 22 |
+
ydl.download([url])
|
| 23 |
+
|
| 24 |
+
return output_path
|
| 25 |
+
|
| 26 |
+
with gr.Blocks(title="SoundCloud 30s Stream Snippet") as demo:
|
| 27 |
+
gr.Markdown("## 🎵 SoundCloud Snippet Generator")
|
| 28 |
+
gr.Markdown("Downloads only the first *N seconds* using yt-dlp + ffmpeg.")
|
| 29 |
+
|
| 30 |
+
url = gr.Textbox(
|
| 31 |
+
label="SoundCloud URL",
|
| 32 |
+
value="https://soundcloud.com/antonio-antetomaso/mutiny-on-the-bounty-closing-titles-cover"
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
duration = gr.Slider(
|
| 36 |
+
minimum=1,
|
| 37 |
+
maximum=300,
|
| 38 |
+
value=30,
|
| 39 |
+
step=1,
|
| 40 |
+
label="Snippet length (seconds)"
|
| 41 |
)
|
| 42 |
+
|
| 43 |
+
btn = gr.Button("Generate snippet")
|
| 44 |
+
|
| 45 |
+
audio = gr.Audio(
|
| 46 |
+
type="filepath",
|
| 47 |
+
label="Preview & download"
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
btn.click(download_snippet, inputs=[url, duration], outputs=audio)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
demo.launch()
|
| 53 |
|