export type VerifyExpect = { fps?: number width?: number height?: number requireAudio?: boolean requireSilent?: boolean minDurationSeconds?: number maxDurationSeconds?: number } export type MarkedAlignmentCheck = { outputOnsetSeconds: number expectedFrame: number fps: number } export type VerifyVideoInput = { probe: unknown expect?: VerifyExpect reviewFrames?: string[] markedAlignment?: MarkedAlignmentCheck } export type StreamSummary = { codecType: string codecName?: string width?: number height?: number fps?: number durationSeconds?: number nbFrames?: number } export type VerifyVideoResult = { format: { formatName?: string durationSeconds?: number sizeBytes?: number bitRate?: number } video?: StreamSummary audio?: StreamSummary hasAudio: boolean frameCount?: number frameCountApproximate: boolean checks: Array<{ name: string; ok: boolean; detail: string }> checklist: string[] reviewStatus: 'pending_human_inspection' approved: false listeningVerified: false markedAlignment?: { withinOneFrame: boolean errorSeconds: number expectedSeconds: number } } type ProbeStream = { codec_type?: string codec_name?: string width?: number height?: number avg_frame_rate?: string r_frame_rate?: string duration?: string nb_frames?: string nb_read_frames?: string } type ProbeFormat = { format_name?: string duration?: string size?: string bit_rate?: string } type ProbeJson = { streams?: ProbeStream[] format?: ProbeFormat } function parseRate(rate: string | undefined): number | undefined { if (!rate || rate === '0/0') return undefined const parts = rate.split('/') if (parts.length === 2) { const num = Number(parts[0]) const den = Number(parts[1]) if (Number.isFinite(num) && Number.isFinite(den) && den !== 0) { return num / den } } const asNumber = Number(rate) return Number.isFinite(asNumber) ? asNumber : undefined } function asNumber(value: string | undefined): number | undefined { if (value === undefined) return undefined const n = Number(value) return Number.isFinite(n) ? n : undefined } function summarizeStream(stream: ProbeStream): StreamSummary { return { codecType: stream.codec_type ?? 'unknown', codecName: stream.codec_name, width: stream.width, height: stream.height, fps: parseRate(stream.avg_frame_rate) ?? parseRate(stream.r_frame_rate), durationSeconds: asNumber(stream.duration), nbFrames: asNumber(stream.nb_frames) ?? asNumber(stream.nb_read_frames), } } function defaultChecklist(reviewFrames: string[] | undefined): string[] { const base = [ 'Inspect opening frame / promise card', 'Inspect a mid-workflow cut with readable UI or code', 'Inspect product-specific proof beat', 'Inspect brand / URL reveal frame', 'Inspect final end card', 'Play the real MP4 end-to-end before delivery', ] if (reviewFrames?.length) { return [ ...base, ...reviewFrames.map((path) => `Human-inspect extracted frame: ${path}`), ] } return base } /** * Summarize ffprobe JSON for a product promo MP4 and build a human review checklist. * Use after render: check duration/codec/resolution/fps/audio against expectations. * Never marks review approved from probe alone — status stays pending until frames * are inspected on the real file. * * @param input - probe JSON, optional expect/reviewFrames/markedAlignment * @returns Parsed details, requirement checks, pending checklist, never auto-approved * * @example * import verifyVideo from 'kody:@kody/skills/verify-video' * * const summary = await verifyVideo({ * probe: ffprobeJson, * expect: { fps: 30, width: 1920, height: 1080, requireAudio: true }, * reviewFrames: ['frames/open.png', 'frames/reveal.png'], * }) */ export default function verifyVideo(input: VerifyVideoInput): VerifyVideoResult { if (!input || typeof input !== 'object') { throw new Error('input object is required') } const allowed = new Set(['probe', 'expect', 'reviewFrames', 'markedAlignment']) const unknown = Object.keys(input).filter((key) => !allowed.has(key)) if (unknown.length) { throw new Error(`unknown input key(s): ${unknown.join(', ')}`) } if (input.probe === undefined || input.probe === null) { throw new Error('probe is required') } const probe = input.probe as ProbeJson if (typeof probe !== 'object') { throw new Error('probe must be an object (ffprobe JSON)') } const streams = Array.isArray(probe.streams) ? probe.streams : [] const format = probe.format ?? {} const videoStream = streams.find((s) => s.codec_type === 'video') const audioStream = streams.find((s) => s.codec_type === 'audio') const video = videoStream ? summarizeStream(videoStream) : undefined const audio = audioStream ? summarizeStream(audioStream) : undefined const durationSeconds = asNumber(format.duration) ?? video?.durationSeconds ?? audio?.durationSeconds const fps = video?.fps let frameCount: number | undefined let frameCountApproximate = false if (video?.nbFrames !== undefined) { frameCount = video.nbFrames } else if (durationSeconds !== undefined && fps !== undefined) { frameCount = Math.round(durationSeconds * fps) frameCountApproximate = true } const checks: VerifyVideoResult['checks'] = [] const expect = input.expect ?? {} if (expect.requireAudio && expect.requireSilent) { throw new Error('expect.requireAudio and expect.requireSilent conflict') } if (expect.fps !== undefined && fps !== undefined) { checks.push({ name: 'fps', ok: Math.abs(fps - expect.fps) < 0.01, detail: `got ${fps}, expect ${expect.fps}`, }) } else if (expect.fps !== undefined) { checks.push({ name: 'fps', ok: false, detail: 'fps missing from probe' }) } if (expect.width !== undefined) { checks.push({ name: 'width', ok: video?.width === expect.width, detail: `got ${video?.width ?? 'missing'}, expect ${expect.width}`, }) } if (expect.height !== undefined) { checks.push({ name: 'height', ok: video?.height === expect.height, detail: `got ${video?.height ?? 'missing'}, expect ${expect.height}`, }) } if (expect.requireAudio) { checks.push({ name: 'audio', ok: Boolean(audioStream), detail: audioStream ? `audio codec ${audioStream.codec_name}` : 'no audio stream', }) } if (expect.requireSilent) { checks.push({ name: 'silent', ok: !audioStream, detail: audioStream ? 'audio stream present' : 'no audio stream', }) } if (expect.minDurationSeconds !== undefined && durationSeconds !== undefined) { checks.push({ name: 'minDuration', ok: durationSeconds + 1e-6 >= expect.minDurationSeconds, detail: `got ${durationSeconds}, min ${expect.minDurationSeconds}`, }) } if (expect.maxDurationSeconds !== undefined && durationSeconds !== undefined) { checks.push({ name: 'maxDuration', ok: durationSeconds - 1e-6 <= expect.maxDurationSeconds, detail: `got ${durationSeconds}, max ${expect.maxDurationSeconds}`, }) } let markedAlignment: VerifyVideoResult['markedAlignment'] if (input.markedAlignment) { const expectedSeconds = input.markedAlignment.expectedFrame / input.markedAlignment.fps const errorSeconds = input.markedAlignment.outputOnsetSeconds - expectedSeconds markedAlignment = { expectedSeconds: Number(expectedSeconds.toFixed(5)), errorSeconds: Number(errorSeconds.toFixed(5)), withinOneFrame: Math.abs(errorSeconds) <= 1 / input.markedAlignment.fps + Number.EPSILON, } checks.push({ name: 'markedAlignment', ok: markedAlignment.withinOneFrame, detail: `error ${markedAlignment.errorSeconds}s (limit ${1 / input.markedAlignment.fps}s)`, }) } const checklist = defaultChecklist(input.reviewFrames) if (frameCountApproximate) { checklist.unshift( 'Frame count is approximate (duration×fps); do not treat it as an exact nb_frames read', ) } checklist.push( 'ffprobe validity alone must not approve delivery — complete human frame/playback review', ) return { format: { formatName: format.format_name, durationSeconds, sizeBytes: asNumber(format.size), bitRate: asNumber(format.bit_rate), }, video, audio, hasAudio: Boolean(audioStream), frameCount, frameCountApproximate, checks, checklist, reviewStatus: 'pending_human_inspection', approved: false, listeningVerified: false, markedAlignment, } }