LayerUtility: Pad Image Batch to 8n+1
Give a Video Model Exactly the Frame Count It Wants
- images
- images
- original_batch_length
Somewhere between your Load Video node and your sampler, a video pipeline will quietly demand that the batch of frames be a specific length. Not "enough frames" - a specific count. For Wan models the Wan VAE compresses time roughly 4x, so a valid batch is 4n+1 frames; other video processors want 8n+1, or any other multiplier*n + remainder rule. Feed them the wrong number and your clip comes back two frames short, or the tail behaves weirdly, or nothing runs at all. This node is the patch-up: it pads an image batch up to the next length that fits your rule, and hands you back the original count so you can trim later.
It's the generic, configurable version of a trick that video people hand-roll constantly. The classic failure is a clip that "loses" frames through a temporal VAE - the fix is to round the frame count up to a valid length before processing, then cut back down after. Pad Image Batch to 8n+1 does the first half. Its sibling, Restore Pad Image Batch, does the second.
How it works
The math is the whole node. Given your multiplier (default 8) and remainder (default 1), it computes the smallest target length matching multiplier*n + remainder that is at least as big as what you have, then pads by repeating the last frame. If your batch is already at a valid length, it's a passthrough - zero harm leaving it in the graph.
Repeating the last frame matters. The node could pad with black frames or loop the sequence, but it doesn't, because those padding frames are garbage that you're going to delete anyway after processing. Duplicating the final frame is the least-bad filler, and it keeps the model's temporal context sane.
The inputs and outputs that matter
Only three inputs, all required, and you'll touch two of them:
images- your batch of frames (IMAGE).multiplier(INT, default 8) - the "8" in "8n+1". This is the rule your downstream process wants, so set it to the model's actual constraint. Wan-style pipelines want 4 here.remainder(INT, default 1) - the "1" in "8n+1". Note the code enforces0 <= remainder < multiplier; the UI lets you type 0–999 but it will error out if you exceed the multiplier.
Outputs:
images- the padded batch.original_batch_length(INT) - wire this intoRestore Pad Image Batchon the far side of your processing. It's the "how many frames did I actually have" record.
A common real setup: load video → pad to 8n+1 → process (interpolate, upscale, pass through a model) → restore to the original count → save. The pad node and restore node are bookends; don't use one without the other unless you genuinely want a longer output.
Installing it
This ships inside the big ComfyUI Layer Style pack by chflame163 - one of the most-used custom node packs in the ecosystem, a Photoshop-in-ComfyUI grab bag of layer, mask, and utility nodes. Install via ComfyUI Manager (search "Layer Style") or:
cd ComfyUI/custom_nodes
git clone https://github.com/chflame163/ComfyUI_LayerStyle
Then install dependencies and restart. For the official portable build:
..\..\..\python_embeded\python.exe -s -m pip install -r requirements.txt
.\repair_dependency.bat
Heads-up: this pack drags in a genuinely heavy dependency list - transformers, timm, opencv-contrib-python, scikit-image, scikit-learn, pymatting, colour-science - and this tiny node uses exactly none of them (it's pure torch). You're paying the whole pack's install cost for a two-line utility. That's the price of convenience; it's not a model-download situation, these two nodes need no weights.
Common issues
The pack itself is the main source of pain, not this node. Import failures after updates are the #1 complaint you'll see on r/comfyui, usually a dependency conflict - most commonly a broken opencv-contrib-python (NameError: name 'guidedFilter' is not defined) or an outdated transformers (Cannot import name 'VitMatteImageProcessor'). The README's fix is to double-click repair_dependency.bat (or repair_dependency_aki.bat on Aki builds), which reinstalls the deps. The xxx.ini not found warning is harmless - rename the *.ini.example files if it bugs you.
For this node specifically: don't feed it an empty batch (it raises), and remember the remainder < multiplier rule. And if you're confused why the default is "8n+1" at all - it isn't a law of nature, it's just the author's shipped convention. Whatever rule your pipeline actually wants, type it in.
Inputs (3)
| Name | Type | Default | Description |
|---|---|---|---|
| images | IMAGE | — | |
| multiplier | INT | 81–999 | — |
| remainder | INT | 10–999 | — |
Outputs (2)
| Name | Type | Description |
|---|---|---|
| images | IMAGE | — |
| original_batch_length | INT | — |