minimax: base64 video/audio reference loader nodes
Browse files- worker/load_media_b64.py +105 -0
worker/load_media_b64.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""ComfyUI custom nodes: decode a base64 video/audio embedded in the workflow.
|
| 2 |
+
|
| 3 |
+
worker-comfyui only uploads images (/upload/image), so reference VIDEO and AUDIO
|
| 4 |
+
for MiniMax H3 ref2va can't arrive as files. These nodes take the media as a
|
| 5 |
+
base64 string widget (embedded in the workflow JSON by the app) and decode it
|
| 6 |
+
with PyAV into ComfyUI's native IMAGE (frames) and AUDIO types — feeding the
|
| 7 |
+
MiniMaxH3ReferenceToVideo ref_video_* / ref_audio_* slots.
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
import base64
|
| 11 |
+
import io
|
| 12 |
+
|
| 13 |
+
import numpy as np
|
| 14 |
+
import torch
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def _decode(b64):
|
| 18 |
+
import av # installed by the worker start script
|
| 19 |
+
raw = base64.b64decode(b64)
|
| 20 |
+
container = av.open(io.BytesIO(raw))
|
| 21 |
+
return container
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def _frames(container):
|
| 25 |
+
frames = []
|
| 26 |
+
for frame in container.decode(video=0):
|
| 27 |
+
frames.append(frame.to_ndarray(format="rgb24"))
|
| 28 |
+
if not frames:
|
| 29 |
+
raise ValueError("no video frames decoded")
|
| 30 |
+
arr = np.stack(frames).astype(np.float32) / 255.0 # [N,H,W,3]
|
| 31 |
+
return torch.from_numpy(arr)
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def _audio(container):
|
| 35 |
+
samples = []
|
| 36 |
+
rate = None
|
| 37 |
+
layout = None
|
| 38 |
+
try:
|
| 39 |
+
for frame in container.decode(audio=0):
|
| 40 |
+
rate = frame.sample_rate
|
| 41 |
+
layout = len(frame.layout.channels)
|
| 42 |
+
a = frame.to_ndarray() # [channels, samples] or [1, samples]
|
| 43 |
+
samples.append(a)
|
| 44 |
+
except Exception:
|
| 45 |
+
pass
|
| 46 |
+
if not samples or rate is None:
|
| 47 |
+
return None
|
| 48 |
+
data = np.concatenate(samples, axis=1) if samples[0].ndim == 2 else np.stack(samples)
|
| 49 |
+
data = data.astype(np.float32)
|
| 50 |
+
if data.max() > 1.5: # int PCM range → normalize
|
| 51 |
+
data = data / 32768.0
|
| 52 |
+
if data.ndim == 1:
|
| 53 |
+
data = data[None, :]
|
| 54 |
+
waveform = torch.from_numpy(data).unsqueeze(0) # [1, C, T]
|
| 55 |
+
return {"waveform": waveform, "sample_rate": int(rate)}
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
class LoadVideoBase64:
|
| 59 |
+
@classmethod
|
| 60 |
+
def INPUT_TYPES(cls):
|
| 61 |
+
return {"required": {"video_base64": ("STRING", {"default": "", "multiline": True})}}
|
| 62 |
+
|
| 63 |
+
RETURN_TYPES = ("IMAGE", "AUDIO")
|
| 64 |
+
RETURN_NAMES = ("frames", "audio")
|
| 65 |
+
FUNCTION = "load"
|
| 66 |
+
CATEGORY = "MiniMaxH3/reference"
|
| 67 |
+
|
| 68 |
+
def load(self, video_base64):
|
| 69 |
+
b64 = (video_base64 or "").split(",")[-1].strip()
|
| 70 |
+
if not b64:
|
| 71 |
+
raise ValueError("video_base64 is empty")
|
| 72 |
+
frames = _frames(_decode(b64))
|
| 73 |
+
audio = _audio(_decode(b64)) or {
|
| 74 |
+
"waveform": torch.zeros(1, 1, 1), "sample_rate": 24000
|
| 75 |
+
}
|
| 76 |
+
return (frames, audio)
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
class LoadAudioBase64:
|
| 80 |
+
@classmethod
|
| 81 |
+
def INPUT_TYPES(cls):
|
| 82 |
+
return {"required": {"audio_base64": ("STRING", {"default": "", "multiline": True})}}
|
| 83 |
+
|
| 84 |
+
RETURN_TYPES = ("AUDIO",)
|
| 85 |
+
FUNCTION = "load"
|
| 86 |
+
CATEGORY = "MiniMaxH3/reference"
|
| 87 |
+
|
| 88 |
+
def load(self, audio_base64):
|
| 89 |
+
b64 = (audio_base64 or "").split(",")[-1].strip()
|
| 90 |
+
if not b64:
|
| 91 |
+
raise ValueError("audio_base64 is empty")
|
| 92 |
+
audio = _audio(_decode(b64))
|
| 93 |
+
if audio is None:
|
| 94 |
+
raise ValueError("no audio stream decoded")
|
| 95 |
+
return (audio,)
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
NODE_CLASS_MAPPINGS = {
|
| 99 |
+
"LoadVideoBase64": LoadVideoBase64,
|
| 100 |
+
"LoadAudioBase64": LoadAudioBase64,
|
| 101 |
+
}
|
| 102 |
+
NODE_DISPLAY_NAME_MAPPINGS = {
|
| 103 |
+
"LoadVideoBase64": "Load Video (base64)",
|
| 104 |
+
"LoadAudioBase64": "Load Audio (base64)",
|
| 105 |
+
}
|