Using @remotion/video-matting in Node.jsv4.0.528
You can use @remotion/video-matting in Node.js to separate a local video into an opaque base and a transparent foreground.
Register @mediabunny/server to enable video decoding, VP9 encoding with alpha, and audio processing. A browser or canvas polyfill is not required.
Installation
- npm
- bun
- pnpm
- yarn
This assumes you are currently using v4.0.527 of Remotion.npm i --save-exact @remotion/[email protected] @huggingface/transformers mediabunny @mediabunny/server
Also update
remotion and all `@remotion/*` packages to the same version.Remove all
^ character in front of the version numbers of it as it can lead to a version conflict.This assumes you are currently using v4.0.527 of Remotion.pnpm i @remotion/[email protected] @huggingface/transformers mediabunny @mediabunny/server
Also update
remotion and all `@remotion/*` packages to the same version.Remove all
^ character in front of the version numbers of it as it can lead to a version conflict.This assumes you are currently using v4.0.527 of Remotion.bun i @remotion/[email protected] @huggingface/transformers mediabunny @mediabunny/server
Also update
remotion and all `@remotion/*` packages to the same version.Remove all
^ character in front of the version numbers of it as it can lead to a version conflict.This assumes you are currently using v4.0.527 of Remotion.yarn --exact add @remotion/[email protected] @huggingface/transformers mediabunny @mediabunny/server
Also update
remotion and all `@remotion/*` packages to the same version.Remove all
^ character in front of the version numbers of it as it can lead to a version conflict.Example
separate-video.tsimport {registerMediabunnyServer } from '@mediabunny/server'; import {canUseVideoMatting ,downloadVideoMattingModel ,loadVideoMattingModel ,separateVideoLayers , } from '@remotion/video-matting'; import {writeFile } from 'node:fs/promises';registerMediabunnyServer (); constmodel = 'modnet'; constsupport = awaitcanUseVideoMatting ({model }); if (!support .supported ) { throw newError (support .detailedReason ); } awaitdownloadVideoMattingModel ({model }); // In long-running apps, put this code in an async function so resources // are released when that function finishes. await usingmodelHandle = awaitloadVideoMattingModel ({model }); await usinglayers = awaitseparateVideoLayers ({src : './input.mp4',model ,audio : 'base',onProgress : ({processedFrames }) =>console .log (processedFrames ), }); awaitwriteFile ( 'base.webm', newUint8Array (await (awaitlayers .base .getBlob ()).arrayBuffer ()), ); awaitwriteFile ( 'foreground.webm', newUint8Array (await (awaitlayers .foreground .getBlob ()).arrayBuffer ()), );
separateVideoLayers() accepts local file paths, file: URLs, remote URLs, and Blob inputs. Outputs are held in memory by default. Use outputWritable to stream large outputs; handle each chunk's position because writes are not necessarily append-only.
Model cache
Model files use the Transformers.js filesystem cache. To choose the directory, set env.cacheDir before downloading or loading models:
model-cache.tsimport {env } from '@huggingface/transformers'; import {resolve } from 'node:path';env .cacheDir =resolve ('.cache/video-matting');
removeVideoMattingModel() removes cached files. Disposing a loaded model releases its memory and keeps the downloaded files.
Requirements
A compatible GPU is required. Transformers.js uses ONNX Runtime's native WebGPU provider in Node.js. canUseVideoMatting() runs a tiny inference to check support, including fp16 computation for ben2-base.
The native ONNX Runtime dependency does not support Linux arm64. GPU availability also depends on the host and its drivers; a typical serverless environment does not provide a GPU.