// Verificação do MP4 com FFprobe (contagem real de quadros decodificados). import { runProcess, succeeded, describeFailure } from "./process.ts"; import type { Tool } from "./tools.ts"; export type ProbeSummary = { formatName: string; sizeBytes: number; durationSeconds: number; video: { codec: string; width: number; height: number; pixFmt: string; rFrameRate: string; avgFrameRate: string; frames: number; } | null; audio: { codec: string; durationSeconds: number } | null; }; export type Expectation = { width?: number; height?: number; fps?: number; durationSeconds?: number; frames?: number; audio?: boolean; }; export type Check = { name: string; ok: boolean; detail: string }; export async function probeVideo(ffprobe: Tool, file: string): Promise { const r = await runProcess(ffprobe.path, [ "-v", "error", "-count_frames", "-show_entries", "stream=codec_type,codec_name,width,height,pix_fmt,r_frame_rate,avg_frame_rate,nb_read_frames,duration:format=duration,format_name,size", "-of", "json", file, ], { timeoutMs: 120_000 }); if (!succeeded(r)) throw new Error(describeFailure("ffprobe", r)); return parseProbe(r.stdout); } export function parseProbe(json: string): ProbeSummary { type RawStream = Record; const raw = JSON.parse(json) as { streams?: RawStream[]; format?: Record }; const streams = raw.streams ?? []; const v = streams.find((s) => s.codec_type === "video"); const a = streams.find((s) => s.codec_type === "audio"); return { formatName: raw.format?.format_name ?? "", sizeBytes: Number(raw.format?.size ?? 0), durationSeconds: Number(raw.format?.duration ?? NaN), video: v ? { codec: String(v.codec_name), width: Number(v.width), height: Number(v.height), pixFmt: String(v.pix_fmt), rFrameRate: String(v.r_frame_rate), avgFrameRate: String(v.avg_frame_rate), frames: Number(v.nb_read_frames), } : null, audio: a ? { codec: String(a.codec_name), durationSeconds: Number(a.duration ?? NaN) } : null, }; } export function evaluateProbe(p: ProbeSummary, e: Expectation): Check[] { const checks: Check[] = []; const add = (name: string, ok: boolean, detail: string) => checks.push({ name, ok, detail }); add("arquivo não vazio", p.sizeBytes > 0, `${p.sizeBytes} bytes`); add("contêiner MP4", p.formatName.includes("mp4"), p.formatName || "desconhecido"); if (!p.video) { add("stream de vídeo", false, "nenhum stream de vídeo"); return checks; } add("codec H.264", p.video.codec === "h264", p.video.codec); add("pixel format yuv420p", p.video.pixFmt === "yuv420p", p.video.pixFmt); add("quadros decodificados > 0", p.video.frames > 0, `${p.video.frames}`); if (e.width !== undefined && e.height !== undefined) { add("dimensões", p.video.width === e.width && p.video.height === e.height, `${p.video.width}x${p.video.height} (esperado ${e.width}x${e.height})`); } if (e.fps !== undefined) { const fps = ratio(p.video.rFrameRate); add("FPS", Math.abs(fps - e.fps) < 1e-6, `${p.video.rFrameRate} (esperado ${e.fps}/1)`); } if (e.frames !== undefined) { add("total de quadros", p.video.frames === e.frames, `${p.video.frames} (esperado ${e.frames})`); } if (e.durationSeconds !== undefined) { // Tolerância de um quadro, conforme o critério de aceite. const tolerance = e.fps ? 1 / e.fps : 1 / 30; const diff = Math.abs(p.durationSeconds - e.durationSeconds); add("duração (±1 quadro)", diff <= tolerance + 1e-9, `${p.durationSeconds.toFixed(4)} s (esperado ${e.durationSeconds} s, diferença ${diff.toFixed(4)} s)`); } if (e.audio !== undefined) { add(e.audio ? "stream de áudio presente" : "sem áudio", e.audio === (p.audio !== null), p.audio ? `${p.audio.codec}, ${p.audio.durationSeconds} s` : "nenhum"); } return checks; } function ratio(r: string): number { const [n, d] = r.split("/").map(Number); return d ? n / d : n; }