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

Update app.py

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