Skip to content

🎥 Make videos programmatically with React

tests

Hunain Ahmedc9e34f96f8fd
diff --git a/packages/example/src/Root.tsx b/packages/example/src/Root.tsx
index e579b60f7b..2fd28a35fa 100644
--- a/packages/example/src/Root.tsx
+++ b/packages/example/src/Root.tsx
@@ -135,6 +135,8 @@ import {Seek} from './StudioApis/Seek';
import {TikTokTextBoxPlayground} from './TikTokTextbox/TikTokTextBox';
import {FitTextOnNLines, fitTextOnNLinesSchema} from './Title/FitTextOnNLines';
import {TransitionRounding} from './TransitionRounding';
+import {VideoTestingPlayback} from './VideoTesting/playback';
+import {VideoTestingTrim} from './VideoTesting/trim';
import {VisualControls} from './VisualControls';
import {VoiceVisualization} from './voice-visualization';
import {WhisperWeb} from './WhisperWeb';
@@ -658,70 +660,84 @@ export const Index: React.FC = () => {
}}
/>
</Folder>
- <Folder name="video-tests">
+ <Folder name="new-video-tests">
<Composition
- id="hls"
- component={HlsDemo}
- width={1920}
+ id="longVideo"
+ component={NewVideoExample}
+ fps={30}
+ height={720}
+ width={1280}
+ durationInFrames={30 * 25}
+ />
+ <Composition
+ id="video-testing-playback-codec"
+ component={VideoTestingPlayback}
+ fps={30}
height={1080}
- durationInFrames={100}
+ width={1080}
+ durationInFrames={25}
+ defaultProps={{
+ type: 'codec',
+ }}
+ />
+ <Composition
+ id="video-testing-trim-codec"
+ component={VideoTestingTrim}
fps={30}
+ height={1080}
+ width={1080}
+ durationInFrames={60}
+ defaultProps={{
+ type: 'codec',
+ }}
/>
<Composition
- id="video-testing-mp4"
+ id="video-testing-webm-codec"
component={VideoTesting}
width={1080}
height={1080}
fps={30}
durationInFrames={100}
defaultProps={{
- codec: 'mp4' as const,
+ type: 'codec',
+ codec: 'webm' as const,
}}
/>
<Composition
- id="video-testing-mp4-offthread"
+ id="video-testing-mp4-codec"
component={VideoTesting}
width={1080}
height={1080}
fps={30}
durationInFrames={100}
defaultProps={{
- type: 'offthread',
+ type: 'codec',
codec: 'mp4' as const,
}}
/>
- <OffthreadRemoteVideo />
- <OffthreadRemoteSeries />
- <LoopedOffthreadRemoteVideo />
+ </Folder>
+ <Folder name="video-tests">
<Composition
- id="OffthreadVideoToCanvas"
- component={OffthreadVideoToCanvas}
- fps={30}
+ id="hls"
+ component={HlsDemo}
+ width={1920}
height={1080}
- width={1080}
durationInFrames={100}
- />
- <Composition
- id="longVideo"
- component={NewVideoExample}
fps={30}
- height={720}
- width={1280}
- durationInFrames={30 * 25}
/>
<Composition
- id="video-testing-webm"
+ id="video-testing-mp4"
component={VideoTesting}
width={1080}
height={1080}
fps={30}
durationInFrames={100}
defaultProps={{
- codec: 'webm' as const,
+ codec: 'mp4' as const,
}}
/>
<Composition
- id="video-testing-webm-offthread"
+ id="video-testing-mp4-offthread"
component={VideoTesting}
width={1080}
height={1080}
@@ -729,31 +745,41 @@ export const Index: React.FC = () => {
durationInFrames={100}
defaultProps={{
type: 'offthread',
- codec: 'webm' as const,
+ codec: 'mp4' as const,
}}
/>
+ <OffthreadRemoteVideo />
+ <OffthreadRemoteSeries />
+ <LoopedOffthreadRemoteVideo />
<Composition
- id="video-testing-webm-codec"
+ id="OffthreadVideoToCanvas"
+ component={OffthreadVideoToCanvas}
+ fps={30}
+ height={1080}
+ width={1080}
+ durationInFrames={100}
+ />
+ <Composition
+ id="video-testing-webm"
component={VideoTesting}
width={1080}
height={1080}
fps={30}
durationInFrames={100}
defaultProps={{
- type: 'codec',
codec: 'webm' as const,
}}
/>
<Composition
- id="video-testing-mp4-codec"
+ id="video-testing-webm-offthread"
component={VideoTesting}
width={1080}
height={1080}
fps={30}
durationInFrames={100}
defaultProps={{
- type: 'codec',
- codec: 'mp4' as const,
+ type: 'offthread',
+ codec: 'webm' as const,
}}
/>
<Composition
diff --git a/packages/example/src/VideoTesting/playback.tsx b/packages/example/src/VideoTesting/playback.tsx
new file mode 100644
index 0000000000..98fc1f8d8c
--- /dev/null
+++ b/packages/example/src/VideoTesting/playback.tsx
@@ -0,0 +1,32 @@
+import {experimental_NewVideo as NewVideo} from '@remotion/video';
+import {
+ OffthreadVideo,
+ Sequence,
+ staticFile,
+ useVideoConfig,
+ Video,
+} from 'remotion';
+
+export const VideoTestingPlayback: React.FC<{
+ type?: 'normal' | 'offthread' | 'codec';
+}> = ({type = 'normal'}) => {
+ const {durationInFrames} = useVideoConfig();
+ const videoMp4 = staticFile('framermp4withoutfileextension');
+
+ let Comp;
+ if (type === 'codec') {
+ Comp = NewVideo;
+ } else if (type === 'offthread') {
+ Comp = OffthreadVideo;
+ } else {
+ Comp = Video;
+ }
+
+ return (
+ <div>
+ <Sequence durationInFrames={durationInFrames}>
+ <Comp playbackRate={4} src={videoMp4} />
+ </Sequence>
+ </div>
+ );
+};
diff --git a/packages/example/src/VideoTesting/trim.tsx b/packages/example/src/VideoTesting/trim.tsx
new file mode 100644
index 0000000000..57cf268cc2
--- /dev/null
+++ b/packages/example/src/VideoTesting/trim.tsx
@@ -0,0 +1,32 @@
+import {experimental_NewVideo as NewVideo} from '@remotion/video';
+import {
+ OffthreadVideo,
+ Sequence,
+ staticFile,
+ useVideoConfig,
+ Video,
+} from 'remotion';
+
+export const VideoTestingTrim: React.FC<{
+ type?: 'normal' | 'offthread' | 'codec';
+}> = ({type = 'normal'}) => {
+ const {durationInFrames} = useVideoConfig();
+ const videoMp4 = staticFile('framermp4withoutfileextension');
+
+ let Comp;
+ if (type === 'codec') {
+ Comp = NewVideo;
+ } else if (type === 'offthread') {
+ Comp = OffthreadVideo;
+ } else {
+ Comp = Video;
+ }
+
+ return (
+ <div>
+ <Sequence durationInFrames={durationInFrames}>
+ <Comp trimBefore={20} trimAfter={80} src={videoMp4} />
+ </Sequence>
+ </div>
+ );
+};
diff --git a/packages/it-tests/src/rendering/frame-accuracy.test.ts b/packages/it-tests/src/rendering/frame-accuracy.test.ts
index 1cedcbd2ac..680c9429a4 100644
--- a/packages/it-tests/src/rendering/frame-accuracy.test.ts
+++ b/packages/it-tests/src/rendering/frame-accuracy.test.ts
@@ -1,89 +1,5 @@
-import {RenderInternals} from '@remotion/renderer';
import {expect, test} from 'bun:test';
-import execa from 'execa';
-import fs from 'fs';
-import os from 'os';
-import path from 'path';
-import {random} from 'remotion';
-import sharp from 'sharp';
-
-function selectColor(color: string, frame: number) {
- return Math.floor((random(`${color}-${frame}`) * 255) % 255);
-}
-
-const getMissedFramesforCodec = async (
- codec: 'mp4' | 'webm',
- type: 'normal' | 'offthread' | 'codec',
-) => {
- const outputPath = await fs.promises.mkdtemp(
- path.join(os.tmpdir(), 'remotion-'),
- );
-
- // 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',
- // 'build',
- `video-testing-${codec}${type === 'normal' ? '' : '-' + type}`,
- outputPath,
- '--image-format',
- 'png',
- '--sequence',
- ],
- {
- cwd: path.join(process.cwd(), '..', 'example'),
- },
- );
-
- let missedFrames = 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 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 = 35;
- if (highestDistance > threshold) {
- console.log(colorDistance, {threshold, frame, filename});
- missedFrames++;
- }
- }
- RenderInternals.deleteDirectory(outputPath);
-
- return missedFrames;
-};
+import {getMissedFramesforCodec} from './test-utils';
test(
'should render correct frames from embedded videos - WebM onthread',
@@ -128,25 +44,3 @@ test(
timeout: 40000,
},
);
-
-test(
- 'should render correct frames from embedded videos - WebM codec video',
- async () => {
- const missedFrames = await getMissedFramesforCodec('webm', 'codec');
- expect(missedFrames).toBe(0);
- },
- {
- timeout: 30000,
- },
-);
-
-test(
- 'should render correct frames from embedded videos - MP4 codec video',
- async () => {
- const missedFrames = await getMissedFramesforCodec('mp4', 'codec');
- expect(missedFrames).toBeLessThanOrEqual(0);
- },
- {
- timeout: 40000,
- },
-);
diff --git a/packages/it-tests/src/rendering/new-video-tests.test.ts b/packages/it-tests/src/rendering/new-video-tests.test.ts
new file mode 100644
index 0000000000..965f574d59
--- /dev/null
+++ b/packages/it-tests/src/rendering/new-video-tests.test.ts
@@ -0,0 +1,58 @@
+//1. frame accuracy
+//2. playback rate
+//3. trims
+
+import {expect, test} from 'bun:test';
+import {
+ getMissedFramesAfterTrim,
+ getMissedFramesforCodec,
+ getMissedFramesPlayback,
+} from './test-utils';
+
+test(
+ 'should render correct frames from embedded videos - WebM codec video',
+ async () => {
+ const missedFrames = await getMissedFramesforCodec('webm', 'codec');
+ expect(missedFrames).toBe(0);
+ },
+ {
+ timeout: 30000,
+ },
+);
+
+test(
+ 'should render correct frames from embedded videos - MP4 codec video',
+ async () => {
+ const missedFrames = await getMissedFramesforCodec('mp4', 'codec');
+ expect(missedFrames).toBeLessThanOrEqual(0);
+ },
+ {
+ timeout: 40000,
+ },
+);
+
+//playback rate, enusure no frames are missed
+
+test(
+ 'should render frames with correct playback speed - MP4 codec video',
+ async () => {
+ const missedFrames = await getMissedFramesPlayback('codec');
+ expect(missedFrames).toBeLessThanOrEqual(0);
+ },
+ {
+ timeout: 40000,
+ },
+);
+
+//should ensure that trimmed version is as expected
+
+test(
+ 'should render correct frames after applying trim - MP4 codec video',
+ async () => {
+ const missedFrames = await getMissedFramesAfterTrim('codec');
+ expect(missedFrames).toBeLessThanOrEqual(0);
+ },
+ {
+ timeout: 40000,
+ },
+);
diff --git a/packages/it-tests/src/rendering/test-utils.ts b/packages/it-tests/src/rendering/test-utils.ts
new file mode 100644
index 0000000000..8f8b77fa14
--- /dev/null
+++ b/packages/it-tests/src/rendering/test-utils.ts
@@ -0,0 +1,189 @@
+import {RenderInternals} from '@remotion/renderer';
+import execa from 'execa';
+import fs from 'fs';
+import os from 'os';
+import path from 'path';
+import {random} from 'remotion';
+import sharp from 'sharp';
+
+interface RgbColor {
+ red: number;
+ blue: number;
+ green: number;
+}
+export const getMissedFramesforCodec = async (
+ codec: 'mp4' | 'webm',
+ type: 'normal' | 'offthread' | 'codec',
+) => {
+ const outputPath = await saveSequenceInTempDir(
+ `video-testing-${codec}${type === 'normal' ? '' : '-' + type}`,
+ );
+
+ let missedFrames = 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 img = await sharp(filename).raw().toBuffer();
+
+ const actualColor = {
+ red: img.readUInt8(0),
+ green: img.readUInt8(1),
+ blue: img.readUInt8(2),
+ };
+
+ missedFrames = missedFrameChecker(
+ expectedColor,
+ actualColor,
+ missedFrames,
+ frame,
+ filename,
+ );
+ }
+ RenderInternals.deleteDirectory(outputPath);
+
+ return missedFrames;
+};
+
+export const getMissedFramesPlayback = async (
+ type: 'normal' | 'offthread' | 'codec',
+) => {
+ const outputPath = await saveSequenceInTempDir(
+ `video-testing-playback${type === 'normal' ? '' : '-' + type}`,
+ );
+
+ let missedFrames = 0;
+
+ //4x playbackrate
+ for (let frame = 0; frame < 25; frame++) {
+ //every 4th frame
+ const expectedColor = {
+ red: selectColor('red', frame * 4),
+ green: selectColor('green', frame * 4),
+ blue: selectColor('blue', frame * 4),
+ };
+
+ const paddedIndex = String(frame).padStart(2, '0');
+ const filename = path.join(outputPath, `element-${paddedIndex}.png`);
+ const img = await sharp(filename).raw().toBuffer();
+
+ const actualColor = {
+ red: img.readUInt8(0),
+ green: img.readUInt8(1),
+ blue: img.readUInt8(2),
+ };
+
+ missedFrames = missedFrameChecker(
+ expectedColor,
+ actualColor,
+ missedFrames,
+ frame,
+ filename,
+ );
+ }
+ RenderInternals.deleteDirectory(outputPath);
+
+ return missedFrames;
+};
+
+export const getMissedFramesAfterTrim = async (
+ type: 'normal' | 'offthread' | 'codec',
+) => {
+ const outputPath = await saveSequenceInTempDir(
+ `video-testing-trim${type === 'normal' ? '' : '-' + type}`,
+ );
+ let missedFrames = 0;
+ for (let frame = 20; frame < 80; frame++) {
+ const expectedColor = {
+ red: selectColor('red', frame),
+ green: selectColor('green', frame),
+ blue: selectColor('blue', frame),
+ };
+
+ const paddedIndex = String(frame - 20).padStart(2, '0');
+ const filename = path.join(outputPath, `element-${paddedIndex}.png`);
+ const img = await sharp(filename).raw().toBuffer();
+
+ const actualColor = {
+ red: img.readUInt8(0),
+ green: img.readUInt8(1),
+ blue: img.readUInt8(2),
+ };
+ missedFrames = missedFrameChecker(
+ expectedColor,
+ actualColor,
+ missedFrames,
+ frame,
+ filename,
+ );
+ }
+ RenderInternals.deleteDirectory(outputPath);
+
+ return missedFrames;
+};
+
+function selectColor(color: string, frame: number) {
+ return Math.floor((random(`${color}-${frame}`) * 255) % 255);
+}
+
+async function saveSequenceInTempDir(id: string) {
+ const outputPath = await fs.promises.mkdtemp(
+ path.join(os.tmpdir(), 'remotion-'),
+ );
+
+ await execa(
+ 'pnpm',
+ [
+ 'exec',
+ 'remotion',
+ 'render',
+ // 'build',
+ id,
+ outputPath,
+ '--image-format',
+ 'png',
+ '--sequence',
+ ],
+ {
+ cwd: path.join(process.cwd(), '..', 'example'),
+ },
+ );
+ return outputPath;
+}
+
+function missedFrameChecker(
+ expectedColor: RgbColor,
+ actualColor: RgbColor,
+ missedFrames: number,
+ frame: number,
+ filename: string,
+) {
+ 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 = 35;
+ if (highestDistance > threshold) {
+ console.log(colorDistance, {threshold, frame, filename});
+ return missedFrames + 1;
+ }
+ return missedFrames;
+}