{"record":{"id":"4d081aa311abb829","repo":"Comfy-Org/ComfyUI","slug":"torchaudio-is-not-available-cannot-resample-audio","errorCode":null,"errorMessage":"torchaudio is not available; cannot resample audio.","messagePattern":"torchaudio is not available; cannot resample audio\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"comfy_api/latest/_ui.py","lineNumber":314,"sourceCode":"            sample_rate = audio[\"sample_rate\"]\n\n            # Handle Opus sample rate requirements\n            if format == \"opus\":\n                if sample_rate > 48000:\n                    sample_rate = 48000\n                elif sample_rate not in AudioSaveHelper._OPUS_RATES:\n                    # Find the next highest supported rate\n                    for rate in sorted(AudioSaveHelper._OPUS_RATES):\n                        if rate > sample_rate:\n                            sample_rate = rate\n                            break\n                    if sample_rate not in AudioSaveHelper._OPUS_RATES:  # Fallback if still not supported\n                        sample_rate = 48000\n\n                # Resample if necessary\n                if sample_rate != audio[\"sample_rate\"]:\n                    if not TORCH_AUDIO_AVAILABLE:\n                        raise Exception(\"torchaudio is not available; cannot resample audio.\")\n                    waveform = torchaudio.functional.resample(waveform, audio[\"sample_rate\"], sample_rate)\n\n            # Create output with specified format\n            output_buffer = BytesIO()\n            output_container = av.open(output_buffer, mode=\"w\", format=format)\n\n            # Set metadata on the container\n            for key, value in metadata.items():\n                output_container.metadata[key] = value\n\n            layout = \"mono\" if waveform.shape[0] == 1 else \"stereo\"\n            # Set up the output stream with appropriate properties\n            if format == \"opus\":\n                out_stream = output_container.add_stream(\"libopus\", rate=sample_rate, layout=layout)\n                if quality == \"64k\":\n                    out_stream.bit_rate = 64000\n                elif quality == \"96k\":\n                    out_stream.bit_rate = 96000","sourceCodeStart":296,"sourceCodeEnd":332,"githubUrl":"https://github.com/Comfy-Org/ComfyUI/blob/1c6d8d45b3693bfbb32385b410d813a7fd6be216/comfy_api/latest/_ui.py#L296-L332","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Install torchaudio matching the installed torch version (e.g. pip install torchaudio --index-url https://download.pytorch.org/whl/cu121).","Request an output format/container that accepts the source sample rate (e.g. wav/flac instead of opus) so no resample is needed.","Resample the waveform yourself before the node call and pass audio already at 48000/24000/16000/12000/8000 Hz.","Verify import torchaudio succeeds in the same Python environment ComfyUI runs in (torch/torchaudio ABI mismatch silently disables it)."],"exampleFix":"// before\naudio_save(format=\"opus\")  // 44100 Hz source, torchaudio missing -> Exception\n\n// after\n# pip install torchaudio (same wheel index as torch)\n# or resample beforehand:\nwaveform = torchaudio.functional.resample(waveform, 44100, 48000)\naudio = {\"waveform\": waveform.unsqueeze(0), \"sample_rate\": 48000}\naudio_save(format=\"opus\")","handlingStrategy":"validation","validationCode":"from comfy_api.latest._ui import AudioSaveHelper\nimport torchaudio\n\nneeds_resample = audio[\"sample_rate\"] not in AudioSaveHelper._OPUS_RATES and format == \"opus\"\nif needs_resample:\n    assert torchaudio.functional.resample is not None, \"install torchaudio before opus export at this rate\"","typeGuard":"def can_resample() -> bool:\n    try:\n        import torchaudio  # noqa: F401\n        return True\n    except ImportError:\n        return False","tryCatchPattern":"try:\n    audio_save_node(format=\"opus\")\nexcept Exception as e:\n    if \"torchaudio is not available\" in str(e):\n        audio_save_node(format=\"wav\")  # no resample needed","preventionTips":["Install torchaudio from the same wheel index as torch to avoid import failure.","Prefer lossless formats (wav/flac) when torchaudio is unavailable.","Deliver audio at 48000 Hz to satisfy Opus natively."],"tags":["audio","dependency","resampling","opus","environment"],"backgroundTag":null,"analyzedSha":"1c6d8d45b3693bfbb32385b410d813a7fd6be216","analyzedAt":"2026-08-14T19:37:18.893Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}