47 lines
1.8 KiB
Python
47 lines
1.8 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
|
|
|
|
if path.startswith(('http://', 'https://')):
|
|
# 对于网络路径,直接从内存加载
|
|
try:
|
|
response = requests.get(path)
|
|
response.raise_for_status()
|
|
audio_data = io.BytesIO(response.content)
|
|
|
|
# 使用 librosa 直接从内存中读取音频数据
|
|
import warnings
|
|
with warnings.catch_warnings():
|
|
warnings.simplefilter("ignore")
|
|
audio, _ = core.load(audio_data, sr=sample_rate, offset=offset, duration=duration)
|
|
|
|
except Exception as e:
|
|
raise Exception(f"加载网络音频失败: {str(e)}")
|
|
else:
|
|
# 本地文件使用原有的 librosa 方式加载
|
|
audio, _ = core.load(path, sr=sample_rate, offset=offset, duration=duration)
|
|
|
|
# 转换为 torch tensor 并调整维度
|
|
audio = torch.from_numpy(audio)[None,:,None]
|
|
return (audio,)
|
|
|
|
NODE_CLASS_MAPPINGS = {
|
|
"AudioLoadPath": AudioLoadPath,
|
|
} |