🎥 Make videos programmatically with React
`@remotion/renderer`: Add object signature to getCompositions() (#9502)
Jonny Burger09222f8d534d
diff --git a/packages/docs/docs/renderer/get-compositions.mdx b/packages/docs/docs/renderer/get-compositions.mdxindex c0f321fc98..193a001d5e 100644--- a/packages/docs/docs/renderer/get-compositions.mdx+++ b/packages/docs/docs/renderer/get-compositions.mdx@@ -11,30 +11,40 @@ Gets a list of compositions defined in a Remotion project based on a [Remotion BFor every compositions their [`calculateMetadata()`](/docs/composition#calculatemetadata) function is evaluated. If you just want to evaluate one composition that you want to render, use [`selectComposition()`](/docs/renderer/select-composition).-```ts-const getCompositions: (serveUrlOrWebpackUrl: string, options?: GetCompositionsConfig) => Promise<TComposition[]>;+The new signature, from [v4.0.497](https://github.com/remotion-dev/remotion/releases/v4.0.497) is:++```ts twoslash title="Example"+import {bundle} from '@remotion/bundler';+import {getCompositions} from '@remotion/renderer';++const bundled = await bundle({entryPoint: require.resolve('./src/index.ts')});++const compositions = await getCompositions({+ serveUrl: bundled,+ inputProps: {},+});```-## Arguments+Use [the old signature](#legacy-function-signature) for versions below 4.0.496. It remains supported.-### `serveUrlOrWebpackUrl`+## Arguments-A string pointing to a [Remotion Bundle](/docs/terminology/bundle) generated by [`bundle()`](/docs/bundle) or a URL that hosts the the bundled Remotion project.+An object with the following properties:-### `options?`+### `serveUrl`-An object containing one or more of the following options:+A string pointing to a [Remotion Bundle](/docs/terminology/bundle) generated by [`bundle()`](/docs/bundle) or a URL that hosts the the bundled Remotion project.-#### `inputProps`+### `inputProps`Optional in v4.x, required from v5.0.-[Input Props to pass to the selected composition of your video.](/docs/passing-props#passing-input-props-in-the-cli).+[Input Props to pass to the selected composition of your video.](/docs/passing-props#passing-input-props-in-the-cli)Must be a JSON object.From the root component the props can be read using [`getInputProps()`](/docs/get-input-props).You may transform input props using [`calculateMetadata()`](/docs/calculate-metadata).-#### `puppeteerInstance?`<AvailableFrom v="3.0.0" />+### `puppeteerInstance?`<AvailableFrom v="3.0.0" />An already open [`Browser`](/docs/renderer/open-browser) instance. Use [`openBrowser()`](/docs/renderer/open-browser) to create a new 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.@@ -42,11 +52,11 @@ An already open [`Browser`](/docs/renderer/open-browser) instance. Use [`openBroDespite the name, not actually compatible with `puppeteer`, only with [`openBrowser()`](/docs/renderer/open-browser).:::-#### `browserExecutable?`<AvailableFrom v="2.3.1" />+### `browserExecutable?`<AvailableFrom v="2.3.1" />A string defining the absolute path on disk of the browser executable that should be used. By default Remotion will try to detect it automatically and download one if none is available. If `puppeteerInstance` is defined, it will take precedence over `browserExecutable`.-#### `onBrowserLog?`<AvailableFrom v="3.0.0" />+### `onBrowserLog?`<AvailableFrom v="3.0.0" />Gets called when your project calls `console.log` or another method from console. A browser log has three properties:@@ -96,19 +106,19 @@ getCompositions({});```-#### `timeoutInMilliseconds?`<AvailableFrom v="2.6.3" />+### `timeoutInMilliseconds?`<AvailableFrom v="2.6.3" />A number describing how long one frame may take to resolve all [`delayRender()`](/docs/delay-render) calls before the [render times out and fails(/docs/timeout). Default: `30000`-#### `port?`+### `port?`Prefer a specific port that will be used to serve the Remotion project. If not specified, a random port will be used.-#### `logLevel?`<AvailableFrom v="4.0.0"/>+### `logLevel?`<AvailableFrom v="4.0.0"/><Options id="log" />-#### `chromiumOptions?`<AvailableFrom v="2.6.5" />+### `chromiumOptions?`<AvailableFrom v="2.6.5" />Allows you to set certain Chromium / Google Chrome flags. See: [Chromium flags](/docs/chromium-flags).@@ -148,6 +158,18 @@ _removed in v4.0_An absolute path overriding the `ffprobe` executable to use.+## Legacy function signature++The positional function signature is also supported for backwards compatibility:++```ts twoslash title="Legacy signature"+import {getCompositions} from '@remotion/renderer';++const compositions = await getCompositions('https://example.com', {+ inputProps: {},+});+```+## Return valueReturns a promise that resolves to an array of available compositions. Example value:diff --git a/packages/it-tests/src/rendering/get-compositions.test.ts b/packages/it-tests/src/rendering/get-compositions.test.tsindex 54d6caaac5..5abb446904 100644--- a/packages/it-tests/src/rendering/get-compositions.test.ts+++ b/packages/it-tests/src/rendering/get-compositions.test.ts@@ -6,4 +6,9 @@ test('getCompositions() should give a good error message if there is no compositexpect(() => getCompositions()).toThrow('No serve URL or webpack bundle directory was passed to getCompositions().',);++ // @ts-expect-error+ expect(() => getCompositions({})).toThrow(+ 'No serve URL or webpack bundle directory was passed to getCompositions().',+ );});diff --git a/packages/it-tests/src/ssr/render-still.test.ts b/packages/it-tests/src/ssr/render-still.test.tsindex 3e1ce30d78..161596ab33 100644--- a/packages/it-tests/src/ssr/render-still.test.ts+++ b/packages/it-tests/src/ssr/render-still.test.ts@@ -17,7 +17,8 @@ beforeAll(async () => {test('Render video with browser instance open', async () => {const puppeteerInstance = await openBrowser('chrome');- const compositions = await getCompositions(exampleBuild, {+ const compositions = await getCompositions({+ serveUrl: exampleBuild,puppeteerInstance,inputProps: {},});diff --git a/packages/renderer/src/get-compositions.ts b/packages/renderer/src/get-compositions.tsindex 51daf3e437..8cfa3dbab2 100644--- a/packages/renderer/src/get-compositions.ts+++ b/packages/renderer/src/get-compositions.ts@@ -38,7 +38,7 @@ type InternalGetCompositionsOptions = {onLog: OnLog;} & ToOptions<typeof optionsMap.getCompositions>;-export type GetCompositionsOptions = RequiredInputPropsInV5 & {+export type LegacyGetCompositionsOptions = RequiredInputPropsInV5 & {envVariables?: Record<string, string>;puppeteerInstance?: HeadlessBrowser;onBrowserLog?: (log: BrowserLog) => void;@@ -47,6 +47,34 @@ export type GetCompositionsOptions = RequiredInputPropsInV5 & {port?: number | null;} & Partial<ToOptions<typeof optionsMap.getCompositions>>;+export type GetCompositionsOptions = LegacyGetCompositionsOptions & {+ serveUrl: string;+};++type GetCompositionsArguments =+ | [options: GetCompositionsOptions]+ | [serveUrlOrWebpackUrl: string, config?: LegacyGetCompositionsOptions];++const convertGetCompositionsArgumentsToOptions = (+ args: GetCompositionsArguments,+): GetCompositionsOptions => {+ if ((args.length as number) === 0) {+ throw new Error(+ 'No serve URL or webpack bundle directory was passed to getCompositions().',+ );+ }++ const firstArg = args[0];+ if (typeof firstArg === 'string') {+ return {+ ...(args[1] ?? {}),+ serveUrl: firstArg,+ };+ }++ return firstArg;+};+type InnerGetCompositionsParams = {serializedInputPropsWithCustomSchema: string;envVariables: Record<string, string>;@@ -281,10 +309,10 @@ export const internalGetCompositions = wrapWithErrorHandling(* @see [Documentation](https://www.remotion.dev/docs/renderer/get-compositions)*/export const getCompositions = (- serveUrlOrWebpackUrl: string,- config?: GetCompositionsOptions,+ ...args: GetCompositionsArguments): Promise<VideoConfig[]> => {- if (!serveUrlOrWebpackUrl) {+ const options = convertGetCompositionsArgumentsToOptions(args);+ if (typeof options?.serveUrl !== 'string' || !options.serveUrl) {throw new Error('No serve URL or webpack bundle directory was passed to getCompositions().',);@@ -306,7 +334,8 @@ export const getCompositions = (chromeMode,offthreadVideoThreads,mediaCacheSizeInBytes,- } = config ?? {};+ serveUrl,+ } = options;const indent = false;const logLevel = passedLogLevel ?? 'info';@@ -325,7 +354,7 @@ export const getCompositions = (onBrowserLog: onBrowserLog ?? null,port: port ?? null,puppeteerInstance: puppeteerInstance ?? undefined,- serveUrlOrWebpackUrl,+ serveUrlOrWebpackUrl: serveUrl,server: undefined,timeoutInMilliseconds: timeoutInMilliseconds ?? DEFAULT_TIMEOUT,logLevel,diff --git a/packages/renderer/src/index.ts b/packages/renderer/src/index.tsindex 5723254b54..317d6807a8 100644--- a/packages/renderer/src/index.ts+++ b/packages/renderer/src/index.ts@@ -90,7 +90,11 @@ export {extractAudio} from './extract-audio';export type {FfmpegOverrideFn} from './ffmpeg-override';export {FileExtension} from './file-extensions';export {FrameRange} from './frame-range';-export {GetCompositionsOptions, getCompositions} from './get-compositions';+export {+ GetCompositionsOptions,+ getCompositions,+ LegacyGetCompositionsOptions,+} from './get-compositions';export {SilentPart, getSilentParts} from './get-silent-parts';export {VideoMetadata, getVideoMetadata} from './get-video-metadata';export {