// Conferência da sequência PNG do Movie Maker antes da conversão.
// Padrão confirmado na Godot 4.7.2: %08d.png começando em 0, mais .wav.
import fs from "node:fs";
import path from "node:path";
export const FRAME_PATTERN = /^frame(\d{8})\.png$/;
export const FFMPEG_FRAME_PATTERN = "frame%08d.png";
/** PNG de 540x960 com conteúdo real tem dezenas de KB; abaixo disto é suspeito. */
const MIN_FRAME_BYTES = 2048;
export type FrameCheck = {
ok: boolean;
count: number;
firstIndex: number | null;
lastIndex: number | null;
problems: string[];
firstSceneFrameSize?: { width: number; height: number };
lastFrameSize?: { width: number; height: number };
};
export function checkFrames(dir: string, expectedTotal: number, expectedSize: { width: number; height: number }, firstSceneIndex: number): FrameCheck {
const problems: string[] = [];
const indices = fs.existsSync(dir)
? fs.readdirSync(dir).map((f) => FRAME_PATTERN.exec(f)).filter((m): m is RegExpExecArray => m !== null).map((m) => Number(m[1])).sort((a, b) => a - b)
: [];
const check: FrameCheck = {
ok: false,
count: indices.length,
firstIndex: indices[0] ?? null,
lastIndex: indices.at(-1) ?? null,
problems,
};
if (indices.length === 0) {
problems.push("nenhum PNG no padrão frame%08d.png");
return check;
}
if (indices[0] !== 0) problems.push(`primeiro índice é ${indices[0]}, esperado 0`);
for (let i = 1; i < indices.length; i++) {
if (indices[i] !== indices[i - 1] + 1) {
problems.push(`sequência com lacuna entre ${indices[i - 1]} e ${indices[i]}`);
break;
}
}
if (indices.length !== expectedTotal) problems.push(`${indices.length} PNGs, esperado ${expectedTotal}`);
const fileOf = (i: number) => path.join(dir, `frame${String(i).padStart(8, "0")}.png`);
if (firstSceneIndex <= (check.lastIndex ?? -1)) {
check.firstSceneFrameSize = readPngSize(fileOf(firstSceneIndex)) ?? undefined;
check.lastFrameSize = readPngSize(fileOf(check.lastIndex!)) ?? undefined;
for (const [label, size] of [["primeiro quadro da cena", check.firstSceneFrameSize], ["último quadro", check.lastFrameSize]] as const) {
if (!size) problems.push(`${label}: PNG ilegível`);
else if (size.width !== expectedSize.width || size.height !== expectedSize.height) {
problems.push(`${label}: ${size.width}x${size.height}, esperado ${expectedSize.width}x${expectedSize.height}`);
}
}
}
const tiny = indices.filter((i) => fs.statSync(fileOf(i)).size < MIN_FRAME_BYTES);
if (tiny.length > 0) problems.push(`${tiny.length} PNG(s) suspeitos de vazio (< ${MIN_FRAME_BYTES} bytes), ex.: índice ${tiny[0]}`);
check.ok = problems.length === 0;
return check;
}
/** Lê largura/altura do cabeçalho IHDR, sem decodificar a imagem. */
export function readPngSize(file: string): { width: number; height: number } | null {
try {
const fd = fs.openSync(file, "r");
const buf = Buffer.alloc(24);
fs.readSync(fd, buf, 0, 24, 0);
fs.closeSync(fd);
const signature = buf.subarray(0, 8).toString("hex");
if (signature !== "89504e470d0a1a0a" || buf.subarray(12, 16).toString("ascii") !== "IHDR") return null;
return { width: buf.readUInt32BE(16), height: buf.readUInt32BE(20) };
} catch {
return null;
}
}