Skip to content

🎥 Make videos programmatically with React

: Add OffthreadVideo frame callback

Alec Watts9cc53484e64e
diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts
index aa4b4bf2a2..00717f8072 100644
--- a/packages/core/src/index.ts
+++ b/packages/core/src/index.ts
@@ -247,7 +247,7 @@ export {
Video,
} from './video/index.js';
export {MediaPlaybackError} from './video/MediaPlaybackError.js';
-export type {OnVideoFrame} from './video/props.js';
+export type {OnVideoFrame, OnVideoFrameCallback} from './video/props.js';
export type {VolumeProp} from './volume-prop.js';
export {watchStaticFile} from './watch-static-file.js';
diff --git a/packages/core/src/test/offthread-video.test.tsx b/packages/core/src/test/offthread-video.test.tsx
index 8aec63cce7..c1348f4016 100644
--- a/packages/core/src/test/offthread-video.test.tsx
+++ b/packages/core/src/test/offthread-video.test.tsx
@@ -107,6 +107,16 @@ describe('OffthreadVideo render correctly with props', () => {
).not.toThrow();
});
+ test('It should render OffthreadVideo with onVideoFrameCallback prop', () => {
+ expect(() =>
+ render(
+ <WrapSequenceContext>
+ <OffthreadVideo src="test" onVideoFrameCallback={() => undefined} />
+ </WrapSequenceContext>,
+ ),
+ ).not.toThrow();
+ });
+
test('It should throw when both startFrom and trimBefore are provided', () => {
expect(() =>
render(
diff --git a/packages/core/src/test/video.test.tsx b/packages/core/src/test/video.test.tsx
index 667f41b1cd..9eac6203b4 100644
--- a/packages/core/src/test/video.test.tsx
+++ b/packages/core/src/test/video.test.tsx
@@ -72,6 +72,16 @@ test('It should render Video with trimBefore and trimAfter props', () => {
).not.toThrow();
});
+test('It should render Video with onVideoFrameCallback prop', () => {
+ expect(() =>
+ render(
+ <WrapSequenceContext>
+ <Html5Video src="test" onVideoFrameCallback={() => undefined} />
+ </WrapSequenceContext>,
+ ),
+ ).not.toThrow();
+});
+
test('It should throw when both startFrom and trimBefore are provided', () => {
expect(() =>
render(
diff --git a/packages/core/src/video/OffthreadVideo.tsx b/packages/core/src/video/OffthreadVideo.tsx
index 4ccbbd40e3..22f442a9db 100644
--- a/packages/core/src/video/OffthreadVideo.tsx
+++ b/packages/core/src/video/OffthreadVideo.tsx
@@ -107,6 +107,7 @@ export const InnerOffthreadVideo: React.FC<AllOffthreadVideoProps> = (
toneMapped,
onAutoPlayError,
onVideoFrame,
+ onVideoFrameCallback,
crossOrigin,
delayRenderRetries,
delayRenderTimeoutInMilliseconds,
@@ -122,6 +123,7 @@ export const InnerOffthreadVideo: React.FC<AllOffthreadVideoProps> = (
showInTimeline={showInTimeline ?? true}
onAutoPlayError={onAutoPlayError ?? undefined}
onVideoFrame={onVideoFrame ?? null}
+ onVideoFrameCallback={onVideoFrameCallback ?? null}
crossOrigin={crossOrigin}
{...propsForPreview}
_remotionInternalNativeLoopPassed={false}
@@ -150,6 +152,7 @@ export const OffthreadVideo: React.FC<RemotionOffthreadVideoProps> = ({
onAutoPlayError,
onError,
onVideoFrame,
+ onVideoFrameCallback,
pauseWhenBuffering,
playbackRate,
preservePitch,
@@ -190,6 +193,7 @@ export const OffthreadVideo: React.FC<RemotionOffthreadVideoProps> = ({
onAutoPlayError={onAutoPlayError ?? null}
onError={onError}
onVideoFrame={onVideoFrame}
+ onVideoFrameCallback={onVideoFrameCallback}
pauseWhenBuffering={pauseWhenBuffering ?? true}
playbackRate={playbackRate ?? 1}
preservePitch={preservePitch}
diff --git a/packages/core/src/video/VideoForPreview.tsx b/packages/core/src/video/VideoForPreview.tsx
index a4d55dec9a..f72cce5010 100644
--- a/packages/core/src/video/VideoForPreview.tsx
+++ b/packages/core/src/video/VideoForPreview.tsx
@@ -1,12 +1,12 @@
import React, {
forwardRef,
+ useCallback,
useContext,
useEffect,
useImperativeHandle,
useMemo,
useRef,
useState,
- useCallback,
} from 'react';
import type {IsExact} from '../audio/props.js';
import {SharedAudioContext} from '../audio/shared-audio-tags.js';
@@ -31,10 +31,15 @@ import {evaluateVolume} from '../volume-prop.js';
import {warnAboutTooHighVolume} from '../volume-safeguard.js';
import {useEmitVideoFrame} from './emit-video-frame.js';
import {MediaPlaybackError} from './MediaPlaybackError.js';
-import type {NativeVideoProps, OnVideoFrame, RemotionVideoProps} from './props';
+import type {
+ NativeVideoProps,
+ OnVideoFrame,
+ OnVideoFrameCallback,
+ RemotionVideoProps,
+} from './props';
import {isIosSafari, useAppendVideoFragment} from './video-fragment.js';
-type VideoForPreviewProps = RemotionVideoProps & {
+type VideoForPreviewProps = Omit<RemotionVideoProps, 'onVideoFrameCallback'> & {
readonly onlyWarnForMediaSeekingError: boolean;
readonly onDuration: (src: string, durationInSeconds: number) => void;
readonly pauseWhenBuffering: boolean;
@@ -42,6 +47,7 @@ type VideoForPreviewProps = RemotionVideoProps & {
readonly _remotionInternalStack: string | null;
readonly showInTimeline: boolean;
readonly onVideoFrame: null | OnVideoFrame;
+ readonly onVideoFrameCallback: null | OnVideoFrameCallback;
readonly crossOrigin?: '' | 'anonymous' | 'use-credentials';
};
@@ -114,6 +120,7 @@ const VideoForDevelopmentRefForwardingFunction: React.ForwardRefRenderFunction<
onError,
onAutoPlayError,
onVideoFrame,
+ onVideoFrameCallback,
crossOrigin,
delayRenderRetries,
delayRenderTimeoutInMilliseconds,
@@ -297,7 +304,7 @@ const VideoForDevelopmentRefForwardingFunction: React.ForwardRefRenderFunction<
useRef<VideoForPreviewProps['onDuration']>(onDuration);
currentOnDurationCallback.current = onDuration;
- useEmitVideoFrame({ref: videoRef, onVideoFrame});
+ useEmitVideoFrame({ref: videoRef, onVideoFrame, onVideoFrameCallback});
useEffect(() => {
const {current} = videoRef;
@@ -349,7 +356,7 @@ const VideoForDevelopmentRefForwardingFunction: React.ForwardRefRenderFunction<
const crossOriginValue = getCrossOriginValue({
crossOrigin,
- requestsVideoFrame: Boolean(onVideoFrame),
+ requestsVideoFrame: Boolean(onVideoFrame || onVideoFrameCallback),
isClientSideRendering: false,
});
diff --git a/packages/core/src/video/VideoForRendering.tsx b/packages/core/src/video/VideoForRendering.tsx
index be976aaef7..9c98c91143 100644
--- a/packages/core/src/video/VideoForRendering.tsx
+++ b/packages/core/src/video/VideoForRendering.tsx
@@ -25,14 +25,23 @@ import {useRemotionEnvironment} from '../use-remotion-environment.js';
import {useUnsafeVideoConfig} from '../use-unsafe-video-config.js';
import {evaluateVolume} from '../volume-prop.js';
import {warnAboutTooHighVolume} from '../volume-safeguard.js';
+import {useEmitVideoFrame} from './emit-video-frame.js';
import {getMediaTime} from './get-current-time.js';
import {MediaPlaybackError} from './MediaPlaybackError.js';
-import type {OnVideoFrame, RemotionVideoProps} from './props';
+import type {
+ OnVideoFrame,
+ OnVideoFrameCallback,
+ RemotionVideoProps,
+} from './props';
import {seekToTimeMultipleUntilRight} from './seek-until-right.js';
-type VideoForRenderingProps = RemotionVideoProps & {
+type VideoForRenderingProps = Omit<
+ RemotionVideoProps,
+ 'onVideoFrameCallback'
+> & {
readonly onDuration: (src: string, durationInSeconds: number) => void;
readonly onVideoFrame: null | OnVideoFrame;
+ readonly onVideoFrameCallback: null | OnVideoFrameCallback;
};
const VideoForRenderingForwardFunction: React.ForwardRefRenderFunction<
@@ -53,6 +62,7 @@ const VideoForRenderingForwardFunction: React.ForwardRefRenderFunction<
loopVolumeCurveBehavior,
audioStreamIndex,
onVideoFrame,
+ onVideoFrameCallback,
preservePitch: _preservePitch,
...props
},
@@ -156,6 +166,8 @@ const VideoForRenderingForwardFunction: React.ForwardRefRenderFunction<
return videoRef.current as HTMLVideoElement;
}, []);
+ useEmitVideoFrame({ref: videoRef, onVideoFrame, onVideoFrameCallback});
+
useEffect(() => {
if (!window.remotion_videoEnabled) {
return;
diff --git a/packages/core/src/video/emit-video-frame.ts b/packages/core/src/video/emit-video-frame.ts
index 87d91c3160..44568b2898 100644
--- a/packages/core/src/video/emit-video-frame.ts
+++ b/packages/core/src/video/emit-video-frame.ts
@@ -1,12 +1,14 @@
import {useEffect} from 'react';
-import type {OnVideoFrame} from './props';
+import type {OnVideoFrame, OnVideoFrameCallback} from './props';
export const useEmitVideoFrame = ({
ref,
onVideoFrame,
+ onVideoFrameCallback,
}: {
ref: React.RefObject<HTMLVideoElement | null>;
onVideoFrame: OnVideoFrame | null;
+ onVideoFrameCallback: OnVideoFrameCallback | null;
}) => {
useEffect(() => {
const {current} = ref;
@@ -14,24 +16,32 @@ export const useEmitVideoFrame = ({
return;
}
- if (!onVideoFrame) {
+ if (!onVideoFrame && !onVideoFrameCallback) {
return;
}
let handle = 0;
- const callback = () => {
+ const callback: VideoFrameRequestCallback = (_now, metadata) => {
if (!ref.current) {
return;
}
- onVideoFrame(ref.current);
+ onVideoFrame?.(ref.current);
+ onVideoFrameCallback?.(_now, metadata);
handle = ref.current.requestVideoFrameCallback(callback);
};
- callback();
+ onVideoFrame?.(current);
+ if (!current.requestVideoFrameCallback) {
+ return;
+ }
+
+ handle = current.requestVideoFrameCallback(callback);
return () => {
- current.cancelVideoFrameCallback(handle);
+ if (handle) {
+ current.cancelVideoFrameCallback(handle);
+ }
};
- }, [onVideoFrame, ref]);
+ }, [onVideoFrame, onVideoFrameCallback, ref]);
};
diff --git a/packages/core/src/video/html5-video.tsx b/packages/core/src/video/html5-video.tsx
index 398dd8a583..04e2c41317 100644
--- a/packages/core/src/video/html5-video.tsx
+++ b/packages/core/src/video/html5-video.tsx
@@ -39,6 +39,7 @@ const VideoForwardingFunction: React.ForwardRefRenderFunction<
_remotionInternalNativeLoopPassed,
showInTimeline,
onAutoPlayError,
+ onVideoFrameCallback,
...otherProps
} = props;
const {loop, ...propsOtherThanLoop} = props;
@@ -142,6 +143,7 @@ const VideoForwardingFunction: React.ForwardRefRenderFunction<
>
<Html5Video
pauseWhenBuffering={pauseWhenBuffering ?? false}
+ onVideoFrameCallback={onVideoFrameCallback}
{...otherProps}
ref={ref}
stack={stack}
@@ -164,6 +166,7 @@ const VideoForwardingFunction: React.ForwardRefRenderFunction<
<VideoForRendering
onDuration={onDuration}
onVideoFrame={onVideoFrame ?? null}
+ onVideoFrameCallback={onVideoFrameCallback ?? null}
{...otherProps}
ref={ref}
/>
@@ -176,6 +179,7 @@ const VideoForwardingFunction: React.ForwardRefRenderFunction<
{...otherProps}
ref={ref}
onVideoFrame={null}
+ onVideoFrameCallback={onVideoFrameCallback ?? null}
// Proposal: Make this default to true in v5
pauseWhenBuffering={pauseWhenBuffering ?? false}
onDuration={onDuration}
diff --git a/packages/core/src/video/props.ts b/packages/core/src/video/props.ts
index de389e0e9d..80204627ba 100644
--- a/packages/core/src/video/props.ts
+++ b/packages/core/src/video/props.ts
@@ -56,6 +56,7 @@ export type RemotionVideoProps = NativeVideoProps & {
delayRenderRetries?: number;
onError?: (err: Error) => void;
onAutoPlayError?: null | (() => void);
+ onVideoFrameCallback?: OnVideoFrameCallback;
audioStreamIndex?: number;
};
@@ -97,6 +98,7 @@ type OptionalOffthreadVideoProps = {
showInTimeline: boolean;
onAutoPlayError: null | (() => void);
onVideoFrame: OnVideoFrame | undefined;
+ onVideoFrameCallback: OnVideoFrameCallback | undefined;
crossOrigin: '' | 'anonymous' | 'use-credentials' | undefined;
audioStreamIndex: number;
};
@@ -111,3 +113,7 @@ export type RemotionOffthreadVideoProps = MandatoryOffthreadVideoProps &
Partial<DeprecatedOffthreadVideoProps>;
export type OnVideoFrame = (frame: CanvasImageSource) => void;
+export type OnVideoFrameCallback = (
+ now: DOMHighResTimeStamp,
+ metadata: VideoFrameCallbackMetadata,
+) => void;
diff --git a/packages/docs/docs/html5-video.mdx b/packages/docs/docs/html5-video.mdx
index bc428eb4d3..3ad1aef8da 100644
--- a/packages/docs/docs/html5-video.mdx
+++ b/packages/docs/docs/html5-video.mdx
@@ -293,6 +293,11 @@ If you don't pass a callback, the video will be muted and be retried once.
This prop is useful if you want to handle the error yourself, e.g. for pausing the Player.
Read more here about [autoplay restrictions](/docs/player/autoplay).
+### `onVideoFrameCallback?`<AvailableFrom v="4.0.472" />
+
+A callback function that gets called when the browser reports a presented video frame using [`HTMLVideoElement.requestVideoFrameCallback()`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLVideoElement/requestVideoFrameCallback).
+The callback has the same signature as `requestVideoFrameCallback()`: It receives a `DOMHighResTimeStamp` and the [`VideoFrameCallbackMetadata`](https://developer.mozilla.org/en-US/docs/Web/API/VideoFrameCallbackMetadata).
+
### `crossOrigin?`<AvailableFrom v="4.0.190" />
Corresponds to the [`crossOrigin`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/video#attr-crossorigin) attribute of the `<video>` element.
diff --git a/packages/docs/docs/media/video.mdx b/packages/docs/docs/media/video.mdx
index 7b03019df2..f6c4e1c88c 100644
--- a/packages/docs/docs/media/video.mdx
+++ b/packages/docs/docs/media/video.mdx
@@ -455,6 +455,11 @@ Maps to [`<OffthreadVideo />` -> `onAutoPlayError`](/docs/offthreadvideo#onautop
Maps to [`<OffthreadVideo />` -> `preservePitch`](/docs/offthreadvideo#preservepitch).
Only affects preview playback when falling back to [`<Html5Video>`](/docs/html5-video) or [`<OffthreadVideo>`](/docs/offthreadvideo).
+#### `onVideoFrameCallback?`<AvailableFrom v="4.0.472" />
+
+Maps to [`<OffthreadVideo />` -> `onVideoFrameCallback`](/docs/offthreadvideo#onvideoframecallback).
+Only affects preview playback when falling back to [`<OffthreadVideo>`](/docs/offthreadvideo).
+
### `disallowFallbackToOffthreadVideo?`
By default, if the video cannot be embedded using this tag, [a fallback to `<OffthreadVideo>`](/docs/media/fallback) will be attempted.
diff --git a/packages/docs/docs/offthreadvideo.mdx b/packages/docs/docs/offthreadvideo.mdx
index d6f4a8a54c..d030ef9e2b 100644
--- a/packages/docs/docs/offthreadvideo.mdx
+++ b/packages/docs/docs/offthreadvideo.mdx
@@ -294,6 +294,13 @@ Useful for [video manipulation](/docs/video-manipulation).
The callback is called with a [`CanvasImageSource`](https://developer.mozilla.org/en-US/docs/Web/API/CanvasImageSource) object.
During preview, this is a `HTMLVideoElement` object, during rendering, it is an `HTMLImageElement`.
+### `onVideoFrameCallback?`<AvailableFrom v="4.0.472" />
+
+A callback function that gets called during preview when the browser reports a presented video frame using [`HTMLVideoElement.requestVideoFrameCallback()`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLVideoElement/requestVideoFrameCallback).
+The callback has the same signature as `requestVideoFrameCallback()`: It receives a `DOMHighResTimeStamp` and the [`VideoFrameCallbackMetadata`](https://developer.mozilla.org/en-US/docs/Web/API/VideoFrameCallbackMetadata).
+
+This callback only fires during preview, since rendering uses image extraction instead of a browser video element.
+
### `crossOrigin?`<AvailableFrom v="4.0.190" />
Corresponds to the [`crossOrigin`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/video#attr-crossorigin) attribute of the `<video>` element.
diff --git a/packages/media/src/video/props.ts b/packages/media/src/video/props.ts
index d4fe2ba3c6..24492cc777 100644
--- a/packages/media/src/video/props.ts
+++ b/packages/media/src/video/props.ts
@@ -4,6 +4,7 @@ import type {
LogLevel,
LoopVolumeCurveBehavior,
OnVideoFrame,
+ OnVideoFrameCallback,
SequenceProps,
VolumeProp,
} from 'remotion';
@@ -31,6 +32,7 @@ export type FallbackOffthreadVideoProps = {
pauseWhenBuffering?: boolean;
onAutoPlayError?: null | (() => void);
preservePitch?: boolean;
+ onVideoFrameCallback?: OnVideoFrameCallback;
};
type MandatoryVideoProps = {
diff --git a/packages/media/src/video/video-for-rendering.tsx b/packages/media/src/video/video-for-rendering.tsx
index a947b3a3b1..997797492e 100644
--- a/packages/media/src/video/video-for-rendering.tsx
+++ b/packages/media/src/video/video-for-rendering.tsx
@@ -460,6 +460,7 @@ export const VideoForRendering: React.FC<InnerVideoProps> = ({
audioStreamIndex={audioStreamIndex ?? 0}
className={className}
onVideoFrame={onVideoFrame}
+ onVideoFrameCallback={fallbackOffthreadVideoProps?.onVideoFrameCallback}
volume={volumeProp}
id={id}
onError={fallbackOffthreadVideoProps?.onError}