Nodes/ComfyUI-UniversalToolkit/Best Context Window (UTK)
ComfyUI Node

Best Context Window (UTK)

The video context-window math you were doing by hand

By whmc76·Created about a year ago·Updated about a month ago· 72
Best Context Window (UTK)
    • best_window
    • padding
    • padded_total
    • segments
    total_frames1
    min_window_frames61
    max_window_frames81

    Some video models won't take just any frame count - they want windows of 4n+1 frames, and they want your clip chopped into matching segments so the temporal conditioning lines up. Figuring out "my 130-frame clip needs what window size to avoid padding waste?" by hand is the kind of arithmetic you'll definitely botch at 1 a.m. Best Context Window (UTK) is that arithmetic in a node: feed it your total frames and it returns the cleanest window size, the padding, and how many segments to split into.

    The inputs

    • total_frames - how many frames your clip actually has.
    • min_window_frames / max_window_frames (defaults 61 / 81) - the range of acceptable window sizes. The node searches only inside this band.

    What it computes

    It generates all candidate window sizes in [min, max] that satisfy the 4n+1 rule (so 61, 65, 69, …), then for each one works out the arithmetic of splitting your total into windows: how many segments (k = ceil(total / window)), how many padding frames that requires to fill the last segment (padding = k*window − total), and the padded total.

    The output is the candidate that minimizes padding - and on a tie, the larger window (larger windows mean fewer, more manageable segments).

    Outputs

    Four integers, and each one is a wire you can plug somewhere real:

    • best_window - the window size to use for your video model's context.
    • padding - how many duplicate/padding frames you'll need to reach a clean multiple. Zero is the happy case.
    • padded_total - total_frames + padding, i.e. the actual frame count the model will process.
    • segments - how many chunks your clip splits into.

    So the workflow is: total_frames → this node → feed best_window and segments into your context/latent setup, and know exactly how much padding your pipeline needs instead of discovering it via a failed generation.

    Installing it

    Standard pack install - ComfyUI Manager → search ComfyUI-UniversalToolkit, or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/whmc76/ComfyUI-UniversalToolkit
    cd ComfyUI-UniversalToolkit && pip install -r requirements.txt
    

    Then restart. It's pure Python integer math - no dependencies, no GPU, no models. It's as close to a free node as this pack has.

    Gotchas

    • If your min/max range contains no valid 4n+1 value, it falls back to the closest valid window below max rather than erroring - so it never breaks, but double-check the output if you set weird bounds.
    • "Best" means least padding, not best video quality. A window that needs zero padding is mathematically clean but might be a size your model handles poorly. The node optimizes the bookkeeping; you still pick the bounds that make sense for the model.
    • It only answers "what window," not "how to build the segments." You still wire the frame chopping yourself - this node just makes the numbers trustworthy.

    It's a tiny math helper, the kind you'd normally do in a spreadsheet. But if your video pipeline is finicky about context windows, having this in the graph turns "I think 65 works?" into a number you can trust.

    CategoryUniversalToolkit/Tools

    Inputs (3)

    NameTypeDefaultDescription
    total_framesINT1
    min_window_framesINT61
    max_window_framesINT81

    Outputs (4)

    NameTypeDescription
    best_windowINT
    paddingINT
    padded_totalINT
    segmentsINT