🎥 Make videos programmatically with React
Internal tests: Consolidate media frame accuracy renders
JonnyBurgere3c69fe088d4
diff --git a/packages/example/src/Root.tsx b/packages/example/src/Root.tsxindex e83b16b4bd..f583806a2e 100644--- a/packages/example/src/Root.tsx+++ b/packages/example/src/Root.tsx@@ -191,7 +191,11 @@ import {VideoOnCanvas} from './VideoOnCanvas';import {Greenscreen} from './VideoOnCanvas/greenscreen';import {VideoParser} from './VideoParser';import {VideoSpeed} from './VideoSpeed';-import {VideoTesting, VideoTestingFrameAccuracy} from './VideoTesting';+import {+ VideoTesting,+ VideoTestingFrameAccuracy,+ VideoTestingMediaFrameAccuracy,+} from './VideoTesting';import {VisualMode3D} from './VisualMode3D';import {WarpDemoOuter} from './WarpText';import {WarpDemo2} from './WarpText/demo2';@@ -1149,28 +1153,12 @@ export const Index: React.FC = () => {}}/><Composition- id="video-testing-webm-codec"- component={VideoTesting}- width={1080}- height={1080}- fps={30}- durationInFrames={100}- defaultProps={{- type: 'codec',- codec: 'webm' as const,- }}- />- <Composition- id="video-testing-mp4-codec"- component={VideoTesting}+ id="video-testing-media-frame-accuracy"+ component={VideoTestingMediaFrameAccuracy}width={1080}- height={1080}+ height={540}fps={30}durationInFrames={100}- defaultProps={{- type: 'codec',- codec: 'mp4' as const,- }}/></Folder><Folder name="video-tests">diff --git a/packages/example/src/VideoTesting/index.tsx b/packages/example/src/VideoTesting/index.tsxindex 852ed963f7..38b4b0fbde 100644--- a/packages/example/src/VideoTesting/index.tsx+++ b/packages/example/src/VideoTesting/index.tsx@@ -75,3 +75,35 @@ export const VideoTestingFrameAccuracy: React.FC = () => {</div>);};++export const VideoTestingMediaFrameAccuracy: React.FC = () => {+ return (+ <div>+ {(['webm', 'mp4'] as const).map((codec, index) => (+ <div+ key={codec}+ style={{+ position: 'absolute',+ left: index * 540,+ top: 0,+ 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="codec" />+ </div>+ </div>+ ))}+ </div>+ );+};diff --git a/packages/it-tests/src/rendering/new-video-tests.test.ts b/packages/it-tests/src/rendering/new-video-tests.test.tsindex 9f45769f89..5001ed9132 100644--- a/packages/it-tests/src/rendering/new-video-tests.test.ts+++ b/packages/it-tests/src/rendering/new-video-tests.test.ts@@ -4,27 +4,17 @@import {expect, test} from 'bun:test';import {- getMissedFramesforCodec,+ getMissedFramesForMediaCodecs,getMissedFramesWithPlaybackrate,getMissedFramesWithTrimApplied,} from './test-utils';test(- 'should render correct frames from embedded videos - WebM codec video',+ 'should render correct frames from embedded videos',async () => {- const missedFrames = await getMissedFramesforCodec('webm', 'codec');- expect(missedFrames).toBe(0);- },- {- timeout: 60000,- },-);--test(- 'should render correct frames from embedded videos - MP4 codec video',- async () => {- const missedFrames = await getMissedFramesforCodec('mp4', 'codec');- expect(missedFrames).toBeLessThanOrEqual(0);+ const missedFrames = await getMissedFramesForMediaCodecs();+ expect(missedFrames.webm).toBe(0);+ expect(missedFrames.mp4).toBe(0);},{timeout: 60000,diff --git a/packages/it-tests/src/rendering/test-utils.ts b/packages/it-tests/src/rendering/test-utils.tsindex a66281489b..9e1ea83865 100644--- a/packages/it-tests/src/rendering/test-utils.ts+++ b/packages/it-tests/src/rendering/test-utils.ts@@ -11,15 +11,20 @@ interface RgbColor {blue: number;green: number;}-export const getMissedFramesforCodec = async (- codec: 'mp4' | 'webm',- type: 'normal' | 'offthread' | 'codec',-) => {+export const getMissedFramesForCodecs = async () => {const outputPath = await saveSequenceInTempDir(- `video-testing-${codec}${type === 'normal' ? '' : '-' + type}`,+ 'video-testing-frame-accuracy',);-- let missedFrames = 0;+ 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@@ -33,72 +38,68 @@ export const getMissedFramesforCodec = async (// extract the actual RGB color value of the top left pixel in the frame image that was generated by remotionconst paddedIndex = String(frame).padStart(2, '0');const filename = path.join(outputPath, `element-${paddedIndex}.png`);- const img = await sharp(filename).raw().toBuffer();+ const {data, info} = await sharp(filename)+ .raw()+ .toBuffer({resolveWithObject: true});- const actualColor = {- red: img.readUInt8(0),- green: img.readUInt8(1),- blue: img.readUInt8(2),- };+ 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 = missedFrameChecker(- expectedColor,- actualColor,- missedFrames,- frame,- filename,- );+ missedFrames[codec][type] = missedFrameChecker(+ expectedColor,+ actualColor,+ missedFrames[codec][type],+ frame,+ `${filename} (${codec} ${type})`,+ );+ }}RenderInternals.deleteDirectory(outputPath);return missedFrames;};-export const getMissedFramesForCodecs = async () => {+export const getMissedFramesForMediaCodecs = async () => {const outputPath = await saveSequenceInTempDir(- 'video-testing-frame-accuracy',+ 'video-testing-media-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},+ {codec: 'webm', left: 0},+ {codec: 'mp4', left: 540},] as const;- const missedFrames = {- webm: {normal: 0, offthread: 0},- mp4: {normal: 0, offthread: 0},- };+ const missedFrames = {webm: 0, mp4: 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 outputconst 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 remotionconst 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;+ for (const {codec, left} of cases) {+ const offset = left * info.channels;const actualColor = {red: data.readUInt8(offset),green: data.readUInt8(offset + 1),blue: data.readUInt8(offset + 2),};- missedFrames[codec][type] = missedFrameChecker(+ missedFrames[codec] = missedFrameChecker(expectedColor,actualColor,- missedFrames[codec][type],+ missedFrames[codec],frame,- `${filename} (${codec} ${type})`,+ `${filename} (${codec})`,);}}