Kling AI video to a looping sprite sheet
Turning Kling AI videos into Echo Bat's wingbeats meant painting out blinks the prompt forbade, cutting pink spill from 14% to 0.2%, and splicing beats.
Contents
Echo Bat has six bats to fly, and until this month five of them were the first one with a colour filter on top. I wanted each to be its own character with its own wingbeat, and I was not going to draw 144 frames by hand. So every bat became a Kling AI video: one start frame, a prompt, four seconds of flapping on a magenta screen, and a script that cuts the clip into a looping sprite sheet.
The plan held. Every step of it broke at least once on the way, mostly where the model ignored the prompt in a way the pipeline had not planned for. This post is those failures in the order I hit them, with the numbers. It ends with a second round a few days later, where I tried to get five different moves out of one ten-second clip from Google's Veo.
The pipeline is Python with NumPy, Pillow and ffmpeg, written with Claude Code. The game draws on a canvas 2D renderer inside a Capacitor WebView on Android. How the frames get packed for it is in the texture atlas post; this one stops where that one starts.
The start frame and the prompt
Mine was a 1920×1080 PNG: the bat in its glide pose, wings out level, centred on flat #FF00FF
with plenty of room above and below for the wings. Two choices in it paid off later:
- Hard edges. The bat's outline was made fully opaque, no soft rim. A soft edge over magenta is a pink edge, and the model would have animated the pink.
- A mid-stroke pose. A wingbeat passes through level anyway, so closing the loop there is easier than at the top or bottom of the stroke.
The same image went into Kling's start-frame and end-frame slots. The prompt asked for exactly one wingbeat across the clip, a locked camera, a background that never changes, and this about the face:
The face stays steady, no blinking.
blinking was in the negative prompt too. Each clip came back as 97 frames at 24 fps,
1920×1080, a little over four seconds.
The five new characters were drawn by an image model from the original bat's start frame, then scaled to its wingspan and centred in the same spot, so one extraction script could cut all six the same way.
Finding one wingbeat in the clip
The extractor keys out the magenta, keeps only the shape connected to the middle of the picture, finds where the wingbeat comes back to its first pose, and picks frames from that one cycle.
Before any real video existed I tested it on a fake one: the game's three old bat poses played as a cycle on magenta and compressed with h264, the way a generator's output would be. That test found the first bug. The extractor compared each frame with frame 0 and cut at the best match, and the best match was halfway round the cycle, where the wings pass through level on the way down. Same pose, opposite direction.
Comparing a short run of frames fixed it. A candidate end is scored on how well the four frames starting there match the first four frames of the clip, so wings passing level on the way down no longer look like wings at level on the way up:
# Simplified example: score a loop end on a run of frames, not on one pose.
def loop_score(masks, end, run=4):
steps = range(min(run, len(masks) - end))
return np.mean([np.abs(masks[end + t] - masks[t]).mean() for t in steps])
On the fake clip the cut moved to frame 8, the right one, and the mid-cycle level pass at frame 4 scored 0.058 against 0.0001.
The real clips found another way round it. The toxic bat holds almost still for the first half-second, twitches, and settles back into its start pose. The extractor took that twitch for a whole loop and cut at frame 13, because a still bat matches frame 0 perfectly well. The real wingbeat closes at frame 95, with a score of 0.0004, and I only saw it by printing the score for every frame of the clip. I now read that list before trusting any cut.
Painting out the blinks
The first video, the night bat, blinked at frames 71–76. Every other bat did the same, or close to it:
| Bat | Blinks, in video frames |
|---|---|
| night | 71–76 |
| ember | 50–55 and 71–77 |
| toxic | 39–45 and 74–79 |
| frost | 27–31 and 78–83 |
| gold | 23–26 and 81–87 |
| ghost | 21–36, its glowing pupil going dark |
My first answer was to skip those frames when picking. It worked, but it threw away the in-between wing poses inside the blink, and without them the return to level had a jump in it. The wings in those frames were fine. Only the eye was wrong.
So the eye gets painted back, the way a clean-up pass fixes one drawing in a hand-drawn run. For each blinking frame:
- Take the nearest open-eyed frame on either side as the donor.
- Find the eye in the donor.
- Find how far the head moved between donor and target, by sliding a ring of fur around the eye, the eye itself left out, until it matches best.
- Paste a disc of the donor's eye at the moved position, with a feathered edge.
The method stayed the same across the six bats. Step 2 is the one that changed for almost every bat, because finding an eye depends on what the rest of the face looks like:
- Night and ember. The white of the eye is the biggest white patch on the face. That was the default.
- Toxic. White fangs and a yellow eye with very little white. Looking for white would have put the patch on the fangs, below the eye, so it looks for the yellow iris.
- Frost. White fur everywhere, so it looks for the saturated blue iris.
- Gold. A raised wing beside the head carried white glints that dragged the position off. It gets a rough hint of where the eye is and only looks near it.
- Ghost. Pale fur and a big black eye, and the "blink" was the cyan pupil going out for 16 frames. It looks for the black disc.
Every patched frame was checked in a close-up of the face. The patches follow the head and I could not find a seam in any of them.
Pink spill on the downstroke
On the fast downstroke a thin wing is motion-blurred across the magenta, and the blur carries the screen's colour. The keyer already cleaned the half-transparent edge pixels. These were fully opaque wing pixels that had kept the magenta's blue, and for a few frames every bat had pink wings.
To have a number and not an impression, I counted the solid pixels that were both bluer and redder than they were green, by a margin:
# Simplified example: the share of solid pixels that read as pink.
a = frame.astype(int)
solid = a[..., 3] > 40
r, g, b = (a[..., c][solid] for c in range(3))
pink = ((b > g + 25) & (r > g + 40)).mean()
Ember's worst frame was 14.1%.
One despill rule could not serve six palettes, so each rule starts from the colour that bat never has. Ember is orange and charcoal, and none of its own pixels is bluer than it is green. So on every pixel redder than it is blue, the blue above green is cut back by 70%. Toxic is lime and dark teal, and there the test is blue above green. The first version of that one left a pale pinkish rim, pixels a touch redder than green with blue just under it, so red had to be pulled toward green as well. Frost and ghost are white and ice blue, and their fix raises green to meet red and blue, which turns pink into white. Gold has a cream chest that the warm rule would have discoloured, so its rule gives pink and salmon pixels gold's own hue back and leaves the pale ones alone.
| Bat | Rule | Pink pixels, worst frames → after |
|---|---|---|
| ember | warm | 14% → 0.2% |
| toxic | green | about 6% → under 0.6% |
| gold, frost | gold, pale | 5–17% → 0% |
| ghost | pale | up to 21% → 0% |
The face and fur were compared before and after on each bat, by eye. Later, measured against the source video on frost, the despill step moved the solid pixels by 0.11 out of 255 on average, less than keying the background did.
Two beats where I asked for one
The prompt said exactly one wingbeat. Gold, frost and ghost flapped two or three times, and never came back to level between beats. There was no single cycle in those clips to cut.
What they did have was the same stroke more than once. So I spliced one beat together: the first rise and downstroke from the start of the clip, then the recovery from the last downstroke, back to level at the end. The join goes where the two halves look most alike. For every pair of candidate frames, one from each half, I compared the bat's silhouettes by intersection over union and kept the best pair:
iou = (a & b).sum() / (a | b).sum() # a, b: boolean silhouettes
| Bat | Join, in video frames | Overlap |
|---|---|---|
| gold | 47 → 74 | 0.80 |
| frost | 49 → 73 | 0.82 |
| ghost | 52 → 70 | 0.88 |
The splice is one ffmpeg call. select keeps the frames either side of the join and setpts
renumbers them, so the extractor sees an ordinary clip:
ffmpeg -i gold-clean.mp4 \
-vf "select='lte(n\,47)+gte(n\,74)',setpts=N/24/TB" -r 24 \
-c:v libx264 -crf 8 -pix_fmt yuv444p gold-one-beat.mp4
Gold came out at 71 frames, frost at 74 and ghost at 80. I could not spot the joins in the 24 frames picked from any of them, which is a check by eye on still frames. The overlap is the only measurement behind it.
All six bats then had their own wingbeat in the game, with the shop previews looping it, and the test suite went from 454 to 469 tests, all passing. On the Android emulator I flew night and ember in a run, and went through night's recording frame by frame. The other four I checked only in the shop, because flying them meant buying them on the test save.
One more trap, in the scripts this time. Everything was written for the first bat, and the packer took that bat's 291 px frame width as its default and wrote into the game's live asset folder unless told otherwise. Ember's frames are 337 px wide, and the first ember pack sliced them at 291 without a word. A script written for character one carries character one's numbers until someone passes in the second one's.
Five moves from one ten-second clip
A few days later I wanted more than a wingbeat: a dive, a sonar call, a hurt and a happy hop as well. The Google tool I was using, Veo, made ten seconds per clip, 240 frames at 24 fps. Five two-second moves fit in that, if every move starts and ends on the same frame.
That frame became a contract, the hub pose: the bat level, wings out, eyes open, at its usual spot and size. The prompt put the hub at 0, 2, 4, 6, 8 and 10 seconds, frames 0, 48, 96, 144, 192 and 240, with flap, dive, sonar call, hurt and happy between them. If every move begins and ends on the hub, the game can follow any move with any other without a jump.
The model treated the marks loosely. In the first take the best hub matches landed near frames 0, 41, 88, 136, 183 and 238, the flap ran 32 frames and the dive 56. Frame 41 was not a hub at all: its wings were up.
What I tried to cut it with, in order:
- Fixed time marks. Wrong as soon as the model drifts, and it drifted on every take.
- Automatic hub matching, every frame against the hub picture by shape and colour. It caught the level pass in the middle of a flap, the same mistake the wingbeat extractor had made. Searching only within half a second of each mark helped on one take and failed on the next: the night bat's video has no hub near 6 s, and the closest is at 6.6.
- Sliding and cross-fading the ends onto the hub. On ember's second take the flap's first real frame sat 9 px higher than the hub, so every wingbeat hopped up on its way out. I slid each move so its body lined up with the hub at both ends, and dissolved frames into the hub where a move ended short of it. The dissolves showed two sets of wings at once: roughly a third of ghost's frames, when I went through them, were two poses blended together. The game added to it by drawing faint trail copies of frames it skipped, which read as the same smear. On the emulator it looked worse than the build before.
What worked was the dull version:
- I read each bat's ranges off the video by eye and wrote them into one script, so a re-cut gives the same frames. The model fills the gaps between moves with motion nobody asked for, like an extra wingbeat and a second tuck after the dive, or a wind-up for the hurt at the end of the call. A person looking at the frames can see where a move's own action starts and stops.
- No blended frames. Every frame in the atlas is a frame from the video.
- A move that never gets back to the hub plays out to its key pose and back the same way. None of the five videos ends on the hub, so happy plays out and back on every bat.
- Each move's first and last frames are the same hub picture, and that was checked pixel for pixel on all five bats.
That build was the one I was happy with, apart from one thing: it sometimes stopped dead on the end frame. The stall I found was in the preview GIFs. Each GIF ended on the hub and started again on the hub, so the hub showed twice and the loop paused for a beat. Dropping the last frame of each GIF fixed it.
Two leftovers I am living with. Ghost's video never quite returns to the hub, so its step onto the hub is about twice an ordinary wingbeat step: a small pop, not a double image. And a crash still fades for a moment from whatever pose the bat hit the rock in to the start of the hurt, because there is no path through real frames from any pose to a wrap.
Small things the moves video needed
- Eyes always open. The second prompt says the eyes never blink, close, squint or curve shut in any frame, and lets the brows, ears and mouth carry the emotion. A closed eye makes a frame match the hub less closely, so cuts land in the wrong place, and the game has no blink anyway. The next ember take came back with no blinks to paint out. One take proves nothing, though, and frost's still squinted during its call, so I cannot credit the rule with it.
- The sonar call on the beat. In the audio track the chirp starts at 4.59 s, video frame 110. The call is timed so that frame is on screen as the game's sonar wave leaves the bat, and in a 60 fps emulator recording the mouth opened and the wave left within one frame of each other. The call is 1.96 s long, so the hovering bat's ping moved from every 1.5 s to every 2.5 s to fit it.
- Sound from the same clip. The clips came with audio, so each bat's wing whooshes, chirp, yelp and giggle were cut from its own video. They were picked from a loudness plot and a spectrogram, not by ear, so I count that part as unchecked.
- Things nobody asked for. Ember's hurt and happy turned to face the camera though the prompt said side-on. The night bat's video drew sound-wave arcs, which keyed out, and toxic's had a few frames with a stray "H" caption, which the cut leaves out.
What I would do from the start
- Put the same image in the start and end slots, and still do not trust the end slot to close the loop. Score the loop on a run of frames and read the score for the whole clip.
- Expect blinks whatever the prompt says, and plan to paint them. Skipping them costs the wing poses inside them.
- Measure the spill with a number, and write each despill rule from the colour that character never has.
- When a clip has two beats, splice one at the best-overlapping pair of frames. It costs less than another generation.
- For several moves in one clip, cut by hand and write the ranges down. Do not blend frames to hide a bad join; play the move out and back.
Turning these frames into something the phone can hold is the next step, and it has its own post: 10.1 MB to 2.7 MB with a texture atlas. For cutting still sprites out of a sheet an image model drew, see how to separate sprites from AI sprite sheets.