Spaces:
Running on Zero

File size: 2,220 Bytes
bf90f9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import tempfile
from pathlib import Path
from app.audio.ffmpeg import run_ffmpeg
from app.utils.errors import AppError

def try_repair_audio(input_path: Path, repaired_path: Path) -> bool:
    """Attempts lossless container/moov repair on truncated or stream-corrupted audio files."""
    args = [
        "-y",
        "-err_detect", "ignore_err",
        "-i", str(input_path),
        "-c", "copy",
        "-movflags", "faststart",
        str(repaired_path)
    ]
    proc = run_ffmpeg(args, timeout=30)
    return proc.returncode == 0 and repaired_path.exists() and repaired_path.stat().st_size > 0

def normalize_to_wav(input_path: Path, output_wav_path: Path) -> Path:
    """
    Normalizes input audio to 16 kHz mono 16-bit PCM WAV without destructive
    spectral filtering or dynamic compression, preserving full vocal energy
    for Silero VAD and Faster-Whisper.
    Includes automated container repair fallback for truncated or damaged headers.
    """
    args = [
        "-y",
        "-i", str(input_path),
        "-ar", "16000",
        "-ac", "1",
        "-c:a", "pcm_s16le",
        str(output_wav_path)
    ]
    proc = run_ffmpeg(args, timeout=60)
    if proc.returncode != 0:
        # Attempt container auto-repair before failing
        with tempfile.NamedTemporaryFile(suffix=input_path.suffix, delete=False) as tmp_repair:
            repair_path = Path(tmp_repair.name)
        try:
            if try_repair_audio(input_path, repair_path):
                repair_args = [
                    "-y",
                    "-i", str(repair_path),
                    "-ar", "16000",
                    "-ac", "1",
                    "-c:a", "pcm_s16le",
                    str(output_wav_path)
                ]
                proc2 = run_ffmpeg(repair_args, timeout=60)
                if proc2.returncode == 0:
                    return output_wav_path
        finally:
            if repair_path.exists():
                try:
                    repair_path.unlink()
                except Exception:
                    pass

        raise AppError("AUDIO_CORRUPTED", "Failed to normalize audio to 16kHz mono WAV.", details={"stderr": proc.stderr[:200]})
    return output_wav_path