Comfy-Org/ComfyUI · error · Exception

torchaudio is not available; cannot resample audio.

Error message

torchaudio is not available; cannot resample audio.

What it means

Raised by AudioSaveHelper when saving audio whose sample rate must be converted (e.g. Opus only supports 8/12/16/24/48 kHz) but the optional torchaudio dependency is not installed. The helper picks a supported target rate, then needs torchaudio.functional.resample to convert the waveform; without torchaudio it refuses to silently write audio at an invalid rate.

Source

Thrown at comfy_api/latest/_ui.py:314

            sample_rate = audio["sample_rate"]

            # Handle Opus sample rate requirements
            if format == "opus":
                if sample_rate > 48000:
                    sample_rate = 48000
                elif sample_rate not in AudioSaveHelper._OPUS_RATES:
                    # Find the next highest supported rate
                    for rate in sorted(AudioSaveHelper._OPUS_RATES):
                        if rate > sample_rate:
                            sample_rate = rate
                            break
                    if sample_rate not in AudioSaveHelper._OPUS_RATES:  # Fallback if still not supported
                        sample_rate = 48000

                # Resample if necessary
                if sample_rate != audio["sample_rate"]:
                    if not TORCH_AUDIO_AVAILABLE:
                        raise Exception("torchaudio is not available; cannot resample audio.")
                    waveform = torchaudio.functional.resample(waveform, audio["sample_rate"], sample_rate)

            # Create output with specified format
            output_buffer = BytesIO()
            output_container = av.open(output_buffer, mode="w", format=format)

            # Set metadata on the container
            for key, value in metadata.items():
                output_container.metadata[key] = value

            layout = "mono" if waveform.shape[0] == 1 else "stereo"
            # Set up the output stream with appropriate properties
            if format == "opus":
                out_stream = output_container.add_stream("libopus", rate=sample_rate, layout=layout)
                if quality == "64k":
                    out_stream.bit_rate = 64000
                elif quality == "96k":
                    out_stream.bit_rate = 96000

View on GitHub (pinned to 1c6d8d45b3)

Solutions

  1. Install torchaudio matching the installed torch version (e.g. pip install torchaudio --index-url https://download.pytorch.org/whl/cu121).
  2. Request an output format/container that accepts the source sample rate (e.g. wav/flac instead of opus) so no resample is needed.
  3. Resample the waveform yourself before the node call and pass audio already at 48000/24000/16000/12000/8000 Hz.
  4. Verify import torchaudio succeeds in the same Python environment ComfyUI runs in (torch/torchaudio ABI mismatch silently disables it).

Example fix

// before
audio_save(format="opus")  // 44100 Hz source, torchaudio missing -> Exception

// after
# pip install torchaudio (same wheel index as torch)
# or resample beforehand:
waveform = torchaudio.functional.resample(waveform, 44100, 48000)
audio = {"waveform": waveform.unsqueeze(0), "sample_rate": 48000}
audio_save(format="opus")
Defensive patterns

Strategy: validation

Validate before calling

from comfy_api.latest._ui import AudioSaveHelper
import torchaudio

needs_resample = audio["sample_rate"] not in AudioSaveHelper._OPUS_RATES and format == "opus"
if needs_resample:
    assert torchaudio.functional.resample is not None, "install torchaudio before opus export at this rate"

Type guard

def can_resample() -> bool:
    try:
        import torchaudio  # noqa: F401
        return True
    except ImportError:
        return False

Try / catch

try:
    audio_save_node(format="opus")
except Exception as e:
    if "torchaudio is not available" in str(e):
        audio_save_node(format="wav")  # no resample needed

Prevention

When it happens

Trigger: Saving audio via the audio save helper with format='opus' (or any path where the requested sample_rate is not in AudioSaveHelper._OPUS_RATES) when TORCH_AUDIO_AVAILABLE is False, so the fallback target rate (e.g. 48000) differs from audio['sample_rate'] and the resample branch executes.

Common situations: Minimal ComfyUI install without the torchaudio extra; environments where torchaudio fails to import due to a torch/torchaudio version mismatch; rendering TTS audio at 22050/44100 Hz and encoding to Opus.

Related errors


AI-assisted analysis of Comfy-Org/ComfyUI@1c6d8d45b3 (2026-08-14). Data as JSON: /api/errors/4d081aa311abb829. Report an issue: GitHub.