Spaces:
Sleeping
Sleeping
Update js/app.py
Browse files18 unmodified lines19202122232425124 unmodified lines1501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831845 unmodified lines190191192163164165166167168169170171193194195196136 unmodified lines33333433533618 unmodified linesimport asyncioimport osimport shutilimport subprocessimport timeimport uuidfrom contextlib import asynccontextmanager124 unmodified lines )
def load_audio_via_ffmpeg(input_path: Path, target_sr: int) -> torch.Tensor: """ Decodes any audio format to a temporary stereo WAV file at the target sample rate using the system FFmpeg CLI, then loads it with torchaudio using the soundfile backend. This avoids torchaudio shared library issues. """ temp_wav = input_path.with_suffix(".temp.wav") try: cmd = [ "ffmpeg", "-y", "-i", str(input_path), "-vn", "-acodec", "pcm_s16le", "-ac", "2", "-ar", str(target_sr), str(temp_wav) ] result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False) if result.returncode != 0: error_msg = result.stderr.decode("utf-8", errors="ignore") raise RuntimeError(f"FFmpeg conversion failed: {error_msg}") wav, _ = torchaudio.load(str(temp_wav)) return wav finally: if temp_wav.exists(): temp_wav.unlink()
def run_separation(job: Job, input_path: Path): """ Runs Demucs on the input file and writes stem WAVs to the job dir.5 unmodified lines out_dir = job_dir(job.id) out_dir.mkdir(parents=True, exist_ok=True)
wav, sr = torchaudio.load(str(input_path))
# Demucs expects stereo; duplicate channel if the upload is mono. if wav.shape[0] == 1: wav = wav.repeat(2, 1)
# Resample to the model's expected sample rate if needed. if sr != model.samplerate: wav = torchaudio.functional.resample(wav, sr, model.samplerate) wav = load_audio_via_ffmpeg(input_path, model.samplerate)
job.status = JobStatus.PROCESSING job.progress = 10136 unmodified lines media_type="audio/wav", filename=f"{stem}.wav", )
js/app.py+32-9