Skip to content

🎥 Make videos programmatically with React

draw offthreadvideo to canvas

Jonny Burger48c1e072d46b
diff --git a/packages/core/src/video/VideoForPreview.tsx b/packages/core/src/video/VideoForPreview.tsx
index 64344abcf5..aa1037df53 100644
--- a/packages/core/src/video/VideoForPreview.tsx
+++ b/packages/core/src/video/VideoForPreview.tsx
@@ -23,6 +23,7 @@ import {
useMediaMutedState,
useMediaVolumeState,
} from '../volume-position-state.js';
+import {useEmitVideoFrame} from './emit-video-frame.js';
import type {OnVideoFrame, RemotionVideoProps} from './props';
import {isIosSafari, useAppendVideoFragment} from './video-fragment.js';
@@ -64,6 +65,7 @@ const VideoForDevelopmentRefForwardingFunction: React.ForwardRefRenderFunction<
loopVolumeCurveBehavior,
onError,
onAutoPlayError,
+ onVideoFrame,
...nativeProps
} = props;
@@ -192,6 +194,8 @@ const VideoForDevelopmentRefForwardingFunction: React.ForwardRefRenderFunction<
useRef<VideoForPreviewProps['onDuration']>();
currentOnDurationCallback.current = onDuration;
+ useEmitVideoFrame({ref: videoRef, onVideoFrame});
+
useEffect(() => {
const {current} = videoRef;
if (!current) {
diff --git a/packages/core/src/video/emit-video-frame.ts b/packages/core/src/video/emit-video-frame.ts
index 602e5ffd4c..810b6c2110 100644
--- a/packages/core/src/video/emit-video-frame.ts
+++ b/packages/core/src/video/emit-video-frame.ts
@@ -1,16 +1,23 @@
import {useEffect} from 'react';
import type {OnVideoFrame} from './props';
-export const useEmitVideoFrame = (
- ref: React.RefObject<HTMLVideoElement>,
- onVideoFrame: OnVideoFrame,
-) => {
+export const useEmitVideoFrame = ({
+ ref,
+ onVideoFrame,
+}: {
+ ref: React.RefObject<HTMLVideoElement>;
+ onVideoFrame: OnVideoFrame | null;
+}) => {
useEffect(() => {
const {current} = ref;
if (!current) {
return;
}
+ if (!onVideoFrame) {
+ return;
+ }
+
let handle = 0;
const callback = () => {
if (!ref.current) {
diff --git a/packages/example/src/OffthreadVideoToCanvas/index.tsx b/packages/example/src/OffthreadVideoToCanvas/index.tsx
index 176d72f2bf..0150eaacda 100644
--- a/packages/example/src/OffthreadVideoToCanvas/index.tsx
+++ b/packages/example/src/OffthreadVideoToCanvas/index.tsx
@@ -1,9 +1,58 @@
-import {AbsoluteFill, OffthreadVideo, staticFile} from 'remotion';
+import {useCallback, useRef} from 'react';
+import {
+ AbsoluteFill,
+ OffthreadVideo,
+ staticFile,
+ useVideoConfig,
+} from 'remotion';
export const OffthreadVideoToCanvas: React.FC = () => {
+ const canvas = useRef<HTMLCanvasElement>(null);
+ const {width, height} = useVideoConfig();
+
+ const onVideoFrame = useCallback(
+ (frame: CanvasImageSource) => {
+ if (!canvas.current) {
+ return;
+ }
+ const context = canvas.current.getContext('2d');
+
+ if (!context) {
+ return;
+ }
+
+ context.drawImage(frame, 0, 0, width, height);
+ const imageFrame = context.getImageData(0, 0, width, height);
+ const {length} = imageFrame.data;
+
+ // If the pixel is very green, reduce the alpha channel
+ for (let i = 0; i < length; i += 4) {
+ const red = imageFrame.data[i + 0];
+ const green = imageFrame.data[i + 1];
+ const blue = imageFrame.data[i + 2];
+
+ imageFrame.data[i + 0] = 255 - red;
+ imageFrame.data[i + 1] = 255 - green;
+ imageFrame.data[i + 2] = 255 - blue;
+ }
+ context.putImageData(imageFrame, 0, 0);
+ },
+ [height, width],
+ );
+
return (
- <AbsoluteFill style={{backgroundColor: 'red'}}>
- <OffthreadVideo muted src={staticFile('offthreadvideo.mp4')} />
+ <AbsoluteFill style={{}}>
+ <AbsoluteFill>
+ <OffthreadVideo
+ muted
+ style={{opacity: 0}}
+ src={staticFile('vid1.mp4')}
+ onVideoFrame={onVideoFrame}
+ />
+ </AbsoluteFill>
+ <AbsoluteFill>
+ <canvas ref={canvas} width={width} height={height} />
+ </AbsoluteFill>
</AbsoluteFill>
);
};
diff --git a/packages/example/src/Root.tsx b/packages/example/src/Root.tsx
index 2c9a123154..5f650740f3 100644
--- a/packages/example/src/Root.tsx
+++ b/packages/example/src/Root.tsx
@@ -38,6 +38,7 @@ import {
OffthreadRemoteVideo,
calculateMetadataFn,
} from './OffthreadRemoteVideo/OffthreadRemoteVideo';
+import {OffthreadVideoToCanvas} from './OffthreadVideoToCanvas';
import {OrbScene} from './Orb';
import {ShapesMorph} from './Paths/ShapesMorph';
import {SlicePath} from './Paths/SlicePath';
@@ -621,6 +622,14 @@ export const Index: React.FC = () => {
src: staticFile('vid1.mp4'),
}}
/>
+ <Composition
+ id="OffthreadVideoToCanvas"
+ component={OffthreadVideoToCanvas}
+ fps={30}
+ height={1080}
+ width={1080}
+ durationInFrames={100}
+ />
<Composition
id="video-testing-webm"
component={VideoTesting}