Spaces:
Sleeping

Rthur2003 commited on
Commit
14fc9a2
·
1 Parent(s): c255068

fix: add ffmpeg decoding fallback for unsupported audio formats in feature extraction

Browse files
Files changed (1) hide show
  1. app/services/feature_extractor.py +42 -6
app/services/feature_extractor.py CHANGED
@@ -9,6 +9,8 @@ music from human-composed music.
9
  from __future__ import annotations
10
 
11
  import io
 
 
12
  from dataclasses import dataclass
13
  from pathlib import Path
14
  from typing import Optional, Union
@@ -183,6 +185,26 @@ def extract_meta(source: Union[Path, bytes, io.BytesIO]) -> AudioMeta:
183
  # PRIVATE — Audio loading
184
  # ═══════════════════════════════════════════════════════════════════════
185
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
186
  def _load_audio(
187
  source: Union[Path, bytes, io.BytesIO],
188
  target_sr: int,
@@ -191,12 +213,26 @@ def _load_audio(
191
  if isinstance(source, bytes):
192
  source = io.BytesIO(source)
193
 
194
- y, sr = librosa.load(
195
- source if isinstance(source, (str, Path, io.BytesIO)) else str(source),
196
- sr=target_sr,
197
- mono=True,
198
- duration=_DURATION_LIMIT,
199
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
200
 
201
  # Guard against silent / corrupt files
202
  if len(y) < sr:
 
9
  from __future__ import annotations
10
 
11
  import io
12
+ import subprocess
13
+ import tempfile
14
  from dataclasses import dataclass
15
  from pathlib import Path
16
  from typing import Optional, Union
 
185
  # PRIVATE — Audio loading
186
  # ═══════════════════════════════════════════════════════════════════════
187
 
188
+ def _ffmpeg_decode(data: bytes) -> io.BytesIO:
189
+ """Decode any audio format (webm, opus, ogg, etc.) to WAV via ffmpeg."""
190
+ with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp:
191
+ tmp_path = tmp.name
192
+ try:
193
+ result = subprocess.run(
194
+ ["ffmpeg", "-y", "-i", "pipe:0", "-ar", "22050", "-ac", "1",
195
+ "-f", "wav", tmp_path],
196
+ input=data,
197
+ capture_output=True,
198
+ timeout=30,
199
+ )
200
+ if result.returncode != 0:
201
+ raise RuntimeError(f"ffmpeg failed: {result.stderr.decode()[:200]}")
202
+ with open(tmp_path, "rb") as f:
203
+ return io.BytesIO(f.read())
204
+ finally:
205
+ Path(tmp_path).unlink(missing_ok=True)
206
+
207
+
208
  def _load_audio(
209
  source: Union[Path, bytes, io.BytesIO],
210
  target_sr: int,
 
213
  if isinstance(source, bytes):
214
  source = io.BytesIO(source)
215
 
216
+ # Read bytes for potential ffmpeg fallback
217
+ if isinstance(source, io.BytesIO):
218
+ raw_bytes = source.read()
219
+ source = io.BytesIO(raw_bytes)
220
+ else:
221
+ raw_bytes = None
222
+
223
+ try:
224
+ y, sr = librosa.load(
225
+ source if isinstance(source, (str, Path, io.BytesIO)) else str(source),
226
+ sr=target_sr,
227
+ mono=True,
228
+ duration=_DURATION_LIMIT,
229
+ )
230
+ except Exception:
231
+ # soundfile can't read webm/opus/ogg — try ffmpeg decode to WAV
232
+ if raw_bytes is None:
233
+ raise
234
+ wav_buf = _ffmpeg_decode(raw_bytes)
235
+ y, sr = librosa.load(wav_buf, sr=target_sr, mono=True, duration=_DURATION_LIMIT)
236
 
237
  # Guard against silent / corrupt files
238
  if len(y) < sr: