🎥 Make videos programmatically with React
feat(render-still): handle negativ frame index
Antoine Caron6d81a3c6139b
diff --git a/packages/docs/docs/cli/still.md b/packages/docs/docs/cli/still.mdindex fd32dea628..cd3f4fd49c 100644--- a/packages/docs/docs/cli/still.md+++ b/packages/docs/docs/cli/still.md@@ -54,6 +54,12 @@ Specify a location for a dotenv file. Default `.env`.### `--frame`Which frame should be rendered. Example `--frame=10`. Default `0`.+For a composition of 100 frames, could between -100 and 99.+In this case:+- 0 would mean the first frame+- 99 the last frame+- -1 would mean the last frame+- -100 would mean the first frame### `--bundle-cache`diff --git a/packages/docs/docs/lambda/renderstillonlambda.md b/packages/docs/docs/lambda/renderstillonlambda.mdindex 6d0e68fc5d..e0c866513f 100644--- a/packages/docs/docs/lambda/renderstillonlambda.md+++ b/packages/docs/docs/lambda/renderstillonlambda.md@@ -71,6 +71,12 @@ One of:_optional - default `0`_Which frame of the composition should be rendered.+For a composition of 100 frames, could between -100 and 99.+In this case:+- 0 would mean the first frame+- 99 the last frame+- -1 would mean the last frame+- -100 would mean the first frame### `imageFormat?`diff --git a/packages/docs/docs/renderer/render-still.md b/packages/docs/docs/renderer/render-still.mdindex 253fa312ba..4afb46dd00 100644--- a/packages/docs/docs/renderer/render-still.md+++ b/packages/docs/docs/renderer/render-still.md@@ -78,6 +78,12 @@ _optional__optional - default: 0_Which frame should be rendered based on its number.+For a composition of 100 frames, could between -100 and 99.+In this case:+- 0 would mean the first frame+- 99 the last frame+- -1 would mean the last frame+- -100 would mean the first frame### `imageFormat?`diff --git a/packages/example/public/pts-framermp4withoutfileextension.mp4 b/packages/example/public/pts-framermp4withoutfileextension.mp4index b53293278b..806d120374 100644Binary files a/packages/example/public/pts-framermp4withoutfileextension.mp4 and b/packages/example/public/pts-framermp4withoutfileextension.mp4 differdiff --git a/packages/lambda/src/functions/still.ts b/packages/lambda/src/functions/still.tsindex fb3bbc8ec7..2f713b7c13 100644--- a/packages/lambda/src/functions/still.ts+++ b/packages/lambda/src/functions/still.ts@@ -137,7 +137,7 @@ const innerStillHandler = async (serveUrl: lambdaParams.serveUrl,dumpBrowserLogs: false,envVariables: lambdaParams.envVariables,- frame: lambdaParams.frame,+ frame: lambdaParams.frame < 0 ? composition.durationInFrames - lambdaParams.frame : lambdaParams.frame,imageFormat: lambdaParams.imageFormat as StillImageFormat,inputProps: lambdaParams.inputProps,overwrite: false,diff --git a/packages/renderer/src/render-still.ts b/packages/renderer/src/render-still.tsindex 55bbd4e477..26d53fbd6a 100644--- a/packages/renderer/src/render-still.ts+++ b/packages/renderer/src/render-still.ts@@ -103,6 +103,7 @@ const innerRenderStill = async ({);validateNonNullImageFormat(imageFormat);validateFrame(frame, composition.durationInFrames);+ const stillFrame = frame < 0 ? composition.durationInFrames - frame : frame;validatePuppeteerTimeout(timeoutInMilliseconds);validateScale(scale);@@ -185,7 +186,7 @@ const innerRenderStill = async ({envVariables,page,serveUrl,- initialFrame: frame,+ initialFrame: stillFrame,timeoutInMilliseconds,proxyPort,retriesRemaining: 2,@@ -224,14 +225,14 @@ const innerRenderStill = async ({frame: null,page,});- await seekToFrame({frame, page});+ await seekToFrame({frame: stillFrame, page});await provideScreenshot({page,imageFormat,quality,options: {- frame,+ frame: stillFrame,output,},});diff --git a/packages/renderer/src/test/frame.test.ts b/packages/renderer/src/test/frame.test.tsindex 60380d91e3..1508bbd894 100644--- a/packages/renderer/src/test/frame.test.ts+++ b/packages/renderer/src/test/frame.test.ts@@ -2,7 +2,6 @@ import {expect, test} from 'vitest';import {validateFrame} from '../validate-frame';test('Validate frame', () => {- expect(() => validateFrame(-1, 100)).toThrow(/Frame -1 cannot be negative/);expect(() => validateFrame(Infinity, 100)).toThrow(/Frame Infinity is not finite/);@@ -18,4 +17,9 @@ test('Validate frame', () => {);expect(() => validateFrame(0, 1)).not.toThrow();expect(() => validateFrame(1, 2)).not.toThrow();+ expect(() => validateFrame(-1, 100)).not.toThrow();+ expect(() => validateFrame(-100, 100)).not.toThrow();+ expect(() => validateFrame(-101, 100)).toThrow(+ /Cannot use frame -101: Duration of composition is 100, therefore the lowest frame that can be rendered is -100/+ );});diff --git a/packages/renderer/src/validate-frame.ts b/packages/renderer/src/validate-frame.tsindex cf1f794efb..59eab19217 100644--- a/packages/renderer/src/validate-frame.ts+++ b/packages/renderer/src/validate-frame.ts@@ -9,10 +9,6 @@ export const validateFrame = (frame: number, durationInFrames: number) => {);}- if (frame < 0) {- throw new RangeError(`Frame ${frame} cannot be negative`);- }-if (!Number.isFinite(frame)) {throw new RangeError(`Frame ${frame} is not finite`);}@@ -23,6 +19,14 @@ export const validateFrame = (frame: number, durationInFrames: number) => {);}+ if (frame < 0 && frame < -durationInFrames) {+ throw new RangeError(+ `Cannot use frame ${frame}: Duration of composition is ${durationInFrames}, therefore the lowest frame that can be rendered is ${+ -durationInFrames+ }`+ );+ }+if (frame > durationInFrames - 1) {throw new RangeError(`Cannot use frame ${frame}: Duration of composition is ${durationInFrames}, therefore the highest frame that can be rendered is ${