Demucs exposes a Python API for integrating stem separation into scripts and pipelines
Beyond the CLI, Demucs provides demucs.api.Separator for programmatic use. Instantiate with the model name and parameters; call separator.separate_audio_file(path) or separator.separate_tensor(wav_tensor, sr) to receive a dict of stem tensors keyed by stem name. Use demucs.api.save_audio() to write outputs. Parameters can be updated mid-session with separator.update_parameter() without reloading the model — useful when a separation fails with CUDA OOM and you want to retry with a shorter segment. A callback function fires at the start and end of each chunk, enabling progress bars or cancellation.
Examples
import demucs.api
separator = demucs.api.Separator(model="htdemucs", segment=12)
origin, separated = separator.separate_audio_file("track.mp3")
for stem, audio in separated["track.mp3"].items():
demucs.api.save_audio(audio, f"{stem}.wav",
samplerate=separator.samplerate)
Assessment
Write a Python snippet that separates a list of files, catching CUDA OOM errors and retrying each file with segment=8 if needed.