🎥 Make videos programmatically with React
import React, {useMemo, useState} from 'react';
import {
Freeze,
Internals,
Interactive,
Sequence,
useRemotionEnvironment,
useVideoConfig,
type SequenceControls,
type InteractivitySchema,
} from 'remotion';
import {NoReactInternals} from 'remotion/no-react';
import {getLoopDisplay} from '../show-in-timeline';
import {validateToneFrequency} from '../validate-tone-frequency';
import {
getMediaTrimAfter,
getVideoSequenceDuration,
} from './get-video-sequence-duration';
import type {InnerVideoProps, VideoProps} from './props';
import {VideoForPreview} from './video-for-preview';
import {VideoForRendering} from './video-for-rendering';
const {
validateMediaTrimProps,
resolveTrimProps,
validateMediaProps,
useCropStyle,
} = Internals;
export const videoSchema: InteractivitySchema = {
src: {
type: 'asset',
assetType: 'video',
default: undefined,
description: 'Source',
keyframable: false,
},
...Internals.baseSchema,
trimAfter: Internals.trimAfterField,
...Internals.premountSchema,
volume: {
type: 'number',
min: 0,
max: 20,
step: 0.01,
default: 1,
description: 'Volume',
hiddenFromList: false,
},
playbackRate: {
type: 'number',
min: 0.1,
step: 0.01,
default: 1,
description: 'Playback rate',
hiddenFromList: false,
keyframable: false,
},
preservePitch: {
type: 'boolean',
default: false,
description: 'Preserve pitch',
keyframable: false,
},
toneFrequency: {
type: 'number',
min: 0.01,
max: 2,
step: 0.01,
default: 1,
description: 'Pitch',
hiddenFromList: false,
keyframable: false,
},
muted: {type: 'boolean', default: false, description: 'Muted'},
loop: {type: 'boolean', default: false, description: 'Loop'},
...Internals.transformSchema,
...Interactive.backgroundSchema,
...Interactive.borderSchema,
...Interactive.borderRadiusSchema,
...Interactive.cropSchema,
} as const satisfies InteractivitySchema;
const InnerVideo: React.FC<
InnerVideoProps & {
readonly controls: SequenceControls | undefined;
readonly setMediaDurationInSeconds: (durationInSeconds: number) => void;
}
> = ({
src,
audioStreamIndex,
className,
delayRenderRetries,
delayRenderTimeoutInMilliseconds,
disallowFallbackToOffthreadVideo,
fallbackOffthreadVideoProps,
logLevel,
loop,
loopVolumeCurveBehavior,
muted,
onVideoFrame,
playbackRate,
style,
trimAfter,
trimBefore,
volume,
_remotionInternalStack,
toneFrequency,
preservePitch,
showInTimeline,
debugOverlay,
headless,
onError,
credentials,
requestInit,
controls,
objectFit,
maxCanvasSinkFrameSize,
_experimentalInitiallyDrawCachedFrame,
effects,
effectsOutputSize = null,
setMediaDurationInSeconds,
...props
}) => {
const environment = useRemotionEnvironment();
if (typeof src !== 'string') {
throw new TypeError(
`The \`<Video>\` tag requires a string for \`src\`, but got ${JSON.stringify(
src,
)} instead.`,
);
}
validateMediaTrimProps({
startFrom: undefined,
endAt: undefined,
trimBefore,
trimAfter,
});
const {trimBeforeValue, trimAfterValue} = resolveTrimProps({
startFrom: undefined,
endAt: undefined,
trimBefore,
trimAfter,
});
validateToneFrequency({toneFrequency, component: 'Video'});
if (environment.isRendering) {
return (
<VideoForRendering
{...props}
audioStreamIndex={audioStreamIndex ?? null}
className={className}
delayRenderRetries={delayRenderRetries ?? null}
delayRenderTimeoutInMilliseconds={
delayRenderTimeoutInMilliseconds ?? null
}
disallowFallbackToOffthreadVideo={
disallowFallbackToOffthreadVideo ?? false
}
fallbackOffthreadVideoProps={fallbackOffthreadVideoProps}
logLevel={logLevel}
loop={loop}
loopVolumeCurveBehavior={loopVolumeCurveBehavior}
muted={muted}
onVideoFrame={onVideoFrame}
playbackRate={playbackRate}
src={src}
_remotionInternalStack={_remotionInternalStack}
style={style}
volume={volume}
toneFrequency={toneFrequency}
preservePitch={preservePitch}
trimAfterValue={trimAfterValue}
trimBeforeValue={trimBeforeValue}
headless={headless}
onError={onError}
credentials={credentials}
requestInit={requestInit}
objectFit={objectFit}
effects={effects}
effectsOutputSize={effectsOutputSize}
/>
);
}
if (maxCanvasSinkFrameSize?.width !== undefined) {
NoReactInternals.validateDimension(
maxCanvasSinkFrameSize.width,
'maxCanvasSinkFrameSize.width',
'of the <Video /> component',
);
}
if (maxCanvasSinkFrameSize?.height !== undefined) {
NoReactInternals.validateDimension(
maxCanvasSinkFrameSize.height,
'maxCanvasSinkFrameSize.height',
'of the <Video /> component',
);
}
return (
<VideoForPreview
{...props}
setMediaDurationInSeconds={setMediaDurationInSeconds}
audioStreamIndex={audioStreamIndex ?? null}
className={className}
logLevel={logLevel}
loop={loop}
loopVolumeCurveBehavior={loopVolumeCurveBehavior}
muted={muted}
onVideoFrame={onVideoFrame}
playbackRate={playbackRate}
toneFrequency={toneFrequency ?? 1}
preservePitch={preservePitch}
src={src}
style={style}
volume={volume}
showInTimeline={showInTimeline}
trimAfter={trimAfterValue}
trimBefore={trimBeforeValue}
_remotionInternalStack={_remotionInternalStack ?? null}
disallowFallbackToOffthreadVideo={disallowFallbackToOffthreadVideo}
fallbackOffthreadVideoProps={fallbackOffthreadVideoProps}
debugOverlay={debugOverlay ?? false}
headless={headless ?? false}
onError={onError}
credentials={credentials}
requestInit={requestInit}
controls={controls}
objectFit={objectFit}
maxCanvasSinkFrameSize={maxCanvasSinkFrameSize}
effects={effects}
effectsOutputSize={effectsOutputSize}
_experimentalInitiallyDrawCachedFrame={
_experimentalInitiallyDrawCachedFrame
}
/>
);
};
const VideoInner: React.FC<
VideoProps & {
readonly controls: SequenceControls | undefined;
}
> = ({
src,
audioStreamIndex,
className,
delayRenderRetries,
delayRenderTimeoutInMilliseconds,
disallowFallbackToOffthreadVideo,
fallbackOffthreadVideoProps,
logLevel,
loop,
loopVolumeCurveBehavior,
muted,
name,
onVideoFrame,
playbackRate,
showInTimeline,
style,
trimAfter,
trimBefore,
volume,
toneFrequency,
preservePitch,
debugOverlay,
headless,
onError,
credentials,
requestInit,
controls,
objectFit,
maxCanvasSinkFrameSize,
_experimentalInitiallyDrawCachedFrame,
effects,
effectsOutputSize = null,
durationInFrames,
from,
freeze,
hidden,
premountFor,
postmountFor,
styleWhilePremounted,
styleWhilePostmounted,
cropLeft,
cropRight,
cropTop,
cropBottom,
...props
}) => {
validateMediaProps({playbackRate, preservePitch, volume}, 'Video');
const sourceStack = controls
? (Internals.getStackForControls(controls) ?? undefined)
: undefined;
const fallbackLogLevel = Internals.useLogLevel();
const mediaStartsAt = Internals.useMediaStartsAt();
const videoConfig = useVideoConfig();
const effectiveTrimAfter = getMediaTrimAfter({
durationInFrames,
trimAfter,
trimBefore,
});
const videoSequenceDuration = getVideoSequenceDuration({
durationInFrames,
loop: loop ?? false,
playbackRate: playbackRate ?? 1,
trimAfter: effectiveTrimAfter,
trimBefore,
});
const sequenceDurationInFrames = Math.min(
videoSequenceDuration ?? Infinity,
Math.max(0, videoConfig.durationInFrames - (from ?? 0)),
);
const [mediaDurationInSeconds, setMediaDurationInSeconds] = useState<
number | null
>(null);
const basicInfo = Internals.useBasicMediaInTimeline({
src,
playbackRate: playbackRate ?? 1,
trimBefore,
trimAfter: effectiveTrimAfter,
sequenceDurationInFrames,
mediaStartsAt,
displayName: name ?? '<Video>',
loop: loop ?? false,
muted: muted ?? false,
});
const loopDisplay = useMemo(
() =>
getLoopDisplay({
loop: loop ?? false,
mediaDurationInSeconds,
playbackRate: playbackRate ?? 1,
trimAfter: effectiveTrimAfter,
trimBefore,
sequenceDurationInFrames,
compFps: videoConfig.fps,
}),
[
loop,
mediaDurationInSeconds,
playbackRate,
effectiveTrimAfter,
trimBefore,
sequenceDurationInFrames,
videoConfig.fps,
],
);
const isMedia = useMemo(
() => ({
type: 'video' as const,
data: basicInfo,
}),
[basicInfo],
);
const memoizedEffects = Internals.useMemoizedEffects({
effects: effects ?? [],
overrideId: controls?.overrideId ?? null,
videoConfigValues: controls?.videoConfigValues ?? null,
});
const memoizedEffectDefinitions = Internals.useMemoizedEffectDefinitions(
effects ?? [],
);
const {
effectivePostmountFor,
effectivePremountFor,
freezeFrame,
isPremountingOrPostmounting,
postmountingActive,
premountingActive,
premountingStyle,
} = Internals.usePremounting({
from: from ?? 0,
durationInFrames: videoSequenceDuration ?? Infinity,
premountFor: premountFor ?? null,
postmountFor: postmountFor ?? null,
style: style ?? null,
styleWhilePremounted: styleWhilePremounted ?? null,
styleWhilePostmounted: styleWhilePostmounted ?? null,
hideWhilePremounted: 'display-none',
});
const croppedStyle = useCropStyle({
cropLeft,
cropRight,
cropTop,
cropBottom,
style: premountingStyle,
componentName: '<Video />',
});
if (sequenceDurationInFrames === 0) {
return null;
}
return (
<Freeze
frame={freezeFrame}
active={isPremountingOrPostmounting}
_remotionInternalIsPremounting={premountingActive}
>
<Sequence
layout="none"
from={from ?? 0}
durationInFrames={videoSequenceDuration}
freeze={freeze}
_remotionInternalIsMedia={isMedia}
_remotionInternalPremountDisplay={effectivePremountFor || null}
_remotionInternalPostmountDisplay={effectivePostmountFor || null}
_remotionInternalIsPremounting={premountingActive}
_remotionInternalIsPostmounting={postmountingActive}
name={name ?? '<Video>'}
_remotionInternalDocumentationLink="https://www.remotion.dev/docs/media/video"
controls={controls}
_remotionInternalLoopDisplay={loopDisplay}
_remotionInternalEffects={memoizedEffectDefinitions}
showInTimeline={showInTimeline ?? true}
hidden={hidden}
>
<Internals.SequenceContent>
<InnerVideo
{...props}
audioStreamIndex={audioStreamIndex ?? null}
className={className}
delayRenderRetries={delayRenderRetries ?? null}
delayRenderTimeoutInMilliseconds={
delayRenderTimeoutInMilliseconds ?? null
}
disallowFallbackToOffthreadVideo={
disallowFallbackToOffthreadVideo ?? false
}
fallbackOffthreadVideoProps={fallbackOffthreadVideoProps ?? {}}
logLevel={logLevel ?? fallbackLogLevel}
loop={loop ?? false}
loopVolumeCurveBehavior={loopVolumeCurveBehavior ?? 'repeat'}
muted={muted ?? false}
onVideoFrame={onVideoFrame}
playbackRate={playbackRate ?? 1}
// The enclosing Sequence already represents the video in the timeline.
showInTimeline={false}
src={src}
style={croppedStyle ?? {}}
trimAfter={effectiveTrimAfter}
trimBefore={trimBefore}
volume={volume ?? 1}
toneFrequency={toneFrequency ?? 1}
preservePitch={preservePitch ?? null}
_remotionInternalStack={sourceStack}
debugOverlay={debugOverlay ?? false}
headless={headless ?? false}
onError={onError}
credentials={credentials}
requestInit={requestInit}
controls={controls}
objectFit={objectFit ?? 'contain'}
maxCanvasSinkFrameSize={maxCanvasSinkFrameSize ?? null}
_experimentalInitiallyDrawCachedFrame={
_experimentalInitiallyDrawCachedFrame ?? false
}
effects={memoizedEffects}
effectsOutputSize={effectsOutputSize}
setMediaDurationInSeconds={setMediaDurationInSeconds}
/>
</Internals.SequenceContent>
</Sequence>
</Freeze>
);
};
export const Video = Interactive.withSchema({
Component: VideoInner,
componentName: '<Video>',
componentIdentity: 'dev.remotion.media.Video',
schema: videoSchema,
supportsEffects: true,
});