🎥 Make videos programmatically with React
add frame accuracy test, and seek to middle of frames instead of start
cnnrhill661d6fab8f69
diff --git a/package.json b/package.jsonindex b4fc03f6ac..072a8091bf 100644--- a/package.json+++ b/package.json@@ -14,12 +14,14 @@"@jonny/eslint-config": "^2.1.210","@types/jest": "^26.0.20","@types/node": "^14.14.31",+ "@types/sharp": "^0.27.2","eslint": "^7.15.0","execa": "^5.0.0","jest": "^26.6.3","lerna": "^2.11.0","prettier": "^2.2.1","prettier-plugin-organize-imports": "^1.1.1",+ "sharp": "^0.27.2","ts-jest": "^26.5.1","typescript": "^4.1.2"},diff --git a/packages/core/src/video/VideoForDevelopment.tsx b/packages/core/src/video/VideoForDevelopment.tsxindex 3e026c6166..af2af53147 100644--- a/packages/core/src/video/VideoForDevelopment.tsx+++ b/packages/core/src/video/VideoForDevelopment.tsx@@ -35,7 +35,11 @@ export const VideoForDevelopment: React.FC<RemotionVideoProps> = (props) => {// So frame = 0 in Remotion is like frame = 1 for the browserreturn (currentFrame + 1) / videoConfig.fps;}- return currentFrame / (1000 / videoConfig.fps);++ // target middle of frame to reduce risk of rounding errors (which can cause the wrong frame to be shown)+ const msPerFrame = 1000 / videoConfig.fps;+ const msShift = msPerFrame / 2;+ return (currentFrame * msPerFrame + msShift) / 1000;})();if (!playing || absoluteFrame === 0) {diff --git a/packages/core/src/video/VideoForRendering.tsx b/packages/core/src/video/VideoForRendering.tsxindex 182cac9582..a1e1d0c126 100644--- a/packages/core/src/video/VideoForRendering.tsx+++ b/packages/core/src/video/VideoForRendering.tsx@@ -20,7 +20,12 @@ export const VideoForRendering: React.FC<RemotionVideoProps> = ({if (!videoRef.current) {return;}- const frameInSeconds = frame / videoConfig.fps;++ // target middle of frame to reduce risk of rounding errors (which can cause the wrong frame to be shown)+ const msPerFrame = 1000 / videoConfig.fps;+ const msShift = msPerFrame / 2;+ const frameInSeconds = (frame * msPerFrame + msShift) / 1000;+const handle = delayRender();if (videoRef.current.currentTime === frameInSeconds) {if (videoRef.current.readyState >= 2) {diff --git a/packages/example/src/Framer/index.tsx b/packages/example/src/Framer/index.tsxindex aaf2f1e714..3a354f8421 100644--- a/packages/example/src/Framer/index.tsx+++ b/packages/example/src/Framer/index.tsx@@ -1,7 +1,16 @@-import {useCurrentFrame} from 'remotion';+import {useCurrentFrame, random} from 'remotion';++export function selectColor(color: string, frame: number): number {+ return Math.floor((random(`${color}-${frame}`) * 255) % 255);+}export const Framer: React.FC = () => {const frame = useCurrentFrame();++ const red = selectColor('red', frame);+ const green = selectColor('green', frame);+ const blue = selectColor('blue', frame);+return (<divstyle={{@@ -10,9 +19,11 @@ export const Framer: React.FC = () => {alignItems: 'center',background: 'white',flex: 1,+ borderTop: `100px solid rgb(${red}, ${green}, ${blue})`,+ paddingBottom: 100,}}>- <h1 style={{fontSize: 120}}>{frame}</h1>+ <h1 style={{ fontSize: 120 }}>{frame}</h1></div>);};diff --git a/packages/example/src/VideoTesting/framer.mp4 b/packages/example/src/VideoTesting/framer.mp4index dade29dc08..fd6fb9bd65 100644Binary files a/packages/example/src/VideoTesting/framer.mp4 and b/packages/example/src/VideoTesting/framer.mp4 differdiff --git a/packages/example/src/VideoTesting/framer.webm b/packages/example/src/VideoTesting/framer.webmnew file mode 100644index 0000000000..7b7c0095dcBinary files /dev/null and b/packages/example/src/VideoTesting/framer.webm differdiff --git a/packages/example/src/VideoTesting/index.tsx b/packages/example/src/VideoTesting/index.tsxindex b8817c2734..f8c71087ff 100644--- a/packages/example/src/VideoTesting/index.tsx+++ b/packages/example/src/VideoTesting/index.tsx@@ -1,11 +1,11 @@import {Sequence, useVideoConfig, Video} from 'remotion';-import video from './framer.mp4';+import video from './framer.webm';export const VideoTesting: React.FC = () => {const {durationInFrames} = useVideoConfig();return (<div>- <Sequence from={0 - 40} durationInFrames={durationInFrames + 40}>+ <Sequence from={0} durationInFrames={durationInFrames}><Video src={video} /></Sequence></div>diff --git a/tests/frame-accuracy.test.ts b/tests/frame-accuracy.test.tsnew file mode 100644index 0000000000..c98e5498b4--- /dev/null+++ b/tests/frame-accuracy.test.ts@@ -0,0 +1,72 @@+import execa from "execa";+import fs from "fs";+import os from "os";+import path from "path";+import sharp from "sharp";++import {selectColor} from "../packages/example/src/Framer";++test('should render correct frames from embedded videos', async () => {+ 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(+ "npx",+ [+ "remotion",+ "render",+ "src/index.tsx",+ "video-testing",+ outputPath,+ "--image-format",+ "jpeg",+ "--quality",+ "100",+ "--sequence",+ ],+ {+ cwd: "packages/example",+ reject: false,+ }+ );++ 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}.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+ if (colorDistance.red > 5 || colorDistance.green > 5 || colorDistance.blue > 5) {+ missedFrames++;+ }+ }++ expect(missedFrames).toBe(0);+});\ No newline at end of file