Skip to content

πŸŽ₯ Make videos programmatically with React

new video manipulation page

Jonny Burger382f6617823e
diff --git a/packages/core/src/video/OffthreadVideo.tsx b/packages/core/src/video/OffthreadVideo.tsx
index ab3ae51e16..4aa87beb76 100644
--- a/packages/core/src/video/OffthreadVideo.tsx
+++ b/packages/core/src/video/OffthreadVideo.tsx
@@ -74,6 +74,7 @@ export const OffthreadVideo: React.FC<OffthreadVideoProps> = (props) => {
_remotionDebugSeeking,
onAutoPlayError,
onVideoFrame,
+ crossOrigin,
...withoutTransparent
} = otherProps;
@@ -88,6 +89,7 @@ export const OffthreadVideo: React.FC<OffthreadVideoProps> = (props) => {
showInTimeline={showInTimeline ?? true}
onAutoPlayError={onAutoPlayError ?? undefined}
onVideoFrame={onVideoFrame ?? null}
+ crossOrigin={crossOrigin}
{...withoutTransparent}
/>
);
diff --git a/packages/core/src/video/Video.tsx b/packages/core/src/video/Video.tsx
index f3aa6e1b47..972e4655ce 100644
--- a/packages/core/src/video/Video.tsx
+++ b/packages/core/src/video/Video.tsx
@@ -35,6 +35,7 @@ const VideoForwardingFunction: React.ForwardRefRenderFunction<
showInTimeline,
onAutoPlayError,
onVideoFrame,
+ crossOrigin,
...otherProps
} = props;
const {loop, _remotionDebugSeeking, ...propsOtherThanLoop} = props;
@@ -144,6 +145,7 @@ const VideoForwardingFunction: React.ForwardRefRenderFunction<
_remotionDebugSeeking={_remotionDebugSeeking ?? false}
showInTimeline={showInTimeline ?? true}
onAutoPlayError={onAutoPlayError ?? undefined}
+ crossOrigin={crossOrigin}
/>
);
};
diff --git a/packages/core/src/video/VideoForPreview.tsx b/packages/core/src/video/VideoForPreview.tsx
index aa1037df53..c55f4c471e 100644
--- a/packages/core/src/video/VideoForPreview.tsx
+++ b/packages/core/src/video/VideoForPreview.tsx
@@ -36,6 +36,7 @@ type VideoForPreviewProps = RemotionVideoProps & {
readonly _remotionDebugSeeking: boolean;
readonly showInTimeline: boolean;
readonly onVideoFrame: null | OnVideoFrame;
+ readonly crossOrigin: '' | 'anonymous' | 'use-credentials' | undefined;
};
const VideoForDevelopmentRefForwardingFunction: React.ForwardRefRenderFunction<
@@ -66,6 +67,7 @@ const VideoForDevelopmentRefForwardingFunction: React.ForwardRefRenderFunction<
onError,
onAutoPlayError,
onVideoFrame,
+ crossOrigin,
...nativeProps
} = props;
@@ -245,6 +247,8 @@ const VideoForDevelopmentRefForwardingFunction: React.ForwardRefRenderFunction<
};
}, [isSequenceHidden, style]);
+ const crossOriginValue = crossOrigin ?? (onVideoFrame ? 'anonymous' : '');
+
return (
<video
ref={videoRef}
@@ -254,6 +258,7 @@ const VideoForDevelopmentRefForwardingFunction: React.ForwardRefRenderFunction<
loop={_remotionInternalNativeLoopPassed}
style={actualStyle}
disableRemotePlayback
+ crossOrigin={crossOriginValue}
{...nativeProps}
/>
);
diff --git a/packages/core/src/video/props.ts b/packages/core/src/video/props.ts
index b8ce862e4a..70efd65ea3 100644
--- a/packages/core/src/video/props.ts
+++ b/packages/core/src/video/props.ts
@@ -73,6 +73,7 @@ export type OffthreadVideoProps = {
showInTimeline?: boolean;
onAutoPlayError?: null | (() => void);
onVideoFrame?: OnVideoFrame;
+ crossOrigin?: '' | 'anonymous' | 'use-credentials';
} & RemotionMainVideoProps &
DeprecatedOffthreadVideoProps;
diff --git a/packages/docs/components/GreenscreenExamples/index.tsx b/packages/docs/components/GreenscreenExamples/index.tsx
index f5fb389758..1f772374c4 100644
--- a/packages/docs/components/GreenscreenExamples/index.tsx
+++ b/packages/docs/components/GreenscreenExamples/index.tsx
@@ -1,187 +1,150 @@
-import { Player } from "@remotion/player";
-import React, { useCallback, useEffect, useRef, useState } from "react";
-import { AbsoluteFill, useVideoConfig, Video } from "remotion";
+import {Player} from '@remotion/player';
+import React, {useCallback, useRef, useState} from 'react';
+import {AbsoluteFill, OffthreadVideo, useVideoConfig} from 'remotion';
export const Greenscreen: React.FC<{
- opacity: number;
-}> = ({ opacity }) => {
- const video = useRef<HTMLVideoElement>(null);
- const canvas = useRef<HTMLCanvasElement>(null);
- const { width, height } = useVideoConfig();
-
- const onVideoFrame = useCallback(
- (op: number) => {
- if (
- !canvas.current ||
- !video.current ||
- !video.current.requestVideoFrameCallback
- ) {
- return;
- }
-
- const context = canvas.current.getContext("2d");
-
- if (!context) {
- return;
- }
-
- context.drawImage(video.current, 0, 0, width, height);
- const imageFrame = context.getImageData(0, 0, width, height);
- const { length } = imageFrame.data;
-
- 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];
- if (green > 100 && red < 100 && blue < 100) {
- imageFrame.data[i + 3] = op * 255;
- }
- }
-
- context.putImageData(imageFrame, 0, 0);
- },
- [height, width]
- );
-
- useEffect(() => {
- if (!video.current?.requestVideoFrameCallback) {
- return;
- }
-
- const { current } = video;
- let handle = 0;
- const callback = () => {
- onVideoFrame(opacity);
- handle = current.requestVideoFrameCallback(callback) as unknown as number;
- };
-
- callback();
-
- return () => {
- current.cancelVideoFrameCallback(handle);
- };
- }, [onVideoFrame, opacity]);
-
- return (
- <AbsoluteFill>
- <AbsoluteFill>
- <Video
- ref={video}
- style={{ opacity: 0 }}
- startFrom={300}
- crossOrigin="anonymous"
- src="https://remotion-assets.s3.eu-central-1.amazonaws.com/just-do-it.mp4"
- />
- </AbsoluteFill>
- <AbsoluteFill>
- <canvas ref={canvas} width={width} height={height} />
- </AbsoluteFill>
- </AbsoluteFill>
- );
+ readonly opacity: number;
+}> = ({opacity}) => {
+ 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;
+
+ 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];
+ if (green > 100 && red < 100 && blue < 100) {
+ imageFrame.data[i + 3] = opacity * 255;
+ }
+ }
+
+ context.putImageData(imageFrame, 0, 0);
+ },
+ [height, opacity, width],
+ );
+
+ return (
+ <AbsoluteFill>
+ <AbsoluteFill>
+ <OffthreadVideo
+ style={{opacity: 0}}
+ onVideoFrame={onVideoFrame}
+ src="https://remotion-assets.s3.eu-central-1.amazonaws.com/just-do-it.mp4"
+ />
+ </AbsoluteFill>
+ <AbsoluteFill>
+ <canvas ref={canvas} width={width} height={height} />
+ </AbsoluteFill>
+ </AbsoluteFill>
+ );
};
export const VideoOnCanvas: React.FC = () => {
- const video = useRef<HTMLVideoElement>(null);
- const canvas = useRef<HTMLCanvasElement>(null);
- const { width, height } = useVideoConfig();
-
- const onVideoFrame = useCallback(() => {
- if (
- !canvas.current ||
- !video.current ||
- !video.current.requestVideoFrameCallback
- ) {
- return;
- }
-
- const context = canvas.current.getContext("2d");
-
- if (!context) {
- return;
- }
-
- context.filter = "grayscale(100%)";
- context.drawImage(video.current, 0, 0, width, height);
-
- video.current.requestVideoFrameCallback(() => onVideoFrame());
- }, [height, width]);
-
- useEffect(() => {
- if (!video.current?.requestVideoFrameCallback) {
- return;
- }
-
- video.current.requestVideoFrameCallback(() => onVideoFrame());
- }, [onVideoFrame]);
-
- return (
- <AbsoluteFill>
- <AbsoluteFill>
- <Video
- ref={video}
- style={{ opacity: 0 }}
- startFrom={300}
- src="https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"
- />
- </AbsoluteFill>
- <AbsoluteFill>
- <canvas ref={canvas} width={width} height={height} />
- </AbsoluteFill>
- </AbsoluteFill>
- );
+ 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.filter = 'grayscale(100%)';
+ context.drawImage(frame, 0, 0, width, height);
+ },
+ [height, width],
+ );
+
+ return (
+ <AbsoluteFill>
+ <AbsoluteFill>
+ <OffthreadVideo
+ onVideoFrame={onVideoFrame}
+ style={{opacity: 0}}
+ startFrom={300}
+ src="https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"
+ />
+ </AbsoluteFill>
+ <AbsoluteFill>
+ <canvas ref={canvas} width={width} height={height} />
+ </AbsoluteFill>
+ </AbsoluteFill>
+ );
};
export const VideoCanvasExamples: React.FC<{
- type: "base" | "greenscreen";
-}> = ({ type }) => {
- const component = (() => {
- if (type === "greenscreen") {
- return Greenscreen;
- }
-
- return VideoOnCanvas;
- })();
- const [effect, setEffect] = useState(0);
- return (
- <div>
- <Player
- component={component}
- compositionWidth={1280}
- compositionHeight={720}
- controls
- durationInFrames={150}
- fps={30}
- style={{
- width: "100%",
- }}
- loop
- inputProps={{
- opacity: 1 - effect,
- }}
- />
- {type === "greenscreen" ? (
- <>
- <br />
- <div
- style={{
- flexDirection: "row",
- alignItems: "center",
- display: "flex",
- }}
- >
- Slide to adjust transparency:
- <input
- onChange={(e) => setEffect(Number(e.target.value))}
- type="range"
- min={0}
- max={1}
- step={1 / 255}
- value={effect}
- style={{ marginLeft: 10 }}
- />
- </div>
- </>
- ) : null}
- </div>
- );
+ readonly type: 'base' | 'greenscreen';
+}> = ({type}) => {
+ const component = (() => {
+ if (type === 'greenscreen') {
+ return Greenscreen;
+ }
+
+ return VideoOnCanvas;
+ })();
+ const [effect, setEffect] = useState(0);
+ return (
+ <div>
+ <Player
+ component={component}
+ compositionWidth={1280}
+ compositionHeight={720}
+ controls
+ durationInFrames={150}
+ doubleClickToFullscreen
+ fps={30}
+ style={{
+ width: '100%',
+ }}
+ loop
+ inputProps={{
+ opacity: 1 - effect,
+ }}
+ />
+ {type === 'greenscreen' ? (
+ <>
+ <br />
+ <div
+ style={{
+ flexDirection: 'row',
+ alignItems: 'center',
+ display: 'flex',
+ }}
+ >
+ Slide to adjust transparency:
+ <input
+ onChange={(e) => setEffect(Number(e.target.value))}
+ type="range"
+ min={0}
+ max={1}
+ step={1 / 255}
+ value={effect}
+ style={{marginLeft: 10}}
+ />
+ </div>
+ </>
+ ) : null}
+ </div>
+ );
};
diff --git a/packages/docs/docs/video-manipulation.mdx b/packages/docs/docs/video-manipulation.mdx
index c78e18ac8f..1a69a7b3aa 100644
--- a/packages/docs/docs/video-manipulation.mdx
+++ b/packages/docs/docs/video-manipulation.mdx
@@ -7,43 +7,24 @@ crumb: "How To"
import { VideoCanvasExamples } from "../components/GreenscreenExamples/index";
-You can manipulate a video buffer by rendering a [`<Video>`](/docs/video) onto a `<canvas>` element using the [`drawImage()`](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/drawImage) API and keeping it in sync using the [`requestVideoFrameCallback()`](https://blog.tomayac.com/2020/05/15/the-requestvideoframecallback-api/) API.
+You can draw frames of an [`<OffthreadVideo>`](/docs/offthreadvideo) or a [`<Video>`](/docs/video) onto a `<canvas>` element using the [`drawImage()`](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/drawImage) API.
:::note
-This API currently [only works in Chrome](https://caniuse.com/mdn-api_htmlvideoelement_requestvideoframecallback).
+During preview, makes use of the [`requestVideoFrameCallback()`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLVideoElement/requestVideoFrameCallback) API.
+Browser support: Firefox 130 (August 2024), Chrome 83, Safari 15.4.
:::
## Basic example
-In this example, a Video is rendered and made invisible. Then it is rendered onto a Canvas and a grayscale [`filter`](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/filter) is applied.
+In this example, an [`<OffthreadVideo>`](/docs/offthreadvideo) is rendered and made invisible.
+Every frame that is emitted is drawn to a Canvas and a grayscale [`filter`](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/filter) is applied.
<VideoCanvasExamples type="base"/>
<br/>
```tsx twoslash
import React, { useCallback, useEffect, useRef } from "react";
-import { AbsoluteFill, useVideoConfig, Video } from "remotion";
-declare global {
- interface VideoFrameMetadata {
- presentationTime: DOMHighResTimeStamp;
- expectedDisplayTime: DOMHighResTimeStamp;
- width: number;
- height: number;
- mediaTime: number;
- presentedFrames: number;
- processingDuration?: number;
- captureTime?: DOMHighResTimeStamp;
- receiveTime?: DOMHighResTimeStamp;
- rtpTimestamp?: number;
- }
- type VideoFrameRequestCallbackId = number;
- interface HTMLVideoElement extends HTMLMediaElement {
- requestVideoFrameCallback(
- callback: (now: DOMHighResTimeStamp, metadata: VideoFrameMetadata) => any
- ): VideoFrameRequestCallbackId;
- cancelVideoFrameCallback(handle: VideoFrameRequestCallbackId): void;
- }
-}
+import { AbsoluteFill, useVideoConfig, OffthreadVideo } from "remotion";
// ---cut---
export const VideoOnCanvas: React.FC = () => {
const video = useRef<HTMLVideoElement>(null);
@@ -51,8 +32,8 @@ export const VideoOnCanvas: React.FC = () => {
const { width, height } = useVideoConfig();
// Process a frame
- const onVideoFrame = useCallback(() => {
- if (!canvas.current || !video.current) {
+ const onVideoFrame = useCallback((frame: CanvasImageSource) => {
+ if (!canvas.current ) {
return;
}
const context = canvas.current.getContext("2d");
@@ -62,37 +43,16 @@ export const VideoOnCanvas: React.FC = () => {
}
context.filter = "grayscale(100%)";
- context.drawImage(video.current, 0, 0, width, height);
+ context.drawImage(frame, 0, 0, width, height);
}, [height, width]);
- // Synchronize the video with the canvas
- useEffect(() => {
- const { current } = video;
- if (!current?.requestVideoFrameCallback) {
- return;
- }
-
- let handle = 0;
- const callback = () => {
- onVideoFrame();
- handle = current.requestVideoFrameCallback(callback);
- };
-
- callback();
-
- return () => {
- current.cancelVideoFrameCallback(handle);
- };
- }, [onVideoFrame]);
-
return (
<AbsoluteFill>
<AbsoluteFill>
- <Video
- ref={video}
+ <OffthreadVideo
// Hide the original video tag
style={{ opacity: 0 }}
- startFrom={300}
+ onVideoFrame={onVideoFrame}
src="http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"
/>
</AbsoluteFill>
@@ -134,20 +94,19 @@ declare global {
}
}
import React, { useCallback, useEffect, useRef } from "react";
-import { AbsoluteFill, useVideoConfig, Video } from "remotion";
+import { AbsoluteFill, useVideoConfig, OffthreadVideo } from "remotion";
// ---cut---
export const Greenscreen: React.FC<{
opacity: number;
}> = ({ opacity }) => {
- const video = useRef<HTMLVideoElement>(null);
const canvas = useRef<HTMLCanvasElement>(null);
const { width, height } = useVideoConfig();
// Process a frame
const onVideoFrame = useCallback(
- (opacity: number) => {
- if (!canvas.current || !video.current) {
+ (frame: CanvasImageSource) => {
+ if (!canvas.current ) {
return;
}
const context = canvas.current.getContext("2d");
@@ -156,7 +115,7 @@ export const Greenscreen: React.FC<{
return;
}
- context.drawImage(video.current, 0, 0, width, height);
+ context.drawImage(frame, 0, 0, width, height);
const imageFrame = context.getImageData(0, 0, width, height);
const { length } = imageFrame.data;
@@ -174,33 +133,14 @@ export const Greenscreen: React.FC<{
[height, width]
);
- useEffect(() => {
- const { current } = video;
- if (!current || !current.requestVideoFrameCallback) {
- return;
- }
- let handle = 0;
- const callback = () => {
- onVideoFrame(opacity);
- handle = current.requestVideoFrameCallback(callback);
- };
-
- callback();
-
- return () => {
- current.cancelVideoFrameCallback(handle);
- };
- }, [onVideoFrame, opacity]);
return (
<AbsoluteFill>
<AbsoluteFill>
- <Video
- ref={video}
+ <OffthreadVideo
style={{ opacity: 0 }}
startFrom={300}
- // If we access the data of a remote video, we must add this prop, and the remote video must have CORS enabled
- crossOrigin="anonymous"
+ onVideoFrame={onVideoFrame}
src="https://remotion-assets.s3.eu-central-1.amazonaws.com/just-do-it.mp4"
/>
</AbsoluteFill>
@@ -212,7 +152,8 @@ export const Greenscreen: React.FC<{
};
```
-## TypeScript issues
+## Before v4.0.190
-In our experience, the types for `requestVideoFrameCallback` and `cancelVideoFrameCallback` are missing or wrong by default. Install the newest version for [`@types/web`](https://www.npmjs.com/package/@types/web)
-or add a `// @ts-expect-error` comment to suppress the errors.
+Before v4.0.190, the `onVideoFrame` prop of [`<OffthreadVideo>`](/docs/offthreadvideo) and [`<Video>`](/docs/video) was not supported.
+You could only manipulate a `<Video>` using the `requestVideoFrameCallback` API.
+Click [here](https://github.com/remotion-dev/remotion/blob/b966d0e99cfb91478ca697675f822284da1f9055/packages/docs/docs/video-manipulation.mdx) to see the old version of this page.
diff --git a/packages/docs/docs/video-vs-offthreadvideo.mdx b/packages/docs/docs/video-vs-offthreadvideo.mdx
index 8b4de17645..9ac1ec751b 100644
--- a/packages/docs/docs/video-vs-offthreadvideo.mdx
+++ b/packages/docs/docs/video-vs-offthreadvideo.mdx
@@ -5,32 +5,19 @@ title: "<Video> vs. <OffthreadVideo>"
crumb: "Comparison"
---
-We offer two components for including other videos in your video: [`<Video />`](/docs/video) and [`<OffthreadVideo />`](/docs/offthreadvideo).
+We offer two components for including other videos in your video: [`<Video />`](/docs/video) and [`<OffthreadVideo />`](/docs/offthreadvideo).
This page offers a comparison to help you decide which tag to use.
-## [`<Video />`](/docs/video)
-
-Is based on the native HTML5 `<video>` element and therefore behaves similar to it.
-
-**Pros**
-
-βœ… &nbsp; You can attach a ref and [draw the video to a canvas](/docs/video-manipulation).
-βœ… &nbsp; Can be rendered without having to be downloaded fully (only if you don't need audio and pass `muted`).
-βœ… &nbsp; [Renders embedded transparent videos quickly](/docs/transparent-videos).
-
-**Cons**
-
-β›” &nbsp; Chrome may throttle video tags if the page is heavy.
-β›” &nbsp; If too many video tags get rendered simultaneously, a timeout may occur.
-β›” &nbsp; If the input video framerate does not match with the output framerate, some duplicate frames may occur in the output.
-β›” &nbsp; A Google Chrome build with proprietary codecs is required.
-
## [`<OffthreadVideo />`](/docs/offthreadvideo)
+:::info
+Prefer [`<OffthreadVideo />`](/docs/offthreadvideo) whenever possible.
+:::
+
A more sophisticated way of embedding a video, which:
-- renders a `<video>` tag during preview
-- displays the exact frame in an [`<Img>`](/docs/img) tag during rendering, that frame gets extracted using FFmpeg outside the browser
+- **During preview:** renders a HTML5 `<video>` tag.
+- **While rendering:** Extracts the exact frame outside the browser and displays it in an [`<Img>`](/docs/img) tag.
**Pros**
@@ -42,9 +29,24 @@ A more sophisticated way of embedding a video, which:
**Cons**
β›” &nbsp; The video needs to be downloaded fully before a frame can be rendered.
-β›” &nbsp; No ref can be attached to this element, as it is `<video>` during preview but a `<Img/>` during render.
-β›” &nbsp; Does not natively support the `loop` attribute. Checkout [looping an offthread video](/docs/offthreadvideo#looping-a-video) for more info.
-β›” &nbsp; The video cannot be drawn to a canvas.
-β›” &nbsp; Supports embedded transparent videos only if [`transparent`](/docs/offthreadvideo#transparent-) is set which leads to a performance penalty.
+β›” &nbsp; No ref can be attached to this element.
+β›” &nbsp; More work is required to loop a video. Check out: [Looping an `<Offthread>` video](/docs/offthreadvideo#looping-a-video).
+β›” &nbsp; Supports transparent videos only if the [`transparent`](/docs/offthreadvideo#transparent-) is set which is a bit slower.
+
+## [`<Video />`](/docs/video)
+
+Is based on the native HTML5 `<video>` element and therefore behaves similar to it.
+
+**Pros**
+
+βœ… &nbsp; Can render a video without having it to be downloaded fully (if you don't pass the [`muted`](/docs/video/#muted) prop, the video will still be downloaded fully to extract its audio).
+βœ… &nbsp; You can attach a ref to the `<video>` element.
+
+**Cons**
+
+β›” &nbsp; Fewer codecs are supported.
+β›” &nbsp; Chrome may throttle video tags if the page is heavy.
+β›” &nbsp; If too many video tags get rendered simultaneously, a timeout may occur.
+β›” &nbsp; If the input video framerate does not match with the output framerate, some duplicate frames may occur in the output.
+β›” &nbsp; A Google Chrome build with proprietary codecs is required.
-Prefer [`<OffthreadVideo />`](/docs/offthreadvideo) whenever possible.
diff --git a/packages/example/src/VideoOnCanvas/greenscreen.tsx b/packages/example/src/VideoOnCanvas/greenscreen.tsx
index 18d026bfcc..529b8c494a 100644
--- a/packages/example/src/VideoOnCanvas/greenscreen.tsx
+++ b/packages/example/src/VideoOnCanvas/greenscreen.tsx
@@ -1,61 +1,46 @@
-import React, {useCallback, useEffect, useRef} from 'react';
-import {AbsoluteFill, Video, useVideoConfig} from 'remotion';
+import React, {useCallback, useRef} from 'react';
+import {AbsoluteFill, OffthreadVideo, useVideoConfig} from 'remotion';
export const Greenscreen: React.FC = () => {
- const video = useRef<HTMLVideoElement>(null);
const canvas = useRef<HTMLCanvasElement>(null);
const {width, height} = useVideoConfig();
- const onVideoFrame = useCallback(() => {
- if (
- !canvas.current ||
- !video.current ||
- !video.current.requestVideoFrameCallback
- ) {
- return;
- }
-
- const context = canvas.current.getContext('2d');
-
- if (!context) {
- return;
- }
-
- context.drawImage(video.current, 0, 0, width, height);
- const imageFrame = context.getImageData(0, 0, width, height);
- const {length} = imageFrame.data;
-
- 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];
- if (green > 100 && red < 100 && blue < 100) {
- imageFrame.data[i + 3] = 0;
+ const onVideoFrame = useCallback(
+ (frame: CanvasImageSource) => {
+ if (!canvas.current) {
+ return;
}
- }
+ const context = canvas.current.getContext('2d');
- context.putImageData(imageFrame, 0, 0);
-
- video.current.requestVideoFrameCallback(() => onVideoFrame());
- }, [height, width]);
+ if (!context) {
+ return;
+ }
- useEffect(() => {
- if (!video.current || !video.current.requestVideoFrameCallback) {
- return;
- }
+ context.drawImage(frame, 0, 0, width, height);
+ const imageFrame = context.getImageData(0, 0, width, height);
+ const {length} = imageFrame.data;
+
+ 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];
+ if (green > 100 && red < 100 && blue < 100) {
+ imageFrame.data[i + 3] = 0;
+ }
+ }
- video.current.requestVideoFrameCallback(() => onVideoFrame());
- }, [onVideoFrame]);
+ context.putImageData(imageFrame, 0, 0);
+ },
+ [height, width],
+ );
return (
<AbsoluteFill>
<AbsoluteFill>
- <Video
- ref={video}
+ <OffthreadVideo
style={{opacity: 0}}
- startFrom={300}
- crossOrigin="anonymous"
src="https://remotion-assets.s3.eu-central-1.amazonaws.com/just-do-it.mp4"
+ onVideoFrame={onVideoFrame}
/>
</AbsoluteFill>
<AbsoluteFill>