Nodes/DJZ-Nodes/Batch Range Swap
ComfyUI Node

Batch Range Swap

Swap a chunk of frames with another sequence in one shot

By MushroomFleet·Created 2 years ago·Updated 5 months ago· 78
Batch Range Swap
  • target_sequence
  • swap_frames
  • IMAGE
start_frame0
end_frame1

Sometimes the fastest way to edit a video isn't an editor - it's swapping frames inside the graph. BatchRangeSwap takes two IMAGE batches and replaces a contiguous range of frames in the first one with frames from the second. It's a surgical little utility from the DJZ-Nodes batch toolkit, and it's surprisingly handy once you're doing frame-level work in ComfyUI: replace a bad segment with a re-rendered one, splice a reaction shot into a sequence, or fix one botched range without re-running the whole pipeline.

How it works

You feed in target_sequence (the batch you want to modify) and swap_frames (the frames to insert), plus start_frame and end_frame defining the range. The node then:

  1. Clamps your indices to safe bounds - start_frame can't go below 0 or past the end of the target, and end_frame is forced to be at least start_frame.
  2. Copies the target sequence (your original isn't mutated - good practice for a node).
  3. Replaces result[start:start+n] with swap_frames[:n], where n is the smaller of the range size and the number of swap frames available.

If you ask to swap 40 frames but only feed in 10, it swaps those 10 and prints a warning. If the range is empty, it returns the sequence untouched with a warning. The clamping means out-of-range indices won't crash the node - they just clamp and warn.

Output is a single IMAGE: the modified batch, same length as the target input.

The inputs

Four: target_sequence (IMAGE), swap_frames (IMAGE), start_frame (INT, 0-999), end_frame (INT, 0-999). Both image inputs are required, and it expects real batches - if the target is a single frame it warns and returns it unchanged. Note it doesn't resize or blend anything: the swap frames must already match the target's frame dimensions, or you'll get a shape mismatch.

Installing

Part of DJZ-Nodes (Drift Johnson / MushroomFleet):

cd ComfyUI/custom_nodes
git clone https://github.com/MushroomFleet/DJZ-Nodes
cd DJZ-Nodes
pip install -r requirements.txt

Or search "DJZ-Nodes" in ComfyUI Manager. Pure torch, no extra deps.

Where it fits

A realistic loop: generate a clip, spot a bad segment at frames 30-50, re-run just that section with different settings, then BatchRangeSwap the good frames back into the original sequence. Pair it with the pack's BatchRangeInsert or BatchOffset if you need to add frames rather than replace them, and count frames with the frame_count output on LoadVideoDirectoryV2 so your indices line up.

Gotchas

The two that bite people: frame dimensions must match, because there's no auto-resize, and the swap is by position, not content - swap_frames frame 0 lands at start_frame, regardless of what's in it. Also end_frame is exclusive in practice (the range is [start, end)), so to replace exactly frames 0-9 you want end_frame = 10, not 9. Minor, but it'll cost you a wasted run the first time.

Categoryimage/batch

Inputs (4)

NameTypeDefaultDescription
target_sequenceIMAGE
swap_framesIMAGE
start_frameINT00–999
end_frameINT10–999

Outputs (1)

NameTypeDescription
IMAGEIMAGE