Skip to content
Cloudflare Docs

Video Background

Add video background effects and blur to participant video feeds in your RealtimeKit meetings using the Core SDK.

Installation

Terminal window
npm i @cloudflare/realtimekit-virtual-background

Usage

1. Disable default per frame rendering

Disable the default per frame rendering of video middleware to improve speed and quality by letting this middleware control it on its own:

JavaScript
await meeting.self.setVideoMiddlewareGlobalConfig({
disablePerFrameCanvasRendering: true
});

2. Initialize the transformer

Create a video background transformer object:

JavaScript
import RealtimeKitVideoBackgroundTransformer from '@cloudflare/realtimekit-virtual-background';
const videoBackgroundTransformer = await RealtimeKitVideoBackgroundTransformer.init({
meeting,
});

3. Apply background effects

The videoBackgroundTransformer exposes two types of middlewares:

Static background image

Use createStaticBackgroundVideoMiddleware to set an image as the background:

JavaScript
const imageUrl = 'https://images.unsplash.com/photo-1487088678257-3a541e6e3922';
meeting.self.addVideoMiddleware(
await videoBackgroundTransformer.createStaticBackgroundVideoMiddleware(imageUrl)
);

Background blur

Use createBackgroundBlurVideoMiddleware to blur the background. Pass blurStrength (0-100) as a parameter (50% by default):

JavaScript
meeting.self.addVideoMiddleware(
await videoBackgroundTransformer.createBackgroundBlurVideoMiddleware(50)
);

Browser support

Check browser support before initializing:

JavaScript
if (RealtimeKitVideoBackgroundTransformer.isSupported()) {
const videoBackgroundTransformer = await RealtimeKitVideoBackgroundTransformer.init({
meeting: meeting,
});
meeting.self.addVideoMiddleware(
await videoBackgroundTransformer.createStaticBackgroundVideoMiddleware(imageUrl)
);
}

Advanced configuration

For better, sharper results, pass a custom segmentation configuration:

JavaScript
const videoBackgroundTransformer = await RealtimeKitVideoBackgroundTransformer.init({
meeting,
segmentationConfig: {
model: 'mlkit', // 'meet' | 'mlkit'
backend: 'wasmSimd',
inputResolution: '256x256', // '256x144' for meet
pipeline: 'webgl2', // 'webgl2' | 'canvas2dCpu'
// canvas2dCpu gives sharper blur, webgl2 is faster
targetFps: 35,
}
});