Video matting
Use @remotion/video-matting to separate a source video into an opaque base and
a foreground WebM with transparency. Process the source once and reuse the
outputs in compositions; do not run the model during frame rendering.
Generate the layers
Reuse an existing foreground if it was generated from the same source and has matching timing and dimensions.
In Remotion Studio, select a video and choose Remove background. This
replaces its source with a transparent WebM. Duplicate the layer first when
the original video is needed underneath. Agents with Studio WebMCP access can
also use remove_video_background.
For local files in Node.js:
- Install
@remotion/video-matting,@huggingface/transformers,mediabunny, and@mediabunny/serverwithremotion addusing the project's package manager. - Call
registerMediabunnyServer()from@mediabunny/serverbefore processing. - Choose
modnetfor people. Use the experimentalben2-basemodel for general foreground subjects; it requires more GPU memory and fp16 support. - Call
canUseVideoMatting({model}). If unsupported, report itsdetailedReasoninstead of promising a result. A compatible GPU is required. - Call
downloadVideoMattingModel({model}), thenseparateVideoLayers({src, model, audio: 'base'}).srcmay be a local path. - Retrieve each output with
base.getBlob()andforeground.getBlob()and write the bytes to.webmfiles inpublic/. Dispose the outputs after saving them. CalldisposeVideoMattingModel({model})when no more processing is needed.
See the Node.js setup and
separateVideoLayers() reference
for output streams, progress reporting, and disposal APIs. Custom output streams
must honor each chunk's position; writes are not necessarily append-only.
Compose the result
Use <Video> from @remotion/media for both layers and staticFile() for saved
assets. JSX order determines stacking, from back to front:
- Text behind a subject: original or base video, an
Interactive.Divwith editable text, then the foreground. - Background blur: original or base video with
blur()from@remotion/effects/blur, then the sharp foreground. - Background replacement: a new background, then the foreground. Omit the original video.
- Subject-only effects: apply effects such as
glow(),dropShadow(), orcolorCorrection()to the foreground. Keep the original video underneath when its background should remain visible.
The base still contains the subject. Matting does not reconstruct what was hidden behind it; strong background blur may leave a halo around the subject.
Keep both layers' position, dimensions, objectFit, cropping, from,
trimBefore, durationInFrames, and playbackRate aligned. Play the original
audio once: mute the foreground when the base has audio. For replacement
backgrounds, use audio: 'foreground' during separation or add the original
audio separately. Preserve the foreground's alpha-capable WebM format.
Preview the composition and scrub through motion to check synchronization and the edges of hair or moving objects. See the video matting guide for complete composition examples.