remotion-dev/remotion · error · Error

The video matting model returned invalid dimensions.

Error message

The video matting model returned invalid dimensions.

What it means

drawForegroundFrame validates that the matting model's result has integer, positive width and height before writing its RGBA buffer into an ImageData on the canvas. If the model produces a non-integer, zero, or negative dimension, the frame is rejected rather than drawn corrupted. This guards the downstream ImageData constructor and per-pixel drawing from impossible geometry.

Source

Thrown at packages/video-matting/src/video-matting-canvas.ts:82

	context,
	result,
	source,
	targetWidth,
	targetHeight,
}: {
	context: VideoMattingCanvasContext;
	result: VideoMattingPipelineResult;
	source: VideoMattingCanvas;
	targetWidth: number;
	targetHeight: number;
}) => {
	if (
		!Number.isInteger(result.width) ||
		result.width <= 0 ||
		!Number.isInteger(result.height) ||
		result.height <= 0
	) {
		throw new Error('The video matting model returned invalid dimensions.');
	}

	const expectedLength = result.width * result.height * result.channels;
	if (result.data.length !== expectedLength) {
		throw new Error(
			`The video matting model returned ${result.data.length} bytes, but ${expectedLength} RGBA bytes were expected.`,
		);
	}

	const imageData = new ImageData(result.data, result.width, result.height);

	context.clearRect(0, 0, targetWidth, targetHeight);
	if (result.width === targetWidth && result.height === targetHeight) {
		context.putImageData(imageData, 0, 0);
	} else {
		const intermediateCanvas = createVideoMattingCanvas({
			width: result.width,
			height: result.height,

View on GitHub (pinned to b2f4e34732)

Solutions

  1. Log result.width/result.height before calling drawForegroundFrame to identify the failing frame and source model
  2. Verify the model input/output configuration matches the video resolution (no downscale to 0 from a 0-sized <video>)
  3. Check that the video element is loaded and has intrinsic dimensions before running matting (readyState >= 2, videoWidth > 0)
  4. Update or re-export the matting model; test the same clip with a known-good model
  5. Wrap the call in try/catch and skip/fallback the affected frame instead of failing the render

Example fix

// before
const result = await runMatting(video);
drawForegroundFrame({context, result});
// after
const result = await runMatting(video);
if (!Number.isInteger(result.width) || result.width <= 0 || !Number.isInteger(result.height) || result.height <= 0) {
  throw new Error(`Invalid matting frame dims: ${result.width}x${result.height}`);
}
drawForegroundFrame({context, result});
Defensive patterns

Strategy: validation

Validate before calling

const isDrawable = (r) => Number.isInteger(r.width) && r.width > 0 && Number.isInteger(r.height) && r.height > 0;
if (!isDrawable(result)) throw new Error('Model returned invalid dimensions');

Type guard

const hasValidDims = (r: {width: number; height: number; data: Uint8ClampedArray; channels: number}): r is MattingResult =>
  Number.isInteger(r.width) && r.width > 0 && Number.isInteger(r.height) && r.height > 0;

Try / catch

try {
  drawForegroundFrame({context, result});
} catch (err) {
  if (err instanceof Error && err.message.includes('invalid dimensions')) {
    console.warn('Skipping frame: invalid matting dimensions');
    return;
  }
  throw err;
}

Prevention

When it happens

Trigger: The video matting model inference result has width or height that is NaN, fractional, 0, or negative (e.g. a model emitted a malformed metadata header, or a degraded/on-device model returned a failed frame).

Common situations: Corrupted or truncated model output, an unsupported/quantized model variant emitting 0-sized frames on the first inference, or hardware/driver issues in WebGPU/WASM backends producing empty tensors.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of remotion-dev/remotion@b2f4e34732 (2026-09-09). Data as JSON: /api/errors/d1b767e9bfb9fa6a. Report an issue: GitHub.