MySafeCode commited on
Commit
66a97e7
·
verified ·
1 Parent(s): 06925e6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -38
app.py CHANGED
@@ -1,49 +1,53 @@
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
 
 
 
 
 
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