🎥 Make videos programmatically with React
document renderStill() API
Jonny Burgerc56dff3b3f54
diff --git a/packages/docs/docs/render-frames.md b/packages/docs/docs/render-frames.mdindex 2c4022e3ff..e626c2b7d7 100644--- a/packages/docs/docs/render-frames.md+++ b/packages/docs/docs/render-frames.md@@ -7,6 +7,8 @@ _Part of the `@remotion/renderer` package._Renders a series of images using Puppeteer and computes information for mixing audio.+If you want to render only a still image, use [renderStill()](/docs/render-still).+```tsconst renderFrames: (options: {config: VideoConfig;@@ -71,7 +73,7 @@ A `string` specifying the directory (absolute path) to which frames should be sa### `inputProps`-Custom props which will be passed to the component. Useful for rendering videos with dynamic content. Can be an object of any shape.+[Custom props which will be passed to the component.](/docs/parametrized-rendering) Useful for rendering videos with dynamic content. Can be an object of any shape.### `webpackBundle`@@ -159,3 +161,4 @@ A promise resolving to an object containing the following properties:- [Server-Side rendering](/docs/ssr)- [getCompositions()](/docs/get-compositions)- [stitchFramesToVideo()](/docs/stitch-frames-to-video)+- [renderStill()](/docs/render-still)diff --git a/packages/docs/docs/render-still.md b/packages/docs/docs/render-still.mdnew file mode 100644index 0000000000..74897cba69--- /dev/null+++ b/packages/docs/docs/render-still.md@@ -0,0 +1,134 @@+---+id: render-still+title: renderStill()+---++_Part of the `@remotion/renderer` package. Available from v2.3._++Renders a single frame to an image.++If you want to render the full image sequence, use [renderFrames()](/docs/render-frames) instead.++## Example usage++You first need to bundle the project and fetch the compositions. Read [the code snippet on the site for server-side rendering](/docs/ssr/#render-a-video-programmatically) for an example how to come up with the `bundled` and `composition` variables.++```ts twoslash+// @module: ESNext+// @target: ESNext+import {bundle} from '@remotion/bundler';+import {+ getCompositions,+ renderStill+} from '@remotion/renderer';++// The composition you want to render+const compositionId = 'HelloWorld'++const bundled = await bundle(require.resolve('./src/index'))++const comps = await getCompositions(bundled, {+ inputProps: {+ custom: 'data',+ },+})+const composition = comps.find((c) => c.id === compositionId)++if (!composition) {+ throw new Error(`No composition with the ID ${compositionId} found`)+}++// ---cut---++await renderStill({+ composition,+ webpackBundle: bundled,+ output: '/tmp/still.png',+ inputProps: {+ custom: 'data'+ }+})+```++## Arguments++Takes an object with the following properties:++### `composition`++A video composition object, consisting of `id`, `height`, `width`, `durationInFrames` and `fps`. Use [`getCompositions()`](/docs/get-compositions) to get a list of available video configs.++### `webpackBundle`++The absolute location of your webpack bundle. Use [`bundle()`](/docs/bundle) to bundle your project programmatically.++### `output`++An absolute path to where the frame should be rendered to.++### `inputProps?`++[Custom props which will be passed to the component.](/docs/parametrized-rendering) Useful for rendering videos with dynamic content. Can be an object of any shape.++### `frame?`++_optional - default: 0_++Which frame should be rendered based on it's number.++### `imageFormat?`++_optional - default: "png"_++Which output format the image should have, either `png` or `jpeg`.++### `quality?`++_optional - default: 80_++Sets the JPEG quality - must be an integer between 0 and 100 and can only be passed if `imageFormat` is set to `jpeg`.++### `puppeteerInstance?`++_optional - default `null`_++An already open Puppeteer [`Browser`](https://pptr.dev/#?product=Puppeteer&version=main&show=api-class-browser) instance. Reusing a browser across multiple function calls can speed up the rendering process. You are responsible for opening and closing the browser yourself. If you don't specify this option, a new browser will be opened and closed at the end.++### `envVariables?`++_optional - default `{}`_++An object containing key-value pairs of environment variables which will be injected into your Remotion projected and which can be accessed by reading the global `process.env` object.++### `dumpBrowserLogs?`++_optional - default `false`_++A boolean value deciding whether Puppeteer logs should be printed to the console.++### `onError?`++_optional_++Allows you to react to an exception thrown in your React code. The callback has an argument which is the error.++```tsx twoslash+import {renderStill as rs} from '@remotion/renderer'+const renderStill = (options: Partial<Parameters<typeof rs>[0]>) => {}+// ---cut---+renderStill({+ // ... other arguments+ onError: (err: Error) => {+ // Handle error here+ }+})+```++## Return value++A promise with no value. If the render succeeded, the still has been saved to `output`. If the render failed, the promise rejects.++- [bundle()](/docs/bundle)+- [Server-Side rendering](/docs/ssr)+- [getCompositions()](/docs/get-compositions)+- [renderFrames()](/docs/render-frames)diff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.jsindex c9fe1c7903..fb0483a035 100644--- a/packages/docs/sidebars.js+++ b/packages/docs/sidebars.js@@ -93,7 +93,12 @@ module.exports = {{type: "category",label: "API - @remotion/renderer",- items: ["get-compositions", "render-frames", "stitch-frames-to-video"],+ items: [+ "get-compositions",+ "render-frames",+ "stitch-frames-to-video",+ "render-still",+ ],},{type: "category",diff --git a/packages/renderer/src/index.ts b/packages/renderer/src/index.tsindex e5e9dce03b..aecf06f115 100644--- a/packages/renderer/src/index.ts+++ b/packages/renderer/src/index.ts@@ -20,13 +20,10 @@ declare global {export {FfmpegVersion} from './ffmpeg-flags';export {getCompositions} from './get-compositions';-export {- OnErrorInfo,- OnStartData,- renderFrames,- RenderFramesOutput,-} from './render';+export {renderFrames} from './render';+export {renderStill} from './render-still';export {stitchFramesToVideo} from './stitcher';+export {OnErrorInfo, OnStartData, RenderFramesOutput} from './types';export const RenderInternals = {ensureLocalBrowser,ffmpegHasFeature,diff --git a/packages/renderer/src/render-still.ts b/packages/renderer/src/render-still.tsindex 3b066e48b5..173c245d4b 100644--- a/packages/renderer/src/render-still.ts+++ b/packages/renderer/src/render-still.ts@@ -1,19 +1,17 @@import {Browser as PuppeteerBrowser} from 'puppeteer-core';-import {Browser, Internals, VideoConfig} from 'remotion';+import {Browser, Internals, TCompMetadata} from 'remotion';import {openBrowser} from './open-browser';import {provideScreenshot} from './provide-screenshot';import {seekToFrame} from './seek-to-frame';import {serveStatic} from './serve-static';import {setPropsAndEnv} from './set-props-and-env';-import {OnErrorInfo} from './types';import {validateFrame} from './validate-frame';/*** @description Render a still frame from a composition and returns an image path*/export const renderStill = async ({- config,- compositionId,+ composition,quality,imageFormat = 'png',webpackBundle,@@ -26,40 +24,39 @@ export const renderStill = async ({output,frame = 0,}: {- config: VideoConfig;- compositionId: string;+ composition: TCompMetadata;output: string;webpackBundle: string;frame?: number;- quality?: number;+ inputProps?: unknown;imageFormat?: 'png' | 'jpeg';+ quality?: number;browser?: Browser;puppeteerInstance?: PuppeteerBrowser;dumpBrowserLogs?: boolean;- onError?: (info: OnErrorInfo) => void;- inputProps?: unknown;+ onError?: (err: Error) => void;envVariables?: Record<string, string>;}) => {Internals.validateDimension(- config.height,+ composition.height,'height','in the `config` object passed to `renderStill()`');Internals.validateDimension(- config.width,+ composition.width,'width','in the `config` object passed to `renderStill()`');Internals.validateFps(- config.fps,+ composition.fps,'in the `config` object of `renderStill()`');Internals.validateDurationInFrames(- config.durationInFrames,+ composition.durationInFrames,'in the `config` object passed to `renderStill()`');Internals.validateImageFormat(imageFormat);- validateFrame(frame, config.durationInFrames);+ validateFrame(frame, composition.durationInFrames);if (quality !== undefined && imageFormat !== 'jpeg') {throw new Error(@@ -78,18 +75,18 @@ export const renderStill = async ({]);const page = await browserInstance.newPage();page.setViewport({- width: config.width,- height: config.height,+ width: composition.width,+ height: composition.height,deviceScaleFactor: 1,});const errorCallback = (err: Error) => {- onError?.({error: err, frame: null});+ onError?.(err);};page.on('pageerror', errorCallback);await setPropsAndEnv({inputProps, envVariables, page, port});- const site = `http://localhost:${port}/index.html?composition=${compositionId}`;+ const site = `http://localhost:${port}/index.html?composition=${composition.id}`;await page.goto(site);try {await seekToFrame({frame, page});diff --git a/packages/renderer/src/test/render-still.test.ts b/packages/renderer/src/test/render-still.test.tsindex b4725a4fd7..834064a63d 100644--- a/packages/renderer/src/test/render-still.test.ts+++ b/packages/renderer/src/test/render-still.test.ts@@ -3,12 +3,12 @@ import {renderStill} from '../render-still';test('Need to pass valid metadata', async () => {return expect(() =>renderStill({- compositionId: 'hithere',- config: {+ composition: {width: NaN,height: 1000,fps: 30,durationInFrames: 30,+ id: 'hithere',},frame: 0,output: '/file/output.png',@@ -20,12 +20,12 @@ test('Need to pass valid metadata', async () => {test('Need to pass valid metadata', async () => {return expect(() =>renderStill({- compositionId: 'hithere',- config: {+ composition: {width: 1000,height: 1000,fps: 30,durationInFrames: 30,+ id: 'hithere',},frame: 200,output: '/file/output.png',@@ -39,12 +39,12 @@ test('Need to pass valid metadata', async () => {test('Catches invalid image format', async () => {return expect(() =>renderStill({- compositionId: 'hithere',- config: {+ composition: {width: 1000,height: 1000,fps: 30,durationInFrames: 30,+ id: 'hithere',},// @ts-expect-errorimageFormat: 'jjj',