Skip to content

🎥 Make videos programmatically with React

fix missing frames, make tests pass

Hunain Ahmedd2d7338b4959
diff --git a/packages/core/src/video/codecVideoForRendering.tsx b/packages/core/src/video/codecVideoForRendering.tsx
index 70a787ccfd..34fc9daad8 100644
--- a/packages/core/src/video/codecVideoForRendering.tsx
+++ b/packages/core/src/video/codecVideoForRendering.tsx
@@ -1,4 +1,4 @@
-import React, { useLayoutEffect, useRef } from 'react';
+import React, { useEffect, useLayoutEffect, useRef, useState } from 'react';
import {extractFrames} from '@remotion/webcodecs'
import type {RemotionOffthreadVideoProps} from './props.js';
import { useUnsafeVideoConfig } from '../use-unsafe-video-config.js';
@@ -23,7 +23,7 @@ export const CodecVideoForRendering: React.FC<
delayRenderRetries,
delayRenderTimeoutInMilliseconds,
//call when a frame of the video, i.e. img is rendered
- // onVideoFrame,
+ onVideoFrame,
// // Remove crossOrigin prop during rendering
// // https://discord.com/channels/809501355504959528/844143007183667220/1311639632496033813
// crossOrigin,
@@ -39,6 +39,7 @@ export const CodecVideoForRendering: React.FC<
// const sequenceContext = useContext(SequenceContext);
// const mediaStartsAt = useMediaStartsAt();
const canvasRef = useRef<HTMLCanvasElement>(null);
+ const [activeHandler,setActiveHandler] = useState<any>(null)
// const [frames, setframes] = useState<VideoFrame[]>([]);
const frame = useCurrentFrame();
@@ -120,26 +121,49 @@ export const CodecVideoForRendering: React.FC<
retries: delayRenderRetries ?? undefined,
timeoutInMilliseconds: delayRenderTimeoutInMilliseconds ?? undefined,
});
+ console.log(`new handle created ${newHandle}`)
extractFrames({
src,
- timestampsInSeconds: async () => {
- console.log(`called for frame ${frame} with timestamp ${frame/videoConfig.fps}`)
- return [frame/videoConfig.fps]
+ timestampsInSeconds: async ({ container}) => {
+ let fps = videoConfig.fps;
+ let timestamp; //milisecond precision
+ if(container === "mp4"){
+ //we want the frame one ms ahead, because the timsestamps of mp4 frames show they act this way
+ //also,experiment show that it should be rounded to the nearest ms
+ timestamp = Math.round(((frame+1)/fps)*1000 + 1)/1000;
+ }else{
+ timestamp = Math.round(((frame)/fps)*1000)/1000;
+ }
+ console.log(`called for frame ${frame} with timestamp ${timestamp}`)
+ return [timestamp]
},
onFrame: (extractedFrame) => {
console.log("successfully extracted the frame " + frame);
+ console.log(extractedFrame)
//render the canvas
canvasRef.current?.getContext("2d")?.drawImage(extractedFrame,0, 0);
+ onVideoFrame?.(extractedFrame)
extractedFrame.close()
//continue render
- continueRender(newHandle)
+ setActiveHandler(newHandle)
},
})
},[frame])
+
+useEffect(() => {
+ if (activeHandler !== null) {
+ console.log(`active handler changed, new value: ${activeHandler}`)
+ requestAnimationFrame(() => {
+ console.log(`Frame ${frame} drawn and painted, continuing render`);
+ continueRender(activeHandler);
+ });
+ }
+}, [activeHandler]);
+
return (
<canvas width={videoConfig.width} height={videoConfig.height} ref={canvasRef} />
);
diff --git a/packages/it-tests/src/rendering/frame-accuracy.test.ts b/packages/it-tests/src/rendering/frame-accuracy.test.ts
index c97524c945..faf3d6bfc6 100644
--- a/packages/it-tests/src/rendering/frame-accuracy.test.ts
+++ b/packages/it-tests/src/rendering/frame-accuracy.test.ts
@@ -28,7 +28,7 @@ const getMissedFramesforCodec = async (
'exec',
'remotion',
'render',
- 'build',
+ // 'build',
`video-testing-${codec}${type === 'normal' ? '' : '-'+type}`,
outputPath,
'--image-format',
@@ -137,7 +137,7 @@ test(
expect(missedFrames).toBe(0);
},
{
- timeout: 30000,
+ timeout: 80000,
},
);
@@ -148,6 +148,6 @@ test(
expect(missedFrames).toBeLessThanOrEqual(8);
},
{
- timeout: 40000,
+ timeout: 80000,
},
);
diff --git a/packages/webcodecs/src/extract-frames.ts b/packages/webcodecs/src/extract-frames.ts
index c2bd2b69ee..1f5985e5da 100644
--- a/packages/webcodecs/src/extract-frames.ts
+++ b/packages/webcodecs/src/extract-frames.ts
@@ -1,4 +1,5 @@
import type {
+ MediaParserContainer,
MediaParserLogLevel,
MediaParserVideoSample,
MediaParserVideoTrack,
@@ -15,6 +16,7 @@ import {withResolvers} from './create/with-resolvers';
export type ExtractFramesTimestampsInSecondsFn = (options: {
track: MediaParserVideoTrack;
+ container: MediaParserContainer
durationInSeconds: number | null;
}) => Promise<number[]> | number[];
@@ -53,14 +55,18 @@ const internalExtractFrames = ({
acknowledgeRemotionLicense,
controller,
logLevel,
+ fields: {
+ container: true
+ },
onDurationInSeconds(durationInSeconds) {
dur = durationInSeconds;
},
- onVideoTrack: async ({track}) => {
+ onVideoTrack: async ({track, container}) => {
const timestampTargetsUnsorted =
typeof timestampsInSeconds === 'function'
? await timestampsInSeconds({
track,
+ container,
durationInSeconds: dur,
})
: timestampsInSeconds;