Skip to content

🎥 Make videos programmatically with React

Consolidate frame accuracy render tests

JonnyBurgerb686641b1c70
diff --git a/packages/example/src/Root.tsx b/packages/example/src/Root.tsx
index bec6c7f339..d5aae2e74d 100644
--- a/packages/example/src/Root.tsx
+++ b/packages/example/src/Root.tsx
@@ -188,7 +188,7 @@ import {VideoOnCanvas} from './VideoOnCanvas';
import {Greenscreen} from './VideoOnCanvas/greenscreen';
import {VideoParser} from './VideoParser';
import {VideoSpeed} from './VideoSpeed';
-import {VideoTesting} from './VideoTesting';
+import {VideoTesting, VideoTestingFrameAccuracy} from './VideoTesting';
import {VisualMode3D} from './VisualMode3D';
import {WarpDemoOuter} from './WarpText';
import {WarpDemo2} from './WarpText/demo2';
@@ -1211,6 +1211,14 @@ export const Index: React.FC = () => {
codec: 'mp4' as const,
}}
/>
+ <Composition
+ id="video-testing-frame-accuracy"
+ component={VideoTestingFrameAccuracy}
+ width={1080}
+ height={1080}
+ fps={30}
+ durationInFrames={100}
+ />
<Composition
id="video-testing-mp4-offthread"
component={VideoTesting}
diff --git a/packages/example/src/VideoTesting/index.tsx b/packages/example/src/VideoTesting/index.tsx
index cd3a81b70d..852ed963f7 100644
--- a/packages/example/src/VideoTesting/index.tsx
+++ b/packages/example/src/VideoTesting/index.tsx
@@ -36,3 +36,42 @@ export const VideoTesting: React.FC<{
</div>
);
};
+
+export const VideoTestingFrameAccuracy: React.FC = () => {
+ const cases = [
+ {codec: 'webm', type: 'normal', left: 0, top: 0},
+ {codec: 'webm', type: 'offthread', left: 540, top: 0},
+ {codec: 'mp4', type: 'normal', left: 0, top: 540},
+ {codec: 'mp4', type: 'offthread', left: 540, top: 540},
+ ] as const;
+
+ return (
+ <div>
+ {cases.map(({codec, type, left, top}) => (
+ <div
+ key={`${codec}-${type}`}
+ style={{
+ position: 'absolute',
+ left,
+ top,
+ width: 540,
+ height: 540,
+ overflow: 'hidden',
+ }}
+ >
+ <div
+ style={{
+ position: 'relative',
+ width: 1080,
+ height: 1080,
+ transform: 'scale(0.5)',
+ transformOrigin: 'top left',
+ }}
+ >
+ <VideoTesting codec={codec} type={type} />
+ </div>
+ </div>
+ ))}
+ </div>
+ );
+};
diff --git a/packages/it-tests/src/rendering/frame-accuracy.test.ts b/packages/it-tests/src/rendering/frame-accuracy.test.ts
index 3e360a550b..8ac9f0999b 100644
--- a/packages/it-tests/src/rendering/frame-accuracy.test.ts
+++ b/packages/it-tests/src/rendering/frame-accuracy.test.ts
@@ -1,41 +1,17 @@
import {expect, test} from 'bun:test';
import {
- getMissedFramesforCodec,
+ getMissedFramesForCodecs,
getMissedFramesWithFractionalTrimApplied,
} from './test-utils';
test(
- 'should render correct frames from embedded videos - WebM onthread',
+ 'should render correct frames from embedded videos',
async () => {
- const missedFrames = await getMissedFramesforCodec('webm', 'normal');
- expect(missedFrames).toBeLessThanOrEqual(8);
- },
- {retry: 3},
-);
-
-test(
- 'should render correct frames from embedded videos - WebM offthread',
- async () => {
- const missedFrames = await getMissedFramesforCodec('webm', 'offthread');
- expect(missedFrames).toBe(0);
- },
- {retry: 3},
-);
-
-test(
- 'should render correct frames from embedded videos - MP4 onthread',
- async () => {
- const missedFrames = await getMissedFramesforCodec('mp4', 'normal');
- expect(missedFrames).toBeLessThanOrEqual(8);
- },
- {retry: 3},
-);
-
-test(
- 'should render correct frames from embedded videos - MP4 offthread',
- async () => {
- const missedFrames = await getMissedFramesforCodec('mp4', 'offthread');
- expect(missedFrames).toBe(0);
+ const missedFrames = await getMissedFramesForCodecs();
+ expect(missedFrames.webm.normal).toBeLessThanOrEqual(8);
+ expect(missedFrames.webm.offthread).toBe(0);
+ expect(missedFrames.mp4.normal).toBeLessThanOrEqual(8);
+ expect(missedFrames.mp4.offthread).toBe(0);
},
{retry: 3},
);
diff --git a/packages/it-tests/src/rendering/test-utils.ts b/packages/it-tests/src/rendering/test-utils.ts
index d2b1a809c5..a66281489b 100644
--- a/packages/it-tests/src/rendering/test-utils.ts
+++ b/packages/it-tests/src/rendering/test-utils.ts
@@ -54,6 +54,59 @@ export const getMissedFramesforCodec = async (
return missedFrames;
};
+export const getMissedFramesForCodecs = async () => {
+ const outputPath = await saveSequenceInTempDir(
+ 'video-testing-frame-accuracy',
+ );
+ const cases = [
+ {codec: 'webm', type: 'normal', left: 0, top: 0},
+ {codec: 'webm', type: 'offthread', left: 540, top: 0},
+ {codec: 'mp4', type: 'normal', left: 0, top: 540},
+ {codec: 'mp4', type: 'offthread', left: 540, top: 540},
+ ] as const;
+ const missedFrames = {
+ webm: {normal: 0, offthread: 0},
+ mp4: {normal: 0, offthread: 0},
+ };
+
+ for (let frame = 0; frame < 100; 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),
+ green: selectColor('green', frame),
+ blue: selectColor('blue', frame),
+ };
+
+ // extract the actual RGB color value of the top left pixel in the frame image that was generated by remotion
+ const paddedIndex = String(frame).padStart(2, '0');
+ const filename = path.join(outputPath, `element-${paddedIndex}.png`);
+ const {data, info} = await sharp(filename)
+ .raw()
+ .toBuffer({resolveWithObject: true});
+
+ for (const {codec, type, left, top} of cases) {
+ const offset = (top * info.width + left) * info.channels;
+ const actualColor = {
+ red: data.readUInt8(offset),
+ green: data.readUInt8(offset + 1),
+ blue: data.readUInt8(offset + 2),
+ };
+
+ missedFrames[codec][type] = missedFrameChecker(
+ expectedColor,
+ actualColor,
+ missedFrames[codec][type],
+ frame,
+ `${filename} (${codec} ${type})`,
+ );
+ }
+ }
+ RenderInternals.deleteDirectory(outputPath);
+
+ return missedFrames;
+};
+
export const getMissedFramesWithPlaybackrate = async (
type: 'normal' | 'offthread' | 'codec',
) => {