hetchyy commited on
Commit
7990dde
·
1 Parent(s): b015efb

Compress logging audio format

Browse files
Files changed (1) hide show
  1. src/core/usage_logger.py +19 -6
src/core/usage_logger.py CHANGED
@@ -299,16 +299,29 @@ def _compute_audio_id(audio: np.ndarray, ts: datetime) -> str:
299
  return f"{audio_hash}:{ts.strftime('%Y%m%dT%H%M%S')}"
300
 
301
 
302
- def _encode_audio_flac(audio: np.ndarray, sample_rate: int, audio_id: str) -> str:
303
- """Encode audio to a temp FLAC file; returns the file path."""
304
  import soundfile as sf
 
305
 
306
  tmp_dir = LOG_DIR / "tmp_audio"
307
  tmp_dir.mkdir(parents=True, exist_ok=True)
308
  safe_id = audio_id.replace(":", "-")
309
- filepath = tmp_dir / f"{safe_id}.flac"
310
- sf.write(str(filepath), audio, sample_rate, format="FLAC")
311
- return str(filepath)
 
 
 
 
 
 
 
 
 
 
 
 
312
 
313
 
314
  def _sync_row_to_scheduler(row: Dict[str, Any]) -> None:
@@ -461,7 +474,7 @@ def log_alignment(
461
  def _encode_and_append():
462
  """Encode FLAC and register row with scheduler/fallback."""
463
  try:
464
- row["audio"] = _encode_audio_flac(audio, sample_rate, audio_id)
465
  except Exception as e:
466
  print(f"[USAGE_LOG] FLAC encoding failed: {e}")
467
  if _aligner_scheduler is None:
 
299
  return f"{audio_hash}:{ts.strftime('%Y%m%dT%H%M%S')}"
300
 
301
 
302
+ def _encode_audio_ogg(audio: np.ndarray, sample_rate: int, audio_id: str) -> str:
303
+ """Encode audio to a temp OGG Opus file (32kbps); returns the file path."""
304
  import soundfile as sf
305
+ import subprocess
306
 
307
  tmp_dir = LOG_DIR / "tmp_audio"
308
  tmp_dir.mkdir(parents=True, exist_ok=True)
309
  safe_id = audio_id.replace(":", "-")
310
+
311
+ wav_path = tmp_dir / f"{safe_id}.wav"
312
+ ogg_path = tmp_dir / f"{safe_id}.ogg"
313
+ sf.write(str(wav_path), audio, sample_rate, format="WAV")
314
+ try:
315
+ subprocess.run(
316
+ ["ffmpeg", "-y", "-i", str(wav_path),
317
+ "-c:a", "libopus", "-b:a", "32k",
318
+ "-ar", "16000", "-ac", "1",
319
+ str(ogg_path)],
320
+ capture_output=True, check=True,
321
+ )
322
+ finally:
323
+ wav_path.unlink(missing_ok=True)
324
+ return str(ogg_path)
325
 
326
 
327
  def _sync_row_to_scheduler(row: Dict[str, Any]) -> None:
 
474
  def _encode_and_append():
475
  """Encode FLAC and register row with scheduler/fallback."""
476
  try:
477
+ row["audio"] = _encode_audio_ogg(audio, sample_rate, audio_id)
478
  except Exception as e:
479
  print(f"[USAGE_LOG] FLAC encoding failed: {e}")
480
  if _aligner_scheduler is None: