From 266e9d1024ee7d63c7b471e7724ea2e9876d8b93 Mon Sep 17 00:00:00 2001 From: KarrixLee Date: Wed, 23 Apr 2025 13:36:01 +0800 Subject: [PATCH] refactor: enhance audio loading with error handling and import checks --- comfy-nodes/external_audio.py | 66 ++++++++++++++++++++++++----------- 1 file changed, 45 insertions(+), 21 deletions(-) diff --git a/comfy-nodes/external_audio.py b/comfy-nodes/external_audio.py index 6978c2d..b1318bd 100644 --- a/comfy-nodes/external_audio.py +++ b/comfy-nodes/external_audio.py @@ -1,14 +1,13 @@ -import os import io -import torchaudio from folder_paths import get_annotated_filepath + class ComfyUIDeployExternalAudio: RETURN_TYPES = ("AUDIO",) RETURN_NAMES = ("audio",) FUNCTION = "load_audio" CATEGORY = "🔗ComfyDeploy" - + @classmethod def INPUT_TYPES(cls): return { @@ -29,30 +28,55 @@ class ComfyUIDeployExternalAudio: "STRING", {"multiline": False, "default": ""}, ), - } + }, } - + @classmethod def VALIDATE_INPUTS(s, audio_file, **kwargs): return True - def load_audio(self, input_id, audio_file, default_value=None, display_name=None, description=None): - if audio_file and audio_file != "": - if audio_file.startswith(('http://', 'https://')): - # Handle URL input - import requests - response = requests.get(audio_file) - audio_data = io.BytesIO(response.content) - waveform, sample_rate = torchaudio.load(audio_data) + def load_audio( + self, + input_id, + audio_file, + default_value=None, + display_name=None, + description=None, + ): + try: + import torchaudio + + if audio_file and audio_file != "": + if audio_file.startswith(("http://", "https://")): + # Handle URL input + try: + import requests + + response = requests.get(audio_file) + audio_data = io.BytesIO(response.content) + waveform, sample_rate = torchaudio.load(audio_data) + except Exception as e: + print(f"Error loading audio from URL: {e}") + return (default_value,) + else: + # Handle local file + try: + audio_path = get_annotated_filepath(audio_file) + waveform, sample_rate = torchaudio.load(audio_path) + except Exception as e: + print(f"Error loading local audio file: {e}") + return (default_value,) + + audio = {"waveform": waveform.unsqueeze(0), "sample_rate": sample_rate} + return (audio,) else: - # Handle local file - audio_path = get_annotated_filepath(audio_file) - waveform, sample_rate = torchaudio.load(audio_path) - - audio = {"waveform": waveform.unsqueeze(0), "sample_rate": sample_rate} - return (audio,) - else: + return (default_value,) + except ImportError as e: + print(f"Error: torchaudio not installed or cannot be imported: {e}") return (default_value,) + NODE_CLASS_MAPPINGS = {"ComfyUIDeployExternalAudio": ComfyUIDeployExternalAudio} -NODE_DISPLAY_NAME_MAPPINGS = {"ComfyUIDeployExternalAudio": "External Audio (ComfyUI Deploy)"} +NODE_DISPLAY_NAME_MAPPINGS = { + "ComfyUIDeployExternalAudio": "External Audio (ComfyUI Deploy)" +}