🎥 Make videos programmatically with React
fix algorithm
Jonny Burgerdd5247d5722e
diff --git a/packages/it-tests/packages/example/out.mp4 b/packages/it-tests/packages/example/out.mp4deleted file mode 100644index ebd8dbcc6a..0000000000Binary files a/packages/it-tests/packages/example/out.mp4 and /dev/null differdiff --git a/packages/renderer/src/get-frame-padded-index.ts b/packages/renderer/src/get-frame-padded-index.tsindex 08067b039b..d3f2578b5d 100644--- a/packages/renderer/src/get-frame-padded-index.ts+++ b/packages/renderer/src/get-frame-padded-index.ts@@ -1,14 +1,66 @@-export const getFrameOutputFileName = ({+// Determines the filenames for frames that get rendered.+// - If passed to FFMPEG, they should be consecutive: element-000.jpg, element-001.jpg, element-002.jpg+// - If `--every-nth-frame` is passed, only frames 0, 2, 4 are rendered+// - If an image sequence is created, the filenames should correspond to the frame numbers: element-099.jpg, element-100.jpg++export type CountType = 'from-zero' | 'actual-frames';++const padIndex = ({+ num,filePadLength,+}: {+ num: number;+ filePadLength: number;+}) => {+ return String(num).padStart(filePadLength, '0');+};++export const getFrameOutputFileName = ({index,frame,imageFormat,+ countType,+ lastFrame,+ totalFrames,}: {index: number;frame: number;- filePadLength: number;imageFormat: 'png' | 'jpeg';+ countType: CountType;+ lastFrame: number;+ totalFrames: number;+}) => {+ const filePadLength = getFilePadLength({lastFrame, countType, totalFrames});++ if (countType === 'actual-frames') {+ const paddedIndex = padIndex({filePadLength, num: frame});+ return `element-${paddedIndex}.${imageFormat}`;+ }++ if (countType === 'from-zero') {+ const paddedIndex = padIndex({filePadLength, num: index});+ return `element-${paddedIndex}.${imageFormat}`;+ }++ throw new TypeError('Unknown count type');+};++export const getFilePadLength = ({+ lastFrame,+ totalFrames,+ countType,+}: {+ lastFrame: number;+ totalFrames: number;+ countType: CountType;}) => {- const paddedIndex = String(index).padStart(filePadLength, '0');- return `element-${paddedIndex}.${imageFormat}`;+ if (countType === 'actual-frames') {+ return String(lastFrame).length;+ }++ if (countType === 'from-zero') {+ return String(totalFrames - 1).length;+ }++ throw new Error('Unknown file type');};diff --git a/packages/renderer/src/render-frames.ts b/packages/renderer/src/render-frames.tsindex f3d23d3091..748389c023 100644--- a/packages/renderer/src/render-frames.ts+++ b/packages/renderer/src/render-frames.ts@@ -19,7 +19,11 @@ import {cycleBrowserTabs} from './cycle-browser-tabs';import {handleJavascriptException} from './error-handling/handle-javascript-exception';import {getActualConcurrency} from './get-concurrency';import {getFramesToRender} from './get-duration-from-frame-range';-import {getFrameOutputFileName} from './get-frame-padded-index';+import type {CountType} from './get-frame-padded-index';+import {+ getFilePadLength,+ getFrameOutputFileName,+} from './get-frame-padded-index';import {getRealFrameRange} from './get-frame-to-render';import {DEFAULT_IMAGE_FORMAT} from './image-format';import type {ServeUrlOrWebpackBundle} from './legacy-webpack-config';@@ -172,12 +176,7 @@ const innerRenderFrames = ({page.on('console', logCallback);}- const initialFrame =- typeof frameRange === 'number'- ? frameRange- : frameRange === null || frameRange === undefined- ? 0- : frameRange[0];+ const initialFrame = realFrameRange[0];await setPropsAndEnv({inputProps,@@ -206,9 +205,16 @@ const innerRenderFrames = ({return page;});- // Substract one because 100 frames will be 00-99- // --> 2 digits- const filePadLength = String(lastFrame).length;+ // If rendering a GIF and skipping frames, we must ensure it starts from 0+ // and then is consecutive so FFMPEG recognizes the sequence+ const countType: CountType =+ everyNthFrame === 1 ? 'actual-frames' : 'from-zero';++ const filePadLength = getFilePadLength({+ lastFrame,+ totalFrames: framesToRender.length,+ countType,+ });let framesRendered = 0;const poolPromise = getPool(pages);@@ -267,10 +273,12 @@ const innerRenderFrames = ({const output = path.join(outputDir,getFrameOutputFileName({- filePadLength,frame,imageFormat,index,+ countType,+ lastFrame,+ totalFrames: framesToRender.length,}));await provideScreenshot({