Spaces:
Sleeping
Sleeping
Update js/app.py
Browse files180 unmodified lines18118218318418518618718818919019119219319419519619719819920029 unmodified lines23023123223321923423523623722223823922422522624024124224324424524624724824925025125225395 unmodified lines349350351352353354355356357358359360361362363364365366367368369370371372373374375376377180 unmodified lines temp_wav.unlink()
def encode_to_mp3(wav_path: Path, mp3_path: Path): """ Encodes a WAV file to a lightweight 128k MP3 file for fast browser preview. """ cmd = [ "ffmpeg", "-y", "-i", str(wav_path), "-codec:a", "libmp3lame", "-b:a", "128k", str(mp3_path) ] subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False)
def run_separation(job: Job, input_path: Path): """ Runs Demucs on the input file and writes stem WAVs to the job dir.29 unmodified lines
if job.mode == "4stem": for name, tensor in stem_tensors.items(): wav_path = out_dir / f"{name}.wav" save_audio(tensor, str(out_dir / f"{name}.wav"), model.samplerate) save_audio(tensor, str(wav_path), model.samplerate) encode_to_mp3(wav_path, out_dir / f"{name}.mp3") job.stems = stem_names else: # 2-stem: vocals vs everything else summed together # 2-stem: vocals vs everything else (original mix minus vocals) vocals = stem_tensors["vocals"] instrumental = stem_tensors["drums"] + stem_tensors["bass"] + stem_tensors["other"] save_audio(vocals, str(out_dir / "vocals.wav"), model.samplerate) save_audio(instrumental, str(out_dir / "instrumental.wav"), model.samplerate) instrumental = wav - vocals vocals_wav = out_dir / "vocals.wav" inst_wav = out_dir / "instrumental.wav" save_audio(vocals, str(vocals_wav), model.samplerate) save_audio(instrumental, str(inst_wav), model.samplerate) encode_to_mp3(vocals_wav, out_dir / "vocals.mp3") encode_to_mp3(inst_wav, out_dir / "instrumental.mp3") job.stems = ["vocals", "instrumental"]
job.progress = 10095 unmodified lines media_type="audio/wav", filename=f"{stem}.wav", )
@app.get("/preview/{job_id}/{stem}")def preview(job_id: str, stem: str): job = JOBS.get(job_id) if job is None: raise HTTPException(status_code=404, detail="Job not found.") if job.status != JobStatus.DONE: raise HTTPException(status_code=409, detail="Job isn't finished yet.") if stem not in job.stems: raise HTTPException(status_code=404, detail=f"Stem '{stem}' not available.")
file_path = job_dir(job_id) / f"{stem}.mp3" # Fallback to wav if mp3 conversion failed for some reason if not file_path.exists(): file_path = job_dir(job_id) / f"{stem}.wav"
if not file_path.exists(): raise HTTPException(status_code=404, detail="Preview file missing.")
media_type = "audio/mpeg" if file_path.suffix == ".mp3" else "audio/wav" return FileResponse( path=str(file_path), media_type=media_type, filename=f"{stem}{file_path.suffix}", )js/app.py+55-5