Background Virtualization and Virtual Background
Feature Overview
The background virtualization and virtual background features can separate the subject from the original video image and process the areas outside the subject as follows:
- Blur the areas outside the subject
- Replace the areas outside the subject with a custom image
Background virtualization and virtual background features are widely suitable for the following scenarios:
- Online meetings
- Remote education
- Video calls
Using background virtualization and virtual background features helps to:
- Protect personal privacy
- Avoid cluttered backgrounds from interfering with the audience
- Enhance the fun of interaction
Effect Demonstration
| Feature | Effect |
|---|---|
| Background Blur-Weak | ![]() |
| Background Blur-Medium | ![]() |
| Background Blur-Strong | ![]() |
| Virtual Background | ![]() |
To experience the actual effect, please visit the Background Virtualization and Virtual Background Demo.
Compatibility
The browsers that support background virtualization and virtual background features are as follows:
| Browser | Compatible Version |
|---|---|
| Google Chrome | 90 and above |
| Firefox | 111 and above |
| Safari | 15 and above |
| Mobile browsers | Not supported |
| WeChat embedded webpage | Not supported |
Notes
- Background virtualization and virtual background features have high requirements for device performance. It is recommended to enable the virtual background feature on devices that meet the following conditions:
- Equipped with Core i5 or above CPU.
- Equipped with 8GB or more RAM.
- To better use background virtualization and virtual background features, it is recommended to use the latest version of the desktop Google Chrome browser.
- Background virtualization and virtual background features are paid features. If you need to apply for a trial or inquire about official pricing, please contact ZEGOCLOUD sales.
Example Source Code
Please refer to Download Example Source Code to obtain the source code.
For related source code, please view files in the "src/Examples/Others/VirtualBackground" directory.
Prerequisites
- You have created a project in the ZEGOCLOUD Console and applied for a valid AppID and Server address. For details, please refer to Console - Project Information.
- You have integrated ZEGO Express SDK in your project and implemented basic audio and video stream publishing and playing functions. For details, please refer to Quick Start - Integration and Quick Start - Implementation.
- To use this feature, please contact ZEGOCLOUD technical support.
Implementation Flow
-
Integrate the background processing module.
- Method 1: Integrate using npm
import { ZegoExpressEngine } from'zego-express-engine-webrtc'; import { BackgroundProcess } from'zego-express-engine-webrtc/background-process'; // Reference background processing module ZegoExpressEngine.use(BackgroundProcess);- Method 2: Manual integration after downloading the SDK package from the official website
<script src="./background-process-XXX.js"></script> <script src="./ZegoExpressWebRTC-XXX.js"></script> <script> ZegoExpressEngine.use(BackgroundProcess); ... </script> -
Dynamically load resource files such as wasm that background virtualization and virtual background depend on.
Publish all resource files in the
zego-express-engine-webrtc/background-process/assetsdirectory from the example source code to a CDN or static resource server, and they need to be published to the same resource directory, and generate a URL.
- Due to browser security policies, resource files need to be placed under an HTTPS service.
- If the Host URL of the resource file is different from the Host URL of the web application, you need to enable the CORS policy for the file domain.
-
Initialize the background processing module and pass in the URL of the resource file from step 2 above. The SDK will dynamically load the dependent files.
// Initialize instance, appID and server please obtain from the console const zg = new ZegoExpressEngine(appID, server); try { // The second parameter passes in the URL path where the wasm and other resource files from step 2 are located await zg.initBackgroundModule(0, "../assets"); } catch (err) { // Console prints initialization background processing module error console.error(err); } -
After creating the local data stream createZegoStream, set the use of background blur or virtual background, and set related parameters.
-
Use background blur effect
Call the setBackgroundBlurOptions interface to set the background blur effect.
const stream = await zg.createZegoStream(); zg.setBackgroundBlurOptions(stream, { blurDegree: 1 // Blur level 1, 2, 3, the higher the level, the higher the blur degree }); -
Use virtual background effect
Call the setVirtualBackgroundOptions interface to set the virtual background effect.
-
- Due to browser security policy influence, if the image resource is not under the same domain, the server needs to enable cross-origin access permissions, and set the
crossOriginattribute ofHTMLImageElementto 'anonymous'. - Image loading takes time. You need to call setVirtualBackgroundOptions after the image loading is completed, that is, call it in the
onloadevent.
const stream = await zg.createZegoStream();
// source is the HTMLImageElement of the background image
// objectFit is the virtual background fill method: 'contain', 'cover', 'fill'
zg.setVirtualBackgroundOptions(stream, {
source: img,
objectFit: 'fill'
});| Content Fit Method | Description |
|---|---|
| fill | Does not guarantee maintaining the original proportions, the image will be stretched to fill the entire container. |
| contain | Maintains original size proportions, the image will be scaled. |
| cover | Maintains original size proportions, fills the container as much as possible, but some content may be cropped. |
-
Call the enableBackgroundProcess interface and set
enabletotrueto enable the background blur and virtual background features.// enable passes true to enable virtual background, false to disable zg.enableBackgroundProcess(stream, true, 0); -
Call enableBackgroundProcess and set
enabletofalseto disable the background blur and virtual background features.// enable passes true to enable virtual background, false to disable zg.enableBackgroundProcess(stream, false, 0);




