Update app.py
Browse files
app.py
CHANGED
|
@@ -13,26 +13,33 @@ def process_song():
|
|
| 13 |
return jsonify({"error": "Please provide a song link with ?song=<url>"}), 400
|
| 14 |
|
| 15 |
try:
|
| 16 |
-
# Download audio
|
| 17 |
headers = {"User-Agent": "Mozilla/5.0"}
|
| 18 |
-
resp = requests.get(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
resp.raise_for_status()
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
# Load into pydub
|
| 26 |
-
sound = AudioSegment.from_file(
|
| 27 |
sound = effects.normalize(sound)
|
| 28 |
|
| 29 |
-
# Split channels
|
| 30 |
left, right = sound.split_to_mono()
|
| 31 |
-
|
| 32 |
-
# Empty output
|
| 33 |
output = AudioSegment.silent(duration=len(sound), frame_rate=sound.frame_rate)
|
| 34 |
|
| 35 |
-
#
|
| 36 |
chunk_ms = 10
|
| 37 |
pan_period = 6.0
|
| 38 |
base_depth = 0.7
|
|
@@ -54,18 +61,18 @@ def process_song():
|
|
| 54 |
stereo_chunk = AudioSegment.from_mono_audiosegments(left_chunk, right_chunk)
|
| 55 |
output += stereo_chunk
|
| 56 |
|
| 57 |
-
# Stereo widening
|
| 58 |
widened = output.overlay(output.pan(-0.5), gain_during_overlay=-6)
|
| 59 |
widened = widened.overlay(output.pan(0.5), gain_during_overlay=-6)
|
| 60 |
|
| 61 |
-
# Subtle reverb
|
| 62 |
reverb = widened.overlay(widened - 8, delay=reverb_delay)
|
| 63 |
reverb = reverb.overlay(widened - 14, delay=reverb_delay * 2)
|
| 64 |
|
| 65 |
-
# Final mastering
|
| 66 |
final = effects.normalize(reverb)
|
| 67 |
|
| 68 |
-
# Export to memory
|
| 69 |
buf = io.BytesIO()
|
| 70 |
final.export(buf, format="mp3")
|
| 71 |
buf.seek(0)
|
|
|
|
| 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,
|
| 20 |
+
stream=True,
|
| 21 |
+
headers=headers,
|
| 22 |
+
allow_redirects=True,
|
| 23 |
+
timeout=30
|
| 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 ---
|
| 39 |
left, right = sound.split_to_mono()
|
|
|
|
|
|
|
| 40 |
output = AudioSegment.silent(duration=len(sound), frame_rate=sound.frame_rate)
|
| 41 |
|
| 42 |
+
# --- Pan & effect settings ---
|
| 43 |
chunk_ms = 10
|
| 44 |
pan_period = 6.0
|
| 45 |
base_depth = 0.7
|
|
|
|
| 61 |
stereo_chunk = AudioSegment.from_mono_audiosegments(left_chunk, right_chunk)
|
| 62 |
output += stereo_chunk
|
| 63 |
|
| 64 |
+
# --- Stereo widening ---
|
| 65 |
widened = output.overlay(output.pan(-0.5), gain_during_overlay=-6)
|
| 66 |
widened = widened.overlay(output.pan(0.5), gain_during_overlay=-6)
|
| 67 |
|
| 68 |
+
# --- Subtle reverb ---
|
| 69 |
reverb = widened.overlay(widened - 8, delay=reverb_delay)
|
| 70 |
reverb = reverb.overlay(widened - 14, delay=reverb_delay * 2)
|
| 71 |
|
| 72 |
+
# --- Final mastering ---
|
| 73 |
final = effects.normalize(reverb)
|
| 74 |
|
| 75 |
+
# --- Export to memory ---
|
| 76 |
buf = io.BytesIO()
|
| 77 |
final.export(buf, format="mp3")
|
| 78 |
buf.seek(0)
|