增加从URL加载音频节点

This commit is contained in:
wangbo 2025-05-15 19:21:11 +08:00
parent e004083989
commit 89dbe3b916
2 changed files with 52 additions and 1 deletions

View File

@ -1,5 +1,5 @@
WEB_DIRECTORY = "js"
NODE_CLASS_MAPPINGS = {}
from .nodes import NODE_CLASS_MAPPINGS
__all__ = ['NODE_CLASS_MAPPINGS']
from aiohttp import ClientSession, web

51
nodes.py Normal file
View File

@ -0,0 +1,51 @@
import soundfile as sf
import requests
import io
import numpy as np
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 = "Audio Reactor"
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)
# 使用 soundfile 从内存中读取音频数据
audio, file_sr = sf.read(audio_data)
# 如果需要重采样
if file_sr != sample_rate:
# 这里需要添加重采样逻辑
# 可以使用 librosa.resample 或其他方法
pass
except Exception as e:
raise Exception(f"加载网络音频失败: {str(e)}")
else:
# 本地文件使用原有的 librosa 方式加载
audio, _ = librosa.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,
}