1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
#!/usr/bin/env python3
"""Turn a sl0pboy `.gbv` frame capture into an animated GIF.
Capture one with the emulator's control socket:
./gbctl record start /abs/path/out.gbv 2 # 2 = capture every 2nd frame (~30fps)
...play...
./gbctl record stop
Then:
python3 tools/gbgif.py out.gbv out.gif [--scale 3] [--fps 30] [--start N] [--max N]
The .gbv format is a 10-byte header ("GBCV", u16 width, u16 height, u16
ms-per-frame, little-endian) followed by raw width*height*3 RGB frames.
"""
import argparse, struct, sys
from PIL import Image
def load(path):
with open(path, "rb") as f:
data = f.read()
if data[:4] != b"GBCV":
sys.exit("not a GBCV capture (bad magic)")
w, h, ms = struct.unpack_from("<HHH", data, 4)
off, fsize = 10, w * h * 3
while off + fsize <= len(data):
yield w, h, ms, memoryview(data)[off:off + fsize]
off += fsize
def main():
ap = argparse.ArgumentParser(description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
ap.add_argument("input")
ap.add_argument("output", help="GIF path (or a dir/prefix if --png)")
ap.add_argument("--scale", type=int, default=3, help="integer pixel zoom (default 3)")
ap.add_argument("--fps", type=float, default=0, help="override playback fps (0=use capture)")
ap.add_argument("--start", type=int, default=0, help="skip the first N frames")
ap.add_argument("--max", type=int, default=0, help="keep at most N frames (0=all)")
ap.add_argument("--stride", type=int, default=1, help="keep 1 of every N frames")
ap.add_argument("--png", action="store_true", help="dump PNG frames as <output>NNNN.png instead of a GIF")
args = ap.parse_args()
frames, ms_hdr = [], 17
for i, (w, h, ms, buf) in enumerate(load(args.input)):
ms_hdr = ms
if i < args.start or (i - args.start) % args.stride:
continue
if args.max and len(frames) >= args.max:
break
im = Image.frombytes("RGB", (w, h), bytes(buf))
if args.scale != 1:
im = im.resize((w * args.scale, h * args.scale), Image.NEAREST)
frames.append(im)
if not frames:
sys.exit("no frames matched")
if args.png:
for i, im in enumerate(frames):
im.save(f"{args.output}{i:04d}.png")
print(f"wrote {len(frames)} PNG frames -> {args.output}NNNN.png")
return
dur = (1000.0 / args.fps) if args.fps > 0 else max(ms_hdr, 1) * args.stride
# Build one shared palette from a montage of sampled frames so the GIF
# doesn't flicker between per-frame palettes (GB uses few colors anyway).
sample = frames[:: max(1, len(frames) // 8)][:8] or frames[:1]
fw, fh = frames[0].size
montage = Image.new("RGB", (fw, fh * len(sample)))
for i, s in enumerate(sample):
montage.paste(s, (0, i * fh))
pal = montage.quantize(colors=256, method=Image.MEDIANCUT)
pframes = [f.quantize(palette=pal, dither=Image.NONE) for f in frames]
pframes[0].save(args.output, save_all=True, append_images=pframes[1:],
duration=dur, loop=0, disposal=1, optimize=True)
print(f"{args.output}: {len(frames)} frames, {fw}x{fh}, {1000.0/dur:.1f} fps")
if __name__ == "__main__":
main()
|