Client-Side
Rendering videos on the client side is made highly efficient by utilizing WebCodecs, a modern browser API that supports hardware-accelerated encoding. This allows for fast and optimized video processing directly in the browser.
Setup
To begin, you'll need a Composition
to render. Below is an example of setting up an empty Composition
for demonstration purposes:
Next, create an encoder using the Composition
:
Configuring the Encoder
The encoder can be customized via its second argument, which allows you to configure various output settings. The default configuration is as follows:
For example, to produce a 4K video at 60 FPS from a default 1080p Composition
, you can configure the encoder like this:
Rendering the Video
To start exporting the video, use the render
method, which accepts a render target as an argument. The recommended approach is to use the showSaveFilePicker
API:
This approach writes the MP4 chunks directly to disk as they are generated, allowing you to render videos that exceed your available RAM. However, note that browser support for showSaveFilePicker
is currently limited. In such cases, other export targets can be used:
To ensure compatibility across browsers, you can provide a fallback for showSaveFilePicker
:
This fallback ensures that the export process continues seamlessly even if showSaveFilePicker
is not available.
Aborting the export
This feature was implemented utilizing the AbortController
Web API. You can simply pass in the abort signal as the second argument of the render method: