Skip to content

🎥 Make videos programmatically with React

draw image to canvas

Jonny Burger79803020d49c
diff --git a/packages/core/src/Img.tsx b/packages/core/src/Img.tsx
index 74cdfbe4bf..e23d90a7f2 100644
--- a/packages/core/src/Img.tsx
+++ b/packages/core/src/Img.tsx
@@ -27,6 +27,7 @@ export type ImgProps = Omit<
readonly pauseWhenLoading?: boolean;
readonly delayRenderRetries?: number;
readonly delayRenderTimeoutInMilliseconds?: number;
+ readonly onImageFrame?: (imgelement: HTMLImageElement) => void;
readonly src: string;
};
@@ -41,6 +42,7 @@ const ImgRefForwarding: React.ForwardRefRenderFunction<
pauseWhenLoading,
delayRenderRetries,
delayRenderTimeoutInMilliseconds,
+ onImageFrame,
...props
},
ref,
@@ -156,6 +158,10 @@ const ImgRefForwarding: React.ForwardRefRenderFunction<
);
}
+ if (current) {
+ onImageFrame?.(current);
+ }
+
unblock();
continueRender(newHandle);
};
@@ -184,6 +190,7 @@ const ImgRefForwarding: React.ForwardRefRenderFunction<
delayRenderTimeoutInMilliseconds,
pauseWhenLoading,
isPremounting,
+ onImageFrame,
]);
}
diff --git a/packages/core/src/video/OffthreadVideo.tsx b/packages/core/src/video/OffthreadVideo.tsx
index 8c67b60be0..ab3ae51e16 100644
--- a/packages/core/src/video/OffthreadVideo.tsx
+++ b/packages/core/src/video/OffthreadVideo.tsx
@@ -73,6 +73,7 @@ export const OffthreadVideo: React.FC<OffthreadVideoProps> = (props) => {
toneMapped,
_remotionDebugSeeking,
onAutoPlayError,
+ onVideoFrame,
...withoutTransparent
} = otherProps;
@@ -86,6 +87,7 @@ export const OffthreadVideo: React.FC<OffthreadVideoProps> = (props) => {
pauseWhenBuffering={pauseWhenBuffering ?? false}
showInTimeline={showInTimeline ?? true}
onAutoPlayError={onAutoPlayError ?? undefined}
+ onVideoFrame={onVideoFrame ?? null}
{...withoutTransparent}
/>
);
diff --git a/packages/core/src/video/OffthreadVideoForRendering.tsx b/packages/core/src/video/OffthreadVideoForRendering.tsx
index ea78f528f1..f41e9d3215 100644
--- a/packages/core/src/video/OffthreadVideoForRendering.tsx
+++ b/packages/core/src/video/OffthreadVideoForRendering.tsx
@@ -46,6 +46,7 @@ export const OffthreadVideoForRendering: React.FC<OffthreadVideoProps> = ({
loopVolumeCurveBehavior,
delayRenderRetries,
delayRenderTimeoutInMilliseconds,
+ onVideoFrame,
...props
}) => {
const absoluteFrame = useTimelinePosition();
@@ -253,6 +254,15 @@ export const OffthreadVideoForRendering: React.FC<OffthreadVideoProps> = ({
.join(' ');
}, [props.className]);
+ const onImageFrame = useCallback(
+ (img: HTMLImageElement) => {
+ if (onVideoFrame) {
+ onVideoFrame(img);
+ }
+ },
+ [onVideoFrame],
+ );
+
if (!imageSrc) {
return null;
}
@@ -265,6 +275,7 @@ export const OffthreadVideoForRendering: React.FC<OffthreadVideoProps> = ({
className={className}
delayRenderRetries={delayRenderRetries}
delayRenderTimeoutInMilliseconds={delayRenderTimeoutInMilliseconds}
+ onImageFrame={onImageFrame}
{...props}
onError={onErr}
/>
diff --git a/packages/core/src/video/Video.tsx b/packages/core/src/video/Video.tsx
index 13140b29fa..f3aa6e1b47 100644
--- a/packages/core/src/video/Video.tsx
+++ b/packages/core/src/video/Video.tsx
@@ -34,6 +34,7 @@ const VideoForwardingFunction: React.ForwardRefRenderFunction<
_remotionInternalNativeLoopPassed,
showInTimeline,
onAutoPlayError,
+ onVideoFrame,
...otherProps
} = props;
const {loop, _remotionDebugSeeking, ...propsOtherThanLoop} = props;
@@ -84,6 +85,7 @@ const VideoForwardingFunction: React.ForwardRefRenderFunction<
<Video
{...propsOtherThanLoop}
ref={ref}
+ onVideoFrame={onVideoFrame}
_remotionInternalNativeLoopPassed
/>
</Loop>
@@ -105,6 +107,7 @@ const VideoForwardingFunction: React.ForwardRefRenderFunction<
>
<Video
pauseWhenBuffering={pauseWhenBuffering ?? false}
+ onVideoFrame={onVideoFrame}
{...otherProps}
ref={ref}
/>
@@ -116,7 +119,12 @@ const VideoForwardingFunction: React.ForwardRefRenderFunction<
if (environment.isRendering) {
return (
- <VideoForRendering onDuration={onDuration} {...otherProps} ref={ref} />
+ <VideoForRendering
+ onDuration={onDuration}
+ onVideoFrame={onVideoFrame ?? null}
+ {...otherProps}
+ ref={ref}
+ />
);
}
@@ -125,6 +133,7 @@ const VideoForwardingFunction: React.ForwardRefRenderFunction<
onlyWarnForMediaSeekingError={false}
{...otherProps}
ref={ref}
+ onVideoFrame={onVideoFrame ?? null}
// Proposal: Make this default to true in v5
pauseWhenBuffering={pauseWhenBuffering ?? false}
onDuration={onDuration}
diff --git a/packages/core/src/video/VideoForPreview.tsx b/packages/core/src/video/VideoForPreview.tsx
index dcb15d7206..64344abcf5 100644
--- a/packages/core/src/video/VideoForPreview.tsx
+++ b/packages/core/src/video/VideoForPreview.tsx
@@ -23,7 +23,7 @@ import {
useMediaMutedState,
useMediaVolumeState,
} from '../volume-position-state.js';
-import type {RemotionVideoProps} from './props';
+import type {OnVideoFrame, RemotionVideoProps} from './props';
import {isIosSafari, useAppendVideoFragment} from './video-fragment.js';
type VideoForPreviewProps = RemotionVideoProps & {
@@ -34,6 +34,7 @@ type VideoForPreviewProps = RemotionVideoProps & {
readonly _remotionInternalStack: string | null;
readonly _remotionDebugSeeking: boolean;
readonly showInTimeline: boolean;
+ readonly onVideoFrame: null | OnVideoFrame;
};
const VideoForDevelopmentRefForwardingFunction: React.ForwardRefRenderFunction<
diff --git a/packages/core/src/video/VideoForRendering.tsx b/packages/core/src/video/VideoForRendering.tsx
index 419ae2c722..75eef07ce0 100644
--- a/packages/core/src/video/VideoForRendering.tsx
+++ b/packages/core/src/video/VideoForRendering.tsx
@@ -24,11 +24,12 @@ import {useCurrentFrame} from '../use-current-frame.js';
import {useUnsafeVideoConfig} from '../use-unsafe-video-config.js';
import {evaluateVolume} from '../volume-prop.js';
import {getMediaTime} from './get-current-time.js';
-import type {RemotionVideoProps} from './props';
+import type {OnVideoFrame, RemotionVideoProps} from './props';
import {seekToTimeMultipleUntilRight} from './seek-until-right.js';
type VideoForRenderingProps = RemotionVideoProps & {
readonly onDuration: (src: string, durationInSeconds: number) => void;
+ readonly onVideoFrame: null | OnVideoFrame;
};
const VideoForRenderingForwardFunction: React.ForwardRefRenderFunction<
diff --git a/packages/core/src/video/emit-video-frame.ts b/packages/core/src/video/emit-video-frame.ts
new file mode 100644
index 0000000000..602e5ffd4c
--- /dev/null
+++ b/packages/core/src/video/emit-video-frame.ts
@@ -0,0 +1,30 @@
+import {useEffect} from 'react';
+import type {OnVideoFrame} from './props';
+
+export const useEmitVideoFrame = (
+ ref: React.RefObject<HTMLVideoElement>,
+ onVideoFrame: OnVideoFrame,
+) => {
+ useEffect(() => {
+ const {current} = ref;
+ if (!current) {
+ return;
+ }
+
+ let handle = 0;
+ const callback = () => {
+ if (!ref.current) {
+ return;
+ }
+
+ onVideoFrame(ref.current);
+ handle = ref.current.requestVideoFrameCallback(callback);
+ };
+
+ callback();
+
+ return () => {
+ current.cancelVideoFrameCallback(handle);
+ };
+ }, [onVideoFrame, ref]);
+};
diff --git a/packages/core/src/video/props.ts b/packages/core/src/video/props.ts
index 6c73060e45..b8ce862e4a 100644
--- a/packages/core/src/video/props.ts
+++ b/packages/core/src/video/props.ts
@@ -10,6 +10,7 @@ export type RemotionMainVideoProps = {
*/
_remotionInternalNativeLoopPassed?: boolean;
_remotionDebugSeeking?: boolean;
+ onVideoFrame?: OnVideoFrame;
};
export type RemotionVideoProps = Omit<
@@ -71,5 +72,8 @@ export type OffthreadVideoProps = {
stack?: string;
showInTimeline?: boolean;
onAutoPlayError?: null | (() => void);
+ onVideoFrame?: OnVideoFrame;
} & RemotionMainVideoProps &
DeprecatedOffthreadVideoProps;
+
+export type OnVideoFrame = (frame: CanvasImageSource) => void;
diff --git a/packages/example/src/OffthreadVideoToCanvas/index.tsx b/packages/example/src/OffthreadVideoToCanvas/index.tsx
new file mode 100644
index 0000000000..176d72f2bf
--- /dev/null
+++ b/packages/example/src/OffthreadVideoToCanvas/index.tsx
@@ -0,0 +1,9 @@
+import {AbsoluteFill, OffthreadVideo, staticFile} from 'remotion';
+
+export const OffthreadVideoToCanvas: React.FC = () => {
+ return (
+ <AbsoluteFill style={{backgroundColor: 'red'}}>
+ <OffthreadVideo muted src={staticFile('offthreadvideo.mp4')} />
+ </AbsoluteFill>
+ );
+};