diff --git a/latentsync/whisper/whisper/audio.py b/latentsync/whisper/whisper/audio.py index 6a1986d..8f49d88 100644 --- a/latentsync/whisper/whisper/audio.py +++ b/latentsync/whisper/whisper/audio.py @@ -21,32 +21,26 @@ N_FRAMES = exact_div(N_SAMPLES, HOP_LENGTH) # 3000: number of frames in a mel s def load_audio(file: str, sr: int = SAMPLE_RATE): """ - Open an audio file and read as mono waveform, resampling as necessary - - Parameters - ---------- - file: str - The audio file to open - - sr: int - The sample rate to resample the audio if necessary - - Returns - ------- - A NumPy array containing the audio waveform, in float32 dtype. + Load an audio file and resample to 16kHz """ try: - # This launches a subprocess to decode audio while down-mixing and resampling as necessary. - # Requires the ffmpeg CLI and `ffmpeg-python` package to be installed. - out, _ = ( - ffmpeg.input(file, threads=0) - .output("-", format="s16le", acodec="pcm_s16le", ac=1, ar=sr) - .run(cmd=["ffmpeg", "-nostdin"], capture_stdout=True, capture_stderr=True) - ) - except ffmpeg.Error as e: - raise RuntimeError(f"Failed to load audio: {e.stderr.decode()}") from e - - return np.frombuffer(out, np.int16).flatten().astype(np.float32) / 32768.0 + # Usa subprocess invece di ffmpeg-python + import subprocess + import numpy as np + + cmd = ['ffmpeg', '-i', file, '-f', 'f32le', '-ac', '1', '-ar', str(sr), '-'] + proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + stdout, stderr = proc.communicate() + + if proc.returncode != 0: + raise RuntimeError(f"Failed to load audio: {stderr.decode()}") + + audio = np.frombuffer(stdout, dtype=np.float32) + return audio + + except Exception as e: + print(f"Error loading audio: {str(e)}") + return None def pad_or_trim(array, length: int = N_SAMPLES, *, axis: int = -1):