Nodes/ComfyUI-BETA-Cropnodes/Clip to Sharpest Frame βœ‚οΈ πŸ…‘πŸ…”πŸ…£πŸ…
ComfyUI Node

Clip to Sharpest Frame βœ‚οΈ πŸ…‘πŸ…”πŸ…£πŸ…

Cut the blurry tail off a video by finding its sharpest frame

By Burgstall-labsΒ·Created about a year agoΒ·Updated 6 months agoΒ· 8
Clip to Sharpest Frame βœ‚οΈ πŸ…‘πŸ…”πŸ…£πŸ…
  • images
  • clipped_images
  • sharpest_frame_index
β—„last_n_frames10β–Ί
β—„skip_text_framesfalseβ–Ί
β—„skip_black_white_framesfalseβ–Ί
β—„black_white_threshold0.90β–Ί
β—„show_debugfalseβ–Ί

Clip to Sharpest Frame answers one annoying question: "where does my video stop being blurry?" It looks at the last N frames of an image batch, finds the sharpest one, and hands you the batch clipped up to that frame. The frames after it - the motion-blurred tail, the transition junk, the frames where the camera was still moving - get cut off.

Reach for it in two situations. First, video-to-video work where the tail of a generated clip degrades: diffusing an input video, the last few frames often smear, and you want to trim to the last clean one. Second, dataset and keyframe work - anyone who's built a LoRA from video knows the tedium of scrubbing through and weeding out blurry frames. Feed the node a few hundred extracted frames and it finds where things sharpen up, so you can save only the usable stretch.

The node comes from Burgstall-labs' BETA helper pack, a small MIT grab bag of video/text utilities. It needs no model files - just the pack, which pulls in opencv-python.

How it works

Sharpness is measured by the variance of the Laplacian - a classic autofocus metric. The Laplacian highlights edges; a sharp image has lots of high-contrast edges, so its variance is high, and a blurry one has low variance. The node converts each candidate frame to grayscale, runs a Laplacian, and picks the frame with the highest variance in your trailing window. It then clips images to images[:sharpest_index + 1], i.e. everything from the start through the sharpest frame.

The author is upfront that the core logic was borrowed from somewhere online and, since they never planned to publish it, they lost the bookmark. Fair enough - it's a well-worn technique; the sharpness math is solid.

The inputs that matter

  • images - the frame batch. Nothing special; whatever video frames you have.
  • last_n_frames (default 10) - how many frames from the end of the batch get analyzed. The sharpest candidate always comes from this trailing window. If your video's tail is long, raise it.
  • skip_text_frames and skip_black_white_frames (both default off) - filters so title cards, black frames, and white fades don't win the "sharpness" contest. They're off by default and they're worth turning on if your footage has intros/outros.
  • black_white_threshold (default 0.9) - only used with the black/white skip; the fraction of pixels that must be near-black or near-white before a frame counts as blank. Leave it.
  • show_debug - prints per-frame sharpness scores to the console. Turn it on once to see what you're clipping; it's genuinely useful for understanding why the node picked what it did.

What you get out

  • clipped_images - the batch trimmed to frames 0 through the sharpest frame (inclusive).
  • sharpest_frame_index - the 0-based index of that frame in the original batch. Returns -1 if nothing was processed (empty input, or every frame was filtered out). Wire the index into anything that wants to know where the cut happened.

Install

Via ComfyUI Manager, search "BETA-Helpernodes". Or manually:

cd ComfyUI/custom_nodes
git clone https://github.com/Burgstall-labs/ComfyUI-BETA-Helpernodes.git

Restart ComfyUI afterwards. opencv-python is in the pack's requirements.txt; Manager installs it for you, and if you cloned by hand run pip install -r requirements.txt. This pack was previously published as ComfyUI-BETA-Cropnodes - if you still have that old folder, remove it and reinstall under the new name.

Where people get burned

The skip-text filter is a heuristic, and it's the fiddly part. It runs Canny edge detection plus a Hough line transform to guess "this looks like text" - which means a scene full of window blinds, bricks, or fine linework can be flagged as text and skipped even though it's not. If you notice frames vanishing that shouldn't, turn skip_text_frames off and just let a black/white skip do the blank-frame work.

Also temper your expectations about the metric itself: variance-of-Laplacian rewards texture, and grain is texture. A noisy but genuinely blurry frame can out-score a clean sharp one. For the "trim the degenerate tail" use case it's perfect. For "pick the single best still out of an entire clip," you'll still want eyeballs on the result.

CategoryBurgstall Enabling The Awesomeness

Inputs (6)

NameTypeDefaultDescription
imagesIMAGEβ€”
last_n_framesINT101–1000β€”
skip_text_framesBOOLEANfalseβ€”
skip_black_white_framesBOOLEANfalseβ€”
black_white_thresholdFLOAT0.900–1β€”
show_debugBOOLEANfalseβ€”

Outputs (2)

NameTypeDescription
clipped_imagesIMAGEβ€”
sharpest_frame_indexINTβ€”