Skip to content

🎥 Make videos programmatically with React

renderStill() should also be able to return a Buffer

Afzal Sayed85354a05cb7e
diff --git a/packages/docs/docs/renderer/render-still.md b/packages/docs/docs/renderer/render-still.md
index 368ad1b720..b9148c873e 100644
--- a/packages/docs/docs/renderer/render-still.md
+++ b/packages/docs/docs/renderer/render-still.md
@@ -189,9 +189,9 @@ Accepted values:
Default: `null`.
**Default for Lambda rendering**: `"swangle"`.
-## Return value
+## 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.
+If `output` is not specified or `null`, the return value is a Promise that resolves a `Buffer`. If an output location is specified, the return value is a Promise that resolves no value.
- [Source code for this function](https://github.com/remotion-dev/remotion/blob/main/packages/renderer/src/render-still.ts)
- [bundle()](/docs/bundle)
diff --git a/packages/renderer/src/render-still.ts b/packages/renderer/src/render-still.ts
index 87ed8e838d..294a9230cf 100644
--- a/packages/renderer/src/render-still.ts
+++ b/packages/renderer/src/render-still.ts
@@ -32,7 +32,7 @@ import {validateScale} from './validate-scale';
type InnerStillOptions = {
composition: SmallTCompMetadata;
- output: string;
+ output?: string | null;
frame?: number;
inputProps?: unknown;
imageFormat?: StillImageFormat;
@@ -55,6 +55,8 @@ type InnerStillOptions = {
downloadMap?: DownloadMap;
};
+type RenderStillReturnValue = { buffer: Buffer };
+
export type RenderStillOptions = InnerStillOptions &
ServeUrlOrWebpackBundle & {
port?: number | null;
@@ -83,7 +85,7 @@ const innerRenderStill = async ({
serveUrl: string;
onError: (err: Error) => void;
proxyPort: number;
-}): Promise<void> => {
+}): Promise<RenderStillReturnValue | null> => {
Internals.validateDimension(
composition.height,
'height',
@@ -112,11 +114,7 @@ const innerRenderStill = async ({
validatePuppeteerTimeout(timeoutInMilliseconds);
validateScale(scale);
- if (typeof output !== 'string') {
- throw new TypeError('`output` parameter was not passed or is not a string');
- }
-
- output = path.resolve(process.cwd(), output);
+ output = typeof output === 'string' ? path.resolve(process.cwd(), output) : null;
if (quality !== undefined && imageFormat !== 'jpeg') {
throw new Error(
@@ -126,24 +124,26 @@ const innerRenderStill = async ({
validateQuality(quality);
+ if (output) {
if (fs.existsSync(output)) {
if (!overwrite) {
throw new Error(
`Cannot render still - "overwrite" option was set to false, but the output destination ${output} already exists.`
- );
- }
-
- const stat = statSync(output);
+ );
+ }
+
+ const stat = statSync(output);
+
+ if (!stat.isFile()) {
+ throw new Error(
+ `The output location ${output} already exists, but is not a file, but something else (e.g. folder). Cannot save to it.`
+ );
+ }
+ }
- if (!stat.isFile()) {
- throw new Error(
- `The output location ${output} already exists, but is not a file, but something else (e.g. folder). Cannot save to it.`
- );
- }
+ ensureOutputDirectory(output);
}
- ensureOutputDirectory(output);
-
const browserInstance =
puppeteerInstance ??
(await openBrowser(DEFAULT_BROWSER, {
@@ -232,7 +232,7 @@ const innerRenderStill = async ({
});
await seekToFrame({frame: stillFrame, page});
- await provideScreenshot({
+ const buffer = await provideScreenshot({
page,
imageFormat,
quality,
@@ -245,6 +245,8 @@ const innerRenderStill = async ({
});
await cleanup();
+
+ return output ? { buffer } : null
};
/**
@@ -252,14 +254,14 @@ const innerRenderStill = async ({
* @description Render a still frame from a composition
* @link https://www.remotion.dev/docs/renderer/render-still
*/
-export const renderStill = (options: RenderStillOptions): Promise<void> => {
+export const renderStill = (options: RenderStillOptions): Promise<RenderStillReturnValue | null> => {
const selectedServeUrl = getServeUrlWithFallback(options);
const downloadMap = options.downloadMap ?? makeDownloadMap();
const onDownload = options.onDownload ?? (() => () => undefined);
- const happyPath = new Promise<void>((resolve, reject) => {
+ const happyPath = new Promise<RenderStillReturnValue | null>((resolve, reject) => {
const onError = (err: Error) => reject(err);
let close: (() => void) | null = null;
@@ -284,7 +286,7 @@ export const renderStill = (options: RenderStillOptions): Promise<void> => {
});
})
- .then((res) => resolve(res))
+ .then((buffer) => resolve(buffer))
.catch((err) => reject(err))
.finally(() => {
// Clean download map if it was not passed in
@@ -292,13 +294,13 @@ export const renderStill = (options: RenderStillOptions): Promise<void> => {
cleanDownloadMap(downloadMap);
}
- return close?.();
+ close?.();
});
});
return Promise.race([
happyPath,
- new Promise<void>((_resolve, reject) => {
+ new Promise<RenderStillReturnValue | null>((_resolve, reject) => {
options.cancelSignal?.(() => {
reject(new Error('renderStill() got cancelled'));
});