{"record":{"id":"d1b767e9bfb9fa6a","repo":"remotion-dev/remotion","slug":"the-video-matting-model-returned-invalid-dimension","errorCode":null,"errorMessage":"The video matting model returned invalid dimensions.","messagePattern":"The video matting model returned invalid dimensions\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/video-matting/src/video-matting-canvas.ts","lineNumber":82,"sourceCode":"\tcontext,\n\tresult,\n\tsource,\n\ttargetWidth,\n\ttargetHeight,\n}: {\n\tcontext: VideoMattingCanvasContext;\n\tresult: VideoMattingPipelineResult;\n\tsource: VideoMattingCanvas;\n\ttargetWidth: number;\n\ttargetHeight: number;\n}) => {\n\tif (\n\t\t!Number.isInteger(result.width) ||\n\t\tresult.width <= 0 ||\n\t\t!Number.isInteger(result.height) ||\n\t\tresult.height <= 0\n\t) {\n\t\tthrow new Error('The video matting model returned invalid dimensions.');\n\t}\n\n\tconst expectedLength = result.width * result.height * result.channels;\n\tif (result.data.length !== expectedLength) {\n\t\tthrow new Error(\n\t\t\t`The video matting model returned ${result.data.length} bytes, but ${expectedLength} RGBA bytes were expected.`,\n\t\t);\n\t}\n\n\tconst imageData = new ImageData(result.data, result.width, result.height);\n\n\tcontext.clearRect(0, 0, targetWidth, targetHeight);\n\tif (result.width === targetWidth && result.height === targetHeight) {\n\t\tcontext.putImageData(imageData, 0, 0);\n\t} else {\n\t\tconst intermediateCanvas = createVideoMattingCanvas({\n\t\t\twidth: result.width,\n\t\t\theight: result.height,","sourceCodeStart":64,"sourceCodeEnd":100,"githubUrl":"https://github.com/remotion-dev/remotion/blob/b2f4e34732f3c6c222ea029413e22dc402218cd7/packages/video-matting/src/video-matting-canvas.ts#L64-L100","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Log result.width/result.height before calling drawForegroundFrame to identify the failing frame and source model","Verify the model input/output configuration matches the video resolution (no downscale to 0 from a 0-sized <video>)","Check that the video element is loaded and has intrinsic dimensions before running matting (readyState >= 2, videoWidth > 0)","Update or re-export the matting model; test the same clip with a known-good model","Wrap the call in try/catch and skip/fallback the affected frame instead of failing the render"],"exampleFix":"// before\nconst result = await runMatting(video);\ndrawForegroundFrame({context, result});\n// after\nconst result = await runMatting(video);\nif (!Number.isInteger(result.width) || result.width <= 0 || !Number.isInteger(result.height) || result.height <= 0) {\n  throw new Error(`Invalid matting frame dims: ${result.width}x${result.height}`);\n}\ndrawForegroundFrame({context, result});","handlingStrategy":"validation","validationCode":"const isDrawable = (r) => Number.isInteger(r.width) && r.width > 0 && Number.isInteger(r.height) && r.height > 0;\nif (!isDrawable(result)) throw new Error('Model returned invalid dimensions');","typeGuard":"const hasValidDims = (r: {width: number; height: number; data: Uint8ClampedArray; channels: number}): r is MattingResult =>\n  Number.isInteger(r.width) && r.width > 0 && Number.isInteger(r.height) && r.height > 0;","tryCatchPattern":"try {\n  drawForegroundFrame({context, result});\n} catch (err) {\n  if (err instanceof Error && err.message.includes('invalid dimensions')) {\n    console.warn('Skipping frame: invalid matting dimensions');\n    return;\n  }\n  throw err;\n}","preventionTips":["Check video.videoWidth/videoHeight > 0 before running inference","Assert model output metadata matches input resolution each frame","Test the matting pipeline against a known-good model build","Log the first invalid result and abort early rather than looping on bad output"],"tags":["video-matting","validation","dimensions"],"backgroundTag":"invalid-argument-value","analyzedSha":"b2f4e34732f3c6c222ea029413e22dc402218cd7","analyzedAt":"2026-09-09T13:27:51.281Z","contentChangedAt":"2026-09-09T13:27:51.281Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}