Skip to main content

Check a generated MP4 before calling it done

Updated 6 min readMarkdown

A video job can finish and still leave you with the wrong artifact: a short clip with no audio, an unexpected frame size, or a file that does not decode. A completion status is useful, but the downloaded file needs its own check.

I work on Reifora. This article uses a retained output from an earlier product test to explain a local verification step. The TypeScript program below is a standalone teaching example, not a description of Reifora's production backend or a claim that it performs these checks automatically.

Give the file its own expected specification

The sample is a silent Private Jet Exit clip. Probing that exact retained MP4 gives:

{
  "width": 768,
  "height": 1344,
  "duration": 10.125,
  "hasAudio": false
}

The public guide has the input and downloadable example. Its portrait is an AI-generated fictional adult, and the MP4 is an earlier actual product output. No new video generation was purchased to prepare this article.

Use a file you have permission to inspect. Save it as result.mp4. For this particular sample, the expected duration is 10.125 seconds, the encoded frame is 768 × 1344, and audio should be absent. Those values describe this test artifact. Another template needs its own specification.

A tolerance makes the duration comparison explicit. Here, 0.05 seconds allows a small difference around the known sample duration. It is a choice for this example, not a general recommendation for every frame rate or delivery requirement.

Probe locally, then compare

This example needs Node.js 22.18 or newer with TypeScript type stripping available, and FFmpeg's ffprobe on PATH. It uses Node's built-in modules and no environment variables or package dependencies. Save the following as audit-video.ts in your working directory.

The process receives the local filename as an argument, without a shell. There is a 15-second timeout and a one-megabyte output limit. The code selects a video by its stream type rather than assuming that the first stream is video.

import { execFileSync } from 'node:child_process';
import { statSync } from 'node:fs';
import { resolve } from 'node:path';
import { pathToFileURL } from 'node:url';

type Probe = {
  format?: { duration?: string };
  streams?: Array<{
    codec_type?: string;
    width?: number;
    height?: number;
  }>;
};

type Spec = {
  width: number;
  height: number;
  duration: number;
  tolerance: number;
  audio: 'required' | 'absent' | 'either';
};

export function inspect(file: string) {
  const path = resolve(file);
  if (!statSync(path).isFile()) throw new Error('Expected a local file');
  const raw = execFileSync('ffprobe', [
    '-v', 'error', '-show_entries',
    'format=duration:stream=codec_type,width,height',
    '-of', 'json', path,
  ], { encoding: 'utf8', timeout: 15000, maxBuffer: 1024 * 1024 });
  const probe = JSON.parse(raw) as Probe;
  const streams = probe.streams ?? [];
  const videos = streams.filter(s => s.codec_type === 'video');
  if (videos.length !== 1) throw new Error('Expected one video stream');
  const video = videos[0];
  const duration = Number(probe.format?.duration);
  if (!Number.isFinite(duration) || duration <= 0) {
    throw new Error('Missing or invalid duration');
  }
  if (!Number.isInteger(video.width) || !Number.isInteger(video.height) ||
      (video.width ?? 0) <= 0 || (video.height ?? 0) <= 0) {
    throw new Error('Missing or invalid dimensions');
  }
  return {
    width: video.width as number,
    height: video.height as number,
    duration,
    hasAudio: streams.some(s => s.codec_type === 'audio'),
  };
}

export function compare(actual: ReturnType<typeof inspect>, spec: Spec) {
  if (![spec.width, spec.height, spec.duration, spec.tolerance].every(Number.isFinite) ||
      spec.width <= 0 || spec.height <= 0 || spec.duration <= 0 || spec.tolerance < 0) {
    throw new Error('Invalid expected specification');
  }
  const failures: string[] = [];
  if (actual.width !== spec.width || actual.height !== spec.height) {
    failures.push(`dimensions: ${actual.width}x${actual.height}`);
  }
  if (Math.abs(actual.duration - spec.duration) > spec.tolerance) {
    failures.push(`duration: ${actual.duration}`);
  }
  if (spec.audio === 'required' && !actual.hasAudio) failures.push('audio missing');
  if (spec.audio === 'absent' && actual.hasAudio) failures.push('unexpected audio');
  return failures;
}

if (process.argv[1] && import.meta.url === pathToFileURL(resolve(process.argv[1])).href) {
  if (!process.argv[2]) throw new Error('Usage: node audit-video.ts ./result.mp4');
  console.log(JSON.stringify(inspect(process.argv[2]), null, 2));
}

Run it from the directory containing the script and file:

node audit-video.ts ./result.mp4

The CLI prints measurements. It deliberately does not infer a specification from a template name. To turn the measurements into an acceptance check, call the exported comparison function with values you know in advance:

import { inspect, compare } from './audit-video.ts';

const actual = inspect('./result.mp4');
const failures = compare(actual, {
  width: 768,
  height: 1344,
  duration: 10.125,
  tolerance: 0.05,
  audio: 'absent',
});

if (failures.length) throw new Error(failures.join('; '));

required means that an audio stream must exist. absent means that one must not exist. either leaves that check open. An audio stream being present says nothing about whether it is audible or contains the intended soundtrack.

Check the checker with a wrong expectation

The example passed against the retained sample with the specification above. Changing the expected width to 1920 produced a dimension failure. Changing duration to five seconds produced a duration failure. Requiring audio produced audio missing. A negative tolerance and a missing local file were rejected.

These small failures matter because a validator that only passes its happy path can hide an incorrect comparison. Keep a known file and at least one intentionally wrong specification when adapting this code.

The sample's SHA-256 is:

d1d404f01abc63317a734d35361a7a4e1865d814084c32f1f4baa622a98f2caf

That identifies the exact retained file used here. If the public sample is replaced later, compare the hash before assuming you are running the same example.

Decode after reading metadata

ffprobe reports container and stream information. It is useful for quick structural checks, but it does not establish that every packet decodes. For the same local file, a separate decode check was also run:

ffmpeg -v error -xerror -i ./result.mp4 -map 0:v:0 -map '0:a?' -f null -

The optional audio mapping accepts this silent sample. The command completed successfully for the retained file. Capture the exit status in an automated workflow; a process starting successfully is not the same as a successful decode.

For server-side processing of untrusted media, use resource limits and a maintained FFmpeg build. The local-file restriction in this small program avoids turning an input string into an arbitrary remote fetch, but does not make a media decoder a security boundary.

Record the limits of the result

This example compares encoded dimensions. It does not account for display rotation or non-square pixels; normalize those inputs or extend the check before using it for arbitrary uploads. It also requires exactly one video stream, which deliberately excludes some otherwise valid multimedia files.

The measurements do not assess face likeness, hand shape, shot continuity, subtitles or music rights. Watch the content for those questions. A useful record can say “metadata matched the sample specification; full decode passed; visual review still pending.” That gives the next person a clear account of what was actually checked.

Technical reference: ffprobe documentation and FFmpeg documentation. This article and its teaching code were prepared with AI assistance, reviewed against those references, and exercised locally as described above.

About the author

  • Independent developer building Reifora.

No comments yet. Be the first to comment.