Skip to content

🎥 Make videos programmatically with React

loop prop for video preview inspired by original <Video>

Igor Samokhovetsf6cae6774531
diff --git a/packages/media/src/video/props.ts b/packages/media/src/video/props.ts
index 9569ee8963..dadc8b95e4 100644
--- a/packages/media/src/video/props.ts
+++ b/packages/media/src/video/props.ts
@@ -35,5 +35,9 @@ export type VideoProps = {
stack?: string;
logLevel?: LogLevel;
loop?: boolean;
+ /**
+ * @deprecated For internal use only
+ */
+ _remotionInternalNativeLoopPassed?: boolean;
fallbackOffthreadVideoProps?: FallbackOffthreadVideoProps;
};
diff --git a/packages/media/src/video/video-for-preview.tsx b/packages/media/src/video/video-for-preview.tsx
index d3af45f18c..0e463b1408 100644
--- a/packages/media/src/video/video-for-preview.tsx
+++ b/packages/media/src/video/video-for-preview.tsx
@@ -1,7 +1,15 @@
+import {ALL_FORMATS, Input, UrlSource} from 'mediabunny';
import React, {useContext, useEffect, useMemo, useRef, useState} from 'react';
import type {LogLevel, LoopVolumeCurveBehavior, VolumeProp} from 'remotion';
-import {Internals, useBufferState, useCurrentFrame} from 'remotion';
+import {
+ Internals,
+ Loop,
+ useBufferState,
+ useCurrentFrame,
+ useVideoConfig,
+} from 'remotion';
import {MediaPlayer} from './media-player';
+import type {VideoProps} from './props';
const {
useUnsafeVideoConfig,
@@ -15,6 +23,32 @@ const {
usePreload,
} = Internals;
+const calculateLoopDuration = ({
+ endAt,
+ mediaDuration,
+ playbackRate,
+ startFrom,
+}: {
+ mediaDuration: number;
+ playbackRate: number;
+ startFrom: number | undefined;
+ endAt: number | undefined;
+}) => {
+ let duration = mediaDuration;
+
+ if (typeof endAt !== 'undefined') {
+ duration = endAt;
+ }
+
+ if (typeof startFrom !== 'undefined') {
+ duration -= startFrom;
+ }
+
+ const actualDuration = duration / playbackRate;
+
+ return Math.floor(actualDuration);
+};
+
type NewVideoForPreviewProps = {
readonly src: string;
readonly style?: React.CSSProperties;
@@ -25,9 +59,10 @@ type NewVideoForPreviewProps = {
readonly volume?: VolumeProp;
readonly loopVolumeCurveBehavior?: LoopVolumeCurveBehavior;
readonly onVideoFrame?: (frame: CanvasImageSource) => void;
+ readonly _remotionInternalNativeLoopPassed?: boolean;
};
-export const NewVideoForPreview: React.FC<NewVideoForPreviewProps> = ({
+const NewVideoForPreview: React.FC<NewVideoForPreviewProps> = ({
src,
style,
playbackRate = 1,
@@ -146,7 +181,6 @@ export const NewVideoForPreview: React.FC<NewVideoForPreviewProps> = ({
.join(' ');
}, [className]);
- // sync play/pause state with Remotion timeline (like old VideoForPreview video does)
useEffect(() => {
const mediaPlayer = mediaPlayerRef.current;
if (!mediaPlayer) return;
@@ -164,7 +198,6 @@ export const NewVideoForPreview: React.FC<NewVideoForPreviewProps> = ({
}
}, [playing, logLevel, mediaPlayerReady]);
- // sync target time with MediaPlayer
useEffect(() => {
const mediaPlayer = mediaPlayerRef.current;
if (!mediaPlayer || !mediaPlayerReady) return;
@@ -176,21 +209,18 @@ export const NewVideoForPreview: React.FC<NewVideoForPreviewProps> = ({
);
}, [currentTime, logLevel, mediaPlayerReady]);
- // sync MediaPlayer buffering with Remotion buffering
useEffect(() => {
const mediaPlayer = mediaPlayerRef.current;
if (!mediaPlayer || !mediaPlayerReady) return;
mediaPlayer.onBufferingChange((newBufferingState) => {
if (newBufferingState && !delayHandleRef.current) {
- // Start blocking Remotion playback
delayHandleRef.current = buffer.delayPlayback();
Internals.Log.trace(
{logLevel, tag: '@remotion/media'},
'[NewVideoForPreview] MediaPlayer buffering - blocking Remotion playback',
);
} else if (!newBufferingState && delayHandleRef.current) {
- // Unblock Remotion playback
delayHandleRef.current.unblock();
delayHandleRef.current = null;
Internals.Log.trace(
@@ -203,7 +233,6 @@ export const NewVideoForPreview: React.FC<NewVideoForPreviewProps> = ({
const effectiveMuted = muted || mediaMuted || userPreferredVolume <= 0;
- // sync muted state with MediaPlayer
useEffect(() => {
const mediaPlayer = mediaPlayerRef.current;
if (!mediaPlayer || !mediaPlayerReady) return;
@@ -211,7 +240,6 @@ export const NewVideoForPreview: React.FC<NewVideoForPreviewProps> = ({
mediaPlayer.setMuted(effectiveMuted);
}, [effectiveMuted, mediaPlayerReady]);
- // sync volume with MediaPlayer
useEffect(() => {
const mediaPlayer = mediaPlayerRef.current;
if (!mediaPlayer || !mediaPlayerReady) {
@@ -226,7 +254,6 @@ export const NewVideoForPreview: React.FC<NewVideoForPreviewProps> = ({
[playbackRate, globalPlaybackRate],
);
- // sync playbackRate with MediaPlayer (like HTML5 video does)
useEffect(() => {
const mediaPlayer = mediaPlayerRef.current;
if (!mediaPlayer || !mediaPlayerReady) {
@@ -242,7 +269,6 @@ export const NewVideoForPreview: React.FC<NewVideoForPreviewProps> = ({
});
}, [effectivePlaybackRate, mediaPlayerReady, logLevel]);
- // sync onVideoFrame callback with MediaPlayer
useEffect(() => {
const mediaPlayer = mediaPlayerRef.current;
if (!mediaPlayer || !mediaPlayerReady) {
@@ -264,3 +290,122 @@ export const NewVideoForPreview: React.FC<NewVideoForPreviewProps> = ({
/>
);
};
+
+interface VideoForPreviewWithDurationProps extends VideoProps {
+ readonly durationInSeconds: number;
+}
+
+const VideoForPreviewWithDuration: React.FC<
+ VideoForPreviewWithDurationProps
+> = (props) => {
+ const {name, durationInSeconds, loop, trimBefore, trimAfter, ...otherProps} =
+ props;
+ const {fps} = useVideoConfig();
+
+ if (loop) {
+ if (!Number.isFinite(durationInSeconds)) {
+ return (
+ <VideoForPreviewWithDuration
+ {...props}
+ _remotionInternalNativeLoopPassed
+ loop={false}
+ />
+ );
+ }
+
+ const mediaDuration = durationInSeconds * fps;
+
+ return (
+ <Loop
+ durationInFrames={calculateLoopDuration({
+ endAt: trimAfter,
+ mediaDuration,
+ playbackRate: props.playbackRate ?? 1,
+ startFrom: trimBefore,
+ })}
+ layout="none"
+ name={name}
+ >
+ <VideoForPreviewWithDuration
+ {...props}
+ _remotionInternalNativeLoopPassed
+ loop={false}
+ />
+ </Loop>
+ );
+ }
+
+ return (
+ <NewVideoForPreview
+ src={otherProps.src}
+ style={otherProps.style}
+ playbackRate={otherProps.playbackRate}
+ logLevel={otherProps.logLevel}
+ muted={otherProps.muted}
+ volume={otherProps.volume}
+ loopVolumeCurveBehavior={otherProps.loopVolumeCurveBehavior}
+ onVideoFrame={otherProps.onVideoFrame}
+ _remotionInternalNativeLoopPassed={
+ otherProps._remotionInternalNativeLoopPassed
+ }
+ />
+ );
+};
+
+export const VideoForPreview: React.FC<VideoProps> = (props) => {
+ const {loop, src, logLevel} = props;
+ const preloadedSrc = usePreload(src);
+
+ const [durationInSeconds, setDurationInSeconds] = useState<number | null>(
+ null,
+ );
+
+ useEffect(() => {
+ if (!loop) {
+ return;
+ }
+
+ let cancelled = false;
+
+ const computeDuration = async () => {
+ try {
+ const urlSource = new UrlSource(preloadedSrc);
+ const input = new Input({
+ source: urlSource,
+ formats: ALL_FORMATS,
+ });
+
+ const duration = await input.computeDuration();
+
+ if (!cancelled) {
+ setDurationInSeconds(duration);
+ }
+
+ input.dispose();
+ } catch (error) {
+ Internals.Log.error(
+ {logLevel: logLevel ?? 'info', tag: '@remotion/media'},
+ '[VideoForPreview] Failed to compute duration',
+ error,
+ );
+ }
+ };
+
+ computeDuration();
+
+ return () => {
+ cancelled = true;
+ };
+ }, [loop, preloadedSrc, logLevel]);
+
+ if (loop && durationInSeconds === null) {
+ return null;
+ }
+
+ return (
+ <VideoForPreviewWithDuration
+ {...props}
+ durationInSeconds={durationInSeconds ?? 0}
+ />
+ );
+};
diff --git a/packages/media/src/video/video.tsx b/packages/media/src/video/video.tsx
index 206ebcec0b..b052a3d563 100644
--- a/packages/media/src/video/video.tsx
+++ b/packages/media/src/video/video.tsx
@@ -1,15 +1,13 @@
import React from 'react';
import {Internals, Sequence, useRemotionEnvironment} from 'remotion';
import type {VideoProps} from './props';
-import {NewVideoForPreview} from './video-for-preview';
+import {VideoForPreview} from './video-for-preview';
import {VideoForRendering} from './video-for-rendering';
const {validateMediaTrimProps, resolveTrimProps, validateMediaProps} =
Internals;
export const Video: React.FC<VideoProps> = (props) => {
- // Should only destruct `trimBefore` and `trimAfter` from props,
- // rest gets drilled down
const {trimBefore, trimAfter, name, stack, showInTimeline, ...otherProps} =
props;
const environment = useRemotionEnvironment();
@@ -59,17 +57,5 @@ export const Video: React.FC<VideoProps> = (props) => {
return <VideoForRendering {...otherProps} />;
}
- // For preview, use our new canvas-based component
- return (
- <NewVideoForPreview
- src={otherProps.src}
- style={otherProps.style}
- playbackRate={otherProps.playbackRate}
- logLevel={otherProps.logLevel}
- muted={otherProps.muted}
- volume={otherProps.volume}
- loopVolumeCurveBehavior={otherProps.loopVolumeCurveBehavior}
- onVideoFrame={otherProps.onVideoFrame}
- />
- );
+ return <VideoForPreview {...otherProps} />;
};