Nodes/More Math/Video math
ComfyUI Node

Video math

One expression for the frames, another for the audio, both on video

By mcDandy·Created about a year ago·Updated 3 days ago· 5
Video math
  • a
  • b
  • c
  • d
  • VIDEO
Imagesa*(1-w)+b*w
Audioa*(1-w)+b*w
length_mismatcherror
w0.00
x0.00
y0.00
z0.00

Video math is the pack's everything-node for video: it runs one expression over the frames and a second expression over the audio, in a single node. The default, a*(1-w)+b*w on both sides, crossfades video a into video b - frames and sound together. In ComfyUI a video is a bundle of an image tensor and an audio tensor, and this node treats each part with the right math: image variables for the frames, audio variables for the sound.

How it works

A VIDEO object carries both components. The node splits them apart and evaluates the two expressions independently:

  • Images - run on the frame tensor. You get the image variables: a, b, c, d, w, x, y, z, plus B/frame (the frame index), C, H, W, T/frame_count.
  • Audio - run on the audio waveform. You get B, C (audio channel), S (sample), T/sample_count, R/sample_rate.

Then the results are recombined into a single VIDEO. So a per-frame effect like "make the first half of frames darker" uses the frame index in the Images expression, while a crossfade of the sound uses the Audio expression. Two video inputs (a, b) with a w knob is the common case - a video crossfade - but the expressions are fully general.

The length_mismatch combo (tile / error / pad) handles mismatched frame or sample counts between inputs.

The inputs that matter

  • a - required video. b, c, d optional.
  • Images - expression for the image frames, default a*(1-w)+b*w.
  • Audio - expression for the audio component, default a*(1-w)+b*w.
  • length_mismatch - tile / error / pad, default error.
  • w, x, y, z - floats.

Output is a single VIDEO.

Installing it

Part of More Math (mcDandy/more_math). ComfyUI Manager - search "More Math" - or:

cd ComfyUI/custom_nodes
git clone https://github.com/mcDandy/more_math
cd more_math
pip install -r requirements.txt

Restart. Dependencies are antlr4-python3-runtime and torch, no model downloads. Needs a current ComfyUI (newer node API).

Where people get burned

First: the Audio expression runs on the audio even if you only meant to touch frames. If you change only Images and leave Audio as the default, a crossfade will also blend the sound - usually what you want, but it's worth knowing both fields are live.

Second, length_mismatch defaults to error, and videos love to disagree on frame counts. Two clips of different lengths will throw until you pick tile (repeat the shorter) or pad (zero-fill). Decide which one matches the effect you're going for - tile for a loop-style crossfade, pad if silence/black frames are acceptable.

Third, if either input video lacks audio, the audio math can produce silence or flat results depending on how it's zero-filled - check your source videos before you blame the expression.

This is genuinely one of the more useful nodes in the pack for video work: instead of splitting a video apart, editing frames and audio separately, and rejoining, you get both in one expression box. The classic form is deprecated in favor of an autogrow variant, as with most of the pack - both ship, and the autogrow one takes more video inputs.

Categorysd

Inputs (11)

NameTypeDefaultDescription
aVIDEO
ImagesSTRINGa*(1-w)+b*wExpression for the image frames
AudioSTRINGa*(1-w)+b*wExpression for the audio component
length_mismatchCOMBOerrorHow to handle mismatched frame/sample counts. tile: repeat shorter inputs; error: raise error on mismatch; pad: treat missing as zero.
boptVIDEO
coptVIDEO
doptVIDEO
woptFLOAT0.00
xoptFLOAT0.00
yoptFLOAT0.00
zoptFLOAT0.00

Outputs (1)

NameTypeDescription
VIDEOVIDEO