Reaperxxxx commited on
Commit
f1bf62d
·
verified ·
1 Parent(s): fd2c7bf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -6
app.py CHANGED
@@ -3,6 +3,7 @@ from pydub import AudioSegment, effects
3
  import numpy as np
4
  import requests
5
  import io
 
6
 
7
  app = Flask(__name__)
8
 
@@ -10,10 +11,10 @@ app = Flask(__name__)
10
  def process_song():
11
  url = request.args.get("song")
12
  if not url:
13
- return jsonify({"error": "Please provide a song link with ?song=<url>"}), 400
14
-
15
  try:
16
- # --- Download audio with safe buffering ---
17
  headers = {"User-Agent": "Mozilla/5.0"}
18
  resp = requests.get(
19
  url,
@@ -24,15 +25,25 @@ def process_song():
24
  )
25
  resp.raise_for_status()
26
 
27
- # Collect into BytesIO (avoid truncated MP3)
28
  data = io.BytesIO()
29
  for chunk in resp.iter_content(chunk_size=1024 * 1024):
30
  if chunk:
31
  data.write(chunk)
32
  data.seek(0)
33
 
34
- # --- Load into pydub ---
35
- sound = AudioSegment.from_file(data, format="mp3")
 
 
 
 
 
 
 
 
 
 
36
  sound = effects.normalize(sound)
37
 
38
  # --- Split channels ---
 
3
  import numpy as np
4
  import requests
5
  import io
6
+ import os
7
 
8
  app = Flask(__name__)
9
 
 
11
  def process_song():
12
  url = request.args.get("song")
13
  if not url:
14
+ return jsonify({"error": "Please provide a song link with ?song=<url>"})
15
+
16
  try:
17
+ # --- Download audio ---
18
  headers = {"User-Agent": "Mozilla/5.0"}
19
  resp = requests.get(
20
  url,
 
25
  )
26
  resp.raise_for_status()
27
 
28
+ # Buffer to BytesIO
29
  data = io.BytesIO()
30
  for chunk in resp.iter_content(chunk_size=1024 * 1024):
31
  if chunk:
32
  data.write(chunk)
33
  data.seek(0)
34
 
35
+ # --- Detect format from URL extension ---
36
+ ext = os.path.splitext(url.split("?")[0])[-1].lstrip(".").lower()
37
+ fmt = ext if ext in ["mp3", "wav", "flac", "ogg", "m4a"] else None
38
+
39
+ # --- Load with auto-detect ---
40
+ try:
41
+ sound = AudioSegment.from_file(data, format=fmt)
42
+ except Exception:
43
+ data.seek(0) # retry without forcing format
44
+ sound = AudioSegment.from_file(data)
45
+
46
+ # --- Normalize ---
47
  sound = effects.normalize(sound)
48
 
49
  # --- Split channels ---