55 lines
1.9 KiB
Python
55 lines
1.9 KiB
Python
import requests
|
|
import io
|
|
import librosa.core as core
|
|
import torch
|
|
|
|
class AudioLoadPath:
|
|
@classmethod
|
|
def INPUT_TYPES(s):
|
|
return {"required": { "path": ("STRING", {"default": "X://insert/path/here.mp4"}),
|
|
"sample_rate": ("INT", {"default": 22050, "min": 6000, "max": 192000, "step": 1}),
|
|
"offset": ("FLOAT", {"default": 0.0, "min": 0.0, "max": 1e6, "step": 0.001}),
|
|
"duration": ("FLOAT", {"default": 0.0, "min": 0.0, "max": 1e6, "step": 0.001})}}
|
|
|
|
RETURN_TYPES = ("AUDIO", )
|
|
CATEGORY = "EasyAI"
|
|
FUNCTION = "load"
|
|
|
|
def load(self, path: str, sample_rate: int, offset: float, duration: float|None):
|
|
if duration == 0.0:
|
|
duration = None
|
|
|
|
try:
|
|
if path.startswith(('http://', 'https://')):
|
|
response = requests.get(path)
|
|
response.raise_for_status()
|
|
audio_data = io.BytesIO(response.content)
|
|
|
|
import warnings
|
|
with warnings.catch_warnings():
|
|
warnings.simplefilter("ignore")
|
|
audio, _ = core.load(audio_data, sr=sample_rate, offset=offset, duration=duration)
|
|
else:
|
|
audio, _ = core.load(path, sr=sample_rate, offset=offset, duration=duration)
|
|
|
|
# 将音频数据转换为正确的格式
|
|
audio_tensor = torch.from_numpy(audio).float()
|
|
|
|
# 创建符合 SaveAudio 节点期望的字典格式
|
|
audio_dict = {
|
|
"waveform": audio_tensor.unsqueeze(0), # 添加批次维度 [batch, samples]
|
|
"sample_rate": sample_rate
|
|
}
|
|
|
|
return (audio_dict,)
|
|
|
|
except Exception as e:
|
|
raise Exception(f"加载音频失败: {str(e)}")
|
|
|
|
NODE_CLASS_MAPPINGS = {
|
|
"AudioLoadPath": AudioLoadPath,
|
|
}
|
|
|
|
NODE_DISPLAY_NAME_MAPPINGS = {
|
|
"AudioLoadPath": "Load Audio (Path/URL)"
|
|
} |