Spaces:
Running
Running
truncate long files to fit cap, show which files truncated/skipped
Browse files
app.py
CHANGED
|
@@ -490,22 +490,42 @@ def gradio_main():
|
|
| 490 |
import librosa as _lr
|
| 491 |
total_dur = 0.0
|
| 492 |
accepted = 0
|
| 493 |
-
|
|
|
|
| 494 |
for f in audio_files:
|
| 495 |
src = f.name if hasattr(f, "name") else str(f)
|
|
|
|
| 496 |
try:
|
| 497 |
dur = _lr.get_duration(path=src)
|
| 498 |
except Exception:
|
| 499 |
dur = 0.0
|
| 500 |
-
|
| 501 |
-
|
|
|
|
| 502 |
continue
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 509 |
_log(f"[INFO] Total audio: {total_dur:.0f}s ({total_dur/60:.1f} min), {accepted} files")
|
| 510 |
|
| 511 |
_log(f"[INFO] LoRA: '{lora_name}' | Files: {len(audio_files)} | "
|
|
|
|
| 490 |
import librosa as _lr
|
| 491 |
total_dur = 0.0
|
| 492 |
accepted = 0
|
| 493 |
+
skipped_names = []
|
| 494 |
+
truncated_names = []
|
| 495 |
for f in audio_files:
|
| 496 |
src = f.name if hasattr(f, "name") else str(f)
|
| 497 |
+
fname = os.path.basename(src)
|
| 498 |
try:
|
| 499 |
dur = _lr.get_duration(path=src)
|
| 500 |
except Exception:
|
| 501 |
dur = 0.0
|
| 502 |
+
remaining = MAX_TOTAL_AUDIO - total_dur
|
| 503 |
+
if remaining <= 0:
|
| 504 |
+
skipped_names.append(fname)
|
| 505 |
continue
|
| 506 |
+
if dur > remaining:
|
| 507 |
+
# Truncate this file to fit
|
| 508 |
+
import soundfile as _sf
|
| 509 |
+
y, sr = _lr.load(src, sr=None, mono=False)
|
| 510 |
+
max_samples = int(remaining * sr)
|
| 511 |
+
if y.ndim == 1:
|
| 512 |
+
y = y[:max_samples]
|
| 513 |
+
else:
|
| 514 |
+
y = y[:, :max_samples]
|
| 515 |
+
dst = os.path.join(audio_dir, fname)
|
| 516 |
+
_sf.write(dst, y.T if y.ndim > 1 else y, sr)
|
| 517 |
+
truncated_names.append(f"{fname} ({dur:.0f}s -> {remaining:.0f}s)")
|
| 518 |
+
total_dur += remaining
|
| 519 |
+
accepted += 1
|
| 520 |
+
else:
|
| 521 |
+
shutil.copy2(src, os.path.join(audio_dir, fname))
|
| 522 |
+
total_dur += dur
|
| 523 |
+
accepted += 1
|
| 524 |
+
|
| 525 |
+
if truncated_names:
|
| 526 |
+
_log(f"[WARN] Truncated: {', '.join(truncated_names)}")
|
| 527 |
+
if skipped_names:
|
| 528 |
+
_log(f"[WARN] Skipped (over {MAX_TOTAL_AUDIO/60:.0f} min cap): {', '.join(skipped_names)}")
|
| 529 |
_log(f"[INFO] Total audio: {total_dur:.0f}s ({total_dur/60:.1f} min), {accepted} files")
|
| 530 |
|
| 531 |
_log(f"[INFO] LoRA: '{lora_name}' | Files: {len(audio_files)} | "
|