Skip to content

🎥 Make videos programmatically with React

start working on fix

Jonny Burgere336fd9c54d0
diff --git a/packages/it-tests/src/rendering/frame-accuracy-gif.test.ts b/packages/it-tests/src/rendering/frame-accuracy-gif.test.ts
new file mode 100644
index 0000000000..95a1f1ab49
--- /dev/null
+++ b/packages/it-tests/src/rendering/frame-accuracy-gif.test.ts
@@ -0,0 +1,95 @@
+import execa from "execa";
+import fs from "fs";
+import os from "os";
+import path from "path";
+import sharp from "sharp";
+import { random } from "remotion";
+import { expect, test } from "vitest";
+
+function selectColor(color: string, frame: number) {
+ return Math.floor((random(`${color}-${frame}`) * 255) % 255);
+}
+
+const getMissedFramesforCodec = async () => {
+ const outputDir = await fs.promises.mkdtemp(
+ path.join(os.tmpdir(), "remotion-")
+ );
+ const outputPath = path.join(outputDir, "vid.gif");
+
+ // render the VideoTesting example (which contains the Framer composition embedded with <Video>) to an image
+ // sequence which can be checked for accuracy
+ await execa(
+ "pnpm",
+ [
+ "exec",
+ "remotion",
+ "render",
+ "src/index.tsx",
+ `video-testing-mp4-offthread`,
+ "--codec=gif",
+ "--every-nth-frame=2",
+ outputPath,
+ ],
+ {
+ cwd: path.join(process.cwd(), "..", "example"),
+ }
+ );
+
+ console.log(outputPath);
+
+ await execa("ffmpeg", [
+ "-i",
+ outputPath,
+ "-f",
+ "image2",
+ path.join(outputDir, "out%2d.jpeg"),
+ "-y",
+ ]);
+
+ let missedFrames = 0;
+
+ for (let frame = 0; frame < 50; frame++) {
+ // each frame of the embedded video contains a (deterministically) random color which should appear correctly
+ // in the rendered output
+ const expectedColor = {
+ red: selectColor("red", frame * 2),
+ green: selectColor("green", frame * 2),
+ blue: selectColor("blue", frame * 2),
+ };
+
+ // extract the actual RGB color value of the top left pixel in the frame image that was generated by remotion
+ const paddedIndex = String(frame + 1).padStart(2, "0");
+ const filename = path.join(outputDir, `out${paddedIndex}.jpeg`);
+ const img = await sharp(filename).raw().toBuffer();
+
+ const actualColor = {
+ red: img.readUInt8(0),
+ green: img.readUInt8(1),
+ blue: img.readUInt8(2),
+ };
+
+ const colorDistance = {
+ red: Math.abs(expectedColor.red - actualColor.red),
+ green: Math.abs(expectedColor.green - actualColor.green),
+ blue: Math.abs(expectedColor.blue - actualColor.blue),
+ };
+
+ // encoding sometimes shifts the color slightly - so measure the distance between the expected and actual
+ // colors and consider any frame not within an acceptable range to be wrong
+ const highestDistance = Math.max(
+ colorDistance.red,
+ colorDistance.blue,
+ colorDistance.green
+ );
+ const threshold = 70;
+ if (highestDistance > threshold) {
+ missedFrames++;
+ }
+ }
+ return missedFrames;
+};
+
+test("should render correct frames from embedded videos - MP4 offthread", async () => {
+ const missedFrames = await getMissedFramesforCodec();
+ expect(missedFrames).toBe(0);
+});
diff --git a/packages/renderer/src/get-frame-padded-index.ts b/packages/renderer/src/get-frame-padded-index.ts
new file mode 100644
index 0000000000..08067b039b
--- /dev/null
+++ b/packages/renderer/src/get-frame-padded-index.ts
@@ -0,0 +1,14 @@
+export const getFrameOutputFileName = ({
+ filePadLength,
+ index,
+ frame,
+ imageFormat,
+}: {
+ index: number;
+ frame: number;
+ filePadLength: number;
+ imageFormat: 'png' | 'jpeg';
+}) => {
+ const paddedIndex = String(index).padStart(filePadLength, '0');
+ return `element-${paddedIndex}.${imageFormat}`;
+};
diff --git a/packages/renderer/src/render-frames.ts b/packages/renderer/src/render-frames.ts
index 39a243a286..f3d23d3091 100644
--- a/packages/renderer/src/render-frames.ts
+++ b/packages/renderer/src/render-frames.ts
@@ -19,6 +19,7 @@ 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 {getRealFrameRange} from './get-frame-to-render';
import {DEFAULT_IMAGE_FORMAT} from './image-format';
import type {ServeUrlOrWebpackBundle} from './legacy-webpack-config';
@@ -229,8 +230,6 @@ const innerRenderFrames = ({
throw new Error('Render was stopped');
}
- const paddedIndex = String(frame).padStart(filePadLength, '0');
-
const errorCallbackOnFrame = (err: Error) => {
onError(err);
};
@@ -267,7 +266,12 @@ const innerRenderFrames = ({
const output = path.join(
outputDir,
- `element-${paddedIndex}.${imageFormat}`
+ getFrameOutputFileName({
+ filePadLength,
+ frame,
+ imageFormat,
+ index,
+ })
);
await provideScreenshot({
page: freePage,