Skip to content

🎥 Make videos programmatically with React

fix refs

Jonny Burger1c56ded61bef
diff --git a/packages/core/src/video/Video.tsx b/packages/core/src/video/Video.tsx
index 6801d0fce7..e7838a4c97 100644
--- a/packages/core/src/video/Video.tsx
+++ b/packages/core/src/video/Video.tsx
@@ -1,4 +1,4 @@
-import React from 'react';
+import React, {forwardRef} from 'react';
import {Sequence} from '../sequencing';
import {validateMediaProps} from '../validate-media-props';
import {validateStartFromProps} from '../validate-start-from-props';
@@ -6,10 +6,16 @@ import {RemotionMainVideoProps, RemotionVideoProps} from './props';
import {VideoForDevelopment} from './VideoForDevelopment';
import {VideoForRendering} from './VideoForRendering';
-export const Video: React.FC<RemotionVideoProps & RemotionMainVideoProps> = (
- props
-) => {
+const VideoForwardingFunction: React.ForwardRefRenderFunction<
+ HTMLVideoElement,
+ RemotionVideoProps & RemotionMainVideoProps
+> = (props, ref) => {
const {startFrom, endAt, ...otherProps} = props;
+
+ if (typeof ref === 'string') {
+ throw new Error('string refs are not supported');
+ }
+
if (typeof startFrom !== 'undefined' || typeof endAt !== 'undefined') {
validateStartFromProps(startFrom, endAt);
@@ -22,14 +28,16 @@ export const Video: React.FC<RemotionVideoProps & RemotionMainVideoProps> = (
showInTimeline={false}
durationInFrames={endAtFrameNo}
>
- <Video {...otherProps} />
+ <Video {...otherProps} ref={ref} />
</Sequence>
);
}
validateMediaProps(props, 'Video');
if (process.env.NODE_ENV === 'development') {
- return <VideoForDevelopment {...props} />;
+ return <VideoForDevelopment {...props} ref={ref} />;
}
- return <VideoForRendering {...props} />;
+ return <VideoForRendering {...props} ref={ref} />;
};
+
+export const Video = forwardRef(VideoForwardingFunction);
diff --git a/packages/example/src/RemoteVideo/index.tsx b/packages/example/src/RemoteVideo/index.tsx
index d36ec277b7..c54840cfc4 100644
--- a/packages/example/src/RemoteVideo/index.tsx
+++ b/packages/example/src/RemoteVideo/index.tsx
@@ -1,9 +1,13 @@
+import {useRef} from 'react';
import {interpolate, useCurrentFrame, Video} from 'remotion';
const RemoteVideo: React.FC = () => {
const frame = useCurrentFrame();
+ const ref = useRef<HTMLVideoElement>(null);
+
return (
<Video
+ ref={ref}
volume={interpolate(frame, [0, 500], [1, 0], {extrapolateRight: 'clamp'})}
src="http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"
/>