@motiojs/renderer
The Node renderer of Motio: it opens a bundle in a pinned headless Chrome, captures it frame by frame and encodes with FFmpeg (renderMedia, renderStill, renderFrames, stitchFramesToVideo, getCompositions, selectComposition). This page covers warnings and errors, the media functions and the reusable browser; the architecture is in docs/ARCHITECTURE.md, and Rendering from Node walks through a render script.
Warnings and errors
Section titled “Warnings and errors”import { isMotioError, renderMedia, selectComposition } from "@motiojs/renderer";
const card = await selectComposition({ serveUrl, id: "card" });try { const { warnings } = await renderMedia({ serveUrl, composition: card, codec: "h264", outputLocation: "out/card.mp4", onDelayRenderPending: (p) => console.log(`${p.label} still open after ${p.elapsedMs} ms`), }); // [{ code: "W_FONT_FALLBACK", message: 'Font "Brand" is not available; ...', frame: 0 }] if (warnings.length > 0) console.warn(warnings);} catch (error) { if (!isMotioError(error)) throw error; // E_RENDER_ERROR at frame 12 { file: "/project/src/Card.tsx", line: 20, column: 9 } undefined console.error(error.code, "at frame", error.frame, error.details?.source, error.details?.hint);}renderMedia(),renderStill()andrenderFrames()returnwarnings: { code, message, frame? }[](a Motio extension), what the page reported while rendering, deduplicated by code and message and kept with the first frame each was seen on:W_FONT_FALLBACK,W_MATH_RANDOM,W_STATIC_FILE_CASE,W_TIMELINE_SHORT,W_SLOW_READINESS,W_ANIMATION_UNSEEKABLE, andW_FFMPEG_FALLBACKwhen@motiojs/ffmpegcould not download its build and the FFmpeg onPATHwas used.onDelayRenderPending(a Motio extension, on every function that opens a browser) is called every 5 seconds for each opendelayRender()handle with{ label, frame, compositionId, elapsedMs, timeoutMs };frameis null while compositions are listed or metadata resolves.- Errors are
MotioErrorwith a stablecode,frame,compositionanddetails.detailsrepeatsframeandcompositionIdwhen known and may carrysource({ file, line, column }, the first frame of the project’s code in a page error’s stack, mapped through the bundle’s source maps),rawStack(the stack before mapping),hintandoption(the option an invalid value was given to). - A file missing from
public/fails at once instead of at the timeout:E_ASSET_NOT_FOUNDfrom<Img>,E_FONT_LOADfrom@motiojs/fonts, both withdetails.pathand, when a file has a close name,details.suggestion.E_FONT_LOADalso covers undecodable font files and Google Fonts that cannot be downloaded.
Media functions
Section titled “Media functions”Three functions read local media files with the FFmpeg and ffprobe the renderer resolves (the ffmpegExecutable and ffprobeExecutable options, MOTIO_FFMPEG and MOTIO_FFPROBE, @motiojs/ffmpeg when installed, then PATH, which is also used when the package cannot download its build); binariesDirectory points at a folder holding both instead. Paths may be relative to the working directory; URLs are refused, so download remote files first.
getVideoMetadata(path, options?)
Section titled “getVideoMetadata(path, options?)”import { getVideoMetadata } from "@motiojs/renderer";
const meta = await getVideoMetadata("assets/interview.mp4");// {// fps: 29.97003, width: 1920, height: 1080, durationInSeconds: 61.2,// codec: "h264", canPlayInVideoTag: true, supportsSeeking: true,// colorSpace: "bt709", pixelFormat: "yuv420p",// audioCodec: "aac", audioFileExtension: "aac",// rotation: 0,// }const durationInFrames = Math.ceil(meta.durationInSeconds * 30);widthandheightare the stored size.rotation(a Motio addition) is the clockwise rotation a player applies to show the video upright, from the file’s display matrix: phone videos often store landscape pixels withrotation: 90, and their displayed size isheightbywidth.supportsSeekingpredicts whether the video seeks well in a browser: false for ProRes and unknown codecs, and for H.264 files of 5 seconds or more unless they are MP4 or MOV with themoovatom first. Fix such a file withffmpeg -i in.mp4 -c copy -movflags +faststart out.mp4.- An untagged video reports
colorSpace: "bt601".audioCodecis null without audio. - Audio-only files fail with
E_NO_VIDEO_STREAM, missing files withE_MEDIA_SOURCE.
extractAudio({ videoSource, audioOutput })
Section titled “extractAudio({ videoSource, audioOutput })”Copies the first audio stream into a file without re-encoding it. The extension of audioOutput chooses the container, so it must be one that holds the codec: getVideoMetadata().audioFileExtension names one (aac, mp3, opus, wav). .m4a and .mka also work for AAC. A wrong extension fails with E_FFMPEG_FAILED and a message naming the right one; a file without audio fails with E_NO_AUDIO_STREAM. The output’s folder is created, an existing file is replaced, and a failed extraction leaves no partial file.
import { extractAudio, getVideoMetadata } from "@motiojs/renderer";
const { audioFileExtension } = await getVideoMetadata("clip.mp4");if (audioFileExtension) { await extractAudio({ videoSource: "clip.mp4", audioOutput: `out/clip.${audioFileExtension}` });}getSilentParts({ src, noiseThresholdInDecibels?, minDurationInSeconds? })
Section titled “getSilentParts({ src, noiseThresholdInDecibels?, minDurationInSeconds? })”Finds the silent and audible parts of a video or audio file with FFmpeg’s silencedetect. Audio quieter than noiseThresholdInDecibels (default -20 dB relative to full scale, must be below 30) for at least minDurationInSeconds (default 1, must be above 0) is silent. Parts are in seconds, in order, and audibleParts covers the rest of the file.
import { getSilentParts } from "@motiojs/renderer";
const { silentParts, audibleParts, durationInSeconds } = await getSilentParts({ src: "podcast.wav", noiseThresholdInDecibels: -35, minDurationInSeconds: 0.5,});// Keep only what is spoken, for example as <Sequence>s of an <OffthreadVideo>:const cuts = audibleParts.map((p) => [p.startInSeconds, p.endInSeconds]);A silence that lasts to the end of the audio ends at durationInSeconds, the container’s duration.
Compared with Remotion
Section titled “Compared with Remotion”Recorded against @remotion/renderer@4.0.545 on the files in fixtures/node-api (pnpm --dir tools/oracle-render record:node-api): getVideoMetadata returns the same value for every field Remotion returns, on H.264 in MP4 and MKV, H.265 10-bit, VP9 with Opus, ProRes 4444 and a rotated video; extractAudio writes the same decoded samples for AAC, MP3, PCM and Opus into WebM; getSilentParts returns the same parts and durations on AAC, Opus and PCM tracks of videos. Measured differences:
- MP3 duration: Motio reports the container’s (2.000000 s for a 2 s tone), Remotion adds the encoder’s padding (2.037551 s).
- Opus at 0 dB: Remotion starts the silence 0.5 ms in, after a 0.5 ms audible part; Motio finds silence from the first sample.
- Opus into
.opus: FFmpeg’s Ogg muxer keeps the stream’s pre-skip, so the file decodes to 96024 samples where Remotion’s decodes to 96648 (the source: 96000). - Remotion 4.0.545 fails on PCM WAV files in
getSilentPartsand on.m4aoutput inextractAudio; Motio handles both.
Reusing a browser across renders
Section titled “Reusing a browser across renders”Every render normally launches Chrome and kills it at the end. A pipeline that renders many stills or short videos can open the browser once and pass it to every call as puppeteerInstance:
import { openBrowser, renderMedia, renderStill, selectComposition } from "@motiojs/renderer";
const browser = await openBrowser("chrome");try { for (const user of users) { const inputProps = { user }; const composition = await selectComposition({ serveUrl, id: "card", inputProps, puppeteerInstance: browser, }); await renderStill({ serveUrl, composition, inputProps, output: `out/${user.id}.png`, puppeteerInstance: browser, }); await renderMedia({ serveUrl, composition, inputProps, codec: "h264", outputLocation: `out/${user.id}.mp4`, puppeteerInstance: browser, }); }} finally { await browser.close();}- Each render opens its own tabs on the shared browser and closes them when it ends; the browser processes stay until
close(). The layout of a render does not change: its pages are spread over two browser processes by default (more above concurrency 8), andpagesPerBrowserstill applies. The instance starts one browser at once and the others the first time a render needs them. - Transparent captures (PNG with alpha, the
renderStilldefault) composite differently from opaque ones for identical pixels, so they run on a browser of their own, also kept. browserExecutableandchromiumOptionsare fixed when the browser starts: pass them toopenBrowser(). A render givenpuppeteerInstanceignores its own.- A browser that crashes is replaced on the next render. Several renders may share the instance at the same time.
close()kills every process and resolves when they have exited; calling it twice throwsE_BROWSER_CLOSEDunless you pass{ silent: true }. A render given a closed instance fails withE_BROWSER_CLOSED, and a render running while the instance closes fails.- The instance is not a Puppeteer
Browser, and a PuppeteerBrowseris not accepted (E_INVALID_OPTION).
Gain, measured on 10 calls back to back (512x288 composition, Apple M5, medians of 5 alternating rounds, openBrowser() included in the shared time):
| Calls | New browser each time | Shared browser | Ratio |
|---|---|---|---|
renderStill, PNG |
367 ms per call | 160 ms per call | 0.44 |
renderStill, JPEG |
189 ms per call | 115 ms per call | 0.61 |
renderMedia, 30 frames H.264, concurrency 4 |
350 ms per call | 252 ms per call | 0.72 |
selectComposition |
165 ms per call | 90 ms per call | 0.55 |
What remains per call is opening the tabs, loading the bundle and, for videos, capture and encoding.