Update app.py
Browse files
app.py
CHANGED
|
@@ -13,17 +13,21 @@ def process_song():
|
|
| 13 |
return jsonify({"error": "Please provide a song link with ?song=<url>"}), 400
|
| 14 |
|
| 15 |
try:
|
| 16 |
-
# Download audio file
|
| 17 |
-
|
|
|
|
| 18 |
resp.raise_for_status()
|
| 19 |
|
| 20 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
sound = AudioSegment.from_file(io.BytesIO(resp.content), format="mp3")
|
| 22 |
sound = effects.normalize(sound)
|
| 23 |
|
| 24 |
# Split channels
|
| 25 |
-
|
| 26 |
-
left, right = channels[0], channels[1]
|
| 27 |
|
| 28 |
# Empty output
|
| 29 |
output = AudioSegment.silent(duration=len(sound), frame_rate=sound.frame_rate)
|
|
@@ -50,15 +54,15 @@ def process_song():
|
|
| 50 |
stereo_chunk = AudioSegment.from_mono_audiosegments(left_chunk, right_chunk)
|
| 51 |
output += stereo_chunk
|
| 52 |
|
| 53 |
-
#
|
| 54 |
widened = output.overlay(output.pan(-0.5), gain_during_overlay=-6)
|
| 55 |
widened = widened.overlay(output.pan(0.5), gain_during_overlay=-6)
|
| 56 |
|
| 57 |
-
#
|
| 58 |
reverb = widened.overlay(widened - 8, delay=reverb_delay)
|
| 59 |
reverb = reverb.overlay(widened - 14, delay=reverb_delay * 2)
|
| 60 |
|
| 61 |
-
#
|
| 62 |
final = effects.normalize(reverb)
|
| 63 |
|
| 64 |
# Export to memory
|
|
@@ -66,7 +70,12 @@ def process_song():
|
|
| 66 |
final.export(buf, format="mp3")
|
| 67 |
buf.seek(0)
|
| 68 |
|
| 69 |
-
return send_file(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
except Exception as e:
|
| 72 |
return jsonify({"error": str(e)}), 500
|
|
|
|
| 13 |
return jsonify({"error": "Please provide a song link with ?song=<url>"}), 400
|
| 14 |
|
| 15 |
try:
|
| 16 |
+
# Download audio file with headers & redirects
|
| 17 |
+
headers = {"User-Agent": "Mozilla/5.0"}
|
| 18 |
+
resp = requests.get(url, stream=True, headers=headers, allow_redirects=True, timeout=30)
|
| 19 |
resp.raise_for_status()
|
| 20 |
|
| 21 |
+
# Check if it's really audio
|
| 22 |
+
if "audio" not in resp.headers.get("Content-Type", ""):
|
| 23 |
+
return jsonify({"error": f"URL did not return audio, got {resp.headers.get('Content-Type')}"}), 400
|
| 24 |
+
|
| 25 |
+
# Load into pydub
|
| 26 |
sound = AudioSegment.from_file(io.BytesIO(resp.content), format="mp3")
|
| 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)
|
|
|
|
| 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
|
|
|
|
| 70 |
final.export(buf, format="mp3")
|
| 71 |
buf.seek(0)
|
| 72 |
|
| 73 |
+
return send_file(
|
| 74 |
+
buf,
|
| 75 |
+
mimetype="audio/mpeg",
|
| 76 |
+
as_attachment=True,
|
| 77 |
+
download_name="panned_heavenly.mp3"
|
| 78 |
+
)
|
| 79 |
|
| 80 |
except Exception as e:
|
| 81 |
return jsonify({"error": str(e)}), 500
|