🎥 Make videos programmatically with React
import React, {
forwardRef,
useCallback,
useContext,
useEffect,
useImperativeHandle,
useMemo,
useRef,
useState,
} from 'react';
import type {IsExact} from '../audio/props.js';
import {SharedAudioContext} from '../audio/shared-audio-tags.js';
import {makeSharedElementSourceNode} from '../audio/shared-element-source-node.js';
import {useFrameForVolumeProp} from '../audio/use-audio-frame.js';
import {useMediaAudioState} from '../audio/use-media-audio-state.js';
import {
CommittedMetadataProvider,
type CommittedMetadata,
} from '../committed-metadata.js';
import {getCrossOriginValue} from '../get-cross-origin-value.js';
import {useLogLevel, useMountTime} from '../log-level-context.js';
import {playbackLogging} from '../playback-logging.js';
import {usePreload} from '../prefetch.js';
import {SequenceContent} from '../sequence-activity-context.js';
import {SequenceContext} from '../SequenceContext.js';
import {useVolume} from '../use-amplification.js';
import {
scheduleMediaSourceCleanup,
useMediaDuration,
} from '../use-media-duration.js';
import {useMediaInTimelineRegistration} from '../use-media-in-timeline.js';
import {useMediaPlayback} from '../use-media-playback.js';
import {useMediaTag} from '../use-media-tag.js';
import {useRemotionEnvironment} from '../use-remotion-environment.js';
import {useVideoConfig} from '../use-video-config.js';
import {VERSION} from '../version.js';
import {useMediaVolumeState} from '../volume-position-state.js';
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 {isIosSafari, useAppendVideoFragment} from './video-fragment.js';
type VideoForPreviewProps = Omit<RemotionVideoProps, 'onVideoFrame'> & {
readonly onlyWarnForMediaSeekingError: boolean;
readonly onDuration: (src: string, durationInSeconds: number) => void;
readonly pauseWhenBuffering: boolean;
readonly _remotionInternalNativeLoopPassed: boolean;
readonly _remotionInternalStack: string | null;
readonly showInTimeline: boolean;
readonly onVideoFrame: null | OnVideoFrame;
readonly crossOrigin?: '' | 'anonymous' | 'use-credentials';
};
type Expected = Omit<
NativeVideoProps,
'crossOrigin' | 'src' | 'name' | 'muted' | 'style'
>;
const VideoForDevelopmentRefForwardingFunction: React.ForwardRefRenderFunction<
HTMLVideoElement,
VideoForPreviewProps & {
readonly timelineId: string;
readonly userPreferredVolume: number;
readonly isMutedForPlayback: boolean;
}
> = (props, ref) => {
const context = useContext(SharedAudioContext);
if (!context) {
throw new Error('SharedAudioContext not found');
}
const videoRef = useRef<HTMLVideoElement | null>(null);
const sharedSource = useMemo(() => {
if (!context.audioContext) {
return null;
}
return makeSharedElementSourceNode({
audioContext: context.audioContext,
ref: videoRef,
});
}, [context.audioContext]);
/**
* Effects in React 18 fire twice, and we are looking for a way to only fire it once.
* - useInsertionEffect only fires once. If it's available we are in React 18.
* - useLayoutEffect only fires once in React 17.
*
* Need to import it from React to fix React 17 ESM support.
*/
const effectToUse = React.useInsertionEffect ?? React.useLayoutEffect;
// Disconnecting the SharedElementSourceNodes if the tag unmounts to prevent leak.
// https://github.com/remotion-dev/remotion/issues/6285
// But useInsertionEffect will fire before other effects, meaning the
// nodes might still be used. Using rAF to ensure it's after other effects.
effectToUse(() => {
return scheduleMediaSourceCleanup.bind(null, sharedSource?.cleanup ?? null);
}, [sharedSource]);
const {
timelineId,
userPreferredVolume,
isMutedForPlayback,
volume,
muted,
playbackRate,
preservePitch,
onlyWarnForMediaSeekingError,
src,
onDuration,
// @ts-expect-error
acceptableTimeShift,
acceptableTimeShiftInSeconds,
toneFrequency,
name,
_remotionInternalNativeLoopPassed,
_remotionInternalStack,
style,
pauseWhenBuffering,
showInTimeline,
loopVolumeCurveBehavior,
onError,
onAutoPlayError,
onVideoFrame,
crossOrigin,
delayRenderRetries,
delayRenderTimeoutInMilliseconds,
allowAmplificationDuringRender,
useWebAudioApi,
audioStreamIndex,
...nativeProps
} = props;
const _propsValid: IsExact<typeof nativeProps, Expected> = true;
if (!_propsValid) {
throw new Error('typecheck error');
}
const {fps, durationInFrames} = useVideoConfig();
const parentSequence = useContext(SequenceContext);
const logLevel = useLogLevel();
const mountTime = useMountTime();
if (typeof acceptableTimeShift !== 'undefined') {
throw new Error(
'acceptableTimeShift has been removed. Use acceptableTimeShiftInSeconds instead.',
);
}
warnAboutTooHighVolume(userPreferredVolume);
// putting playback before useVolume
// because volume looks at playbackrate
useMediaPlayback({
mediaRef: videoRef,
src,
mediaType: 'video',
playbackRate: props.playbackRate ?? 1,
preservePitch,
onlyWarnForMediaSeekingError,
acceptableTimeshift: acceptableTimeShiftInSeconds ?? null,
isPremounting: Boolean(parentSequence?.premounting),
isPostmounting: Boolean(parentSequence?.postmounting),
pauseWhenBuffering,
onAutoPlayError: onAutoPlayError ?? null,
});
useMediaTag({
id: timelineId,
isPostmounting: Boolean(parentSequence?.postmounting),
isPremounting: Boolean(parentSequence?.premounting),
mediaRef: videoRef,
mediaType: 'video',
onAutoPlayError: onAutoPlayError ?? null,
});
useVolume({
logLevel,
mediaRef: videoRef,
volume: userPreferredVolume,
source: sharedSource,
shouldUseWebAudioApi: useWebAudioApi ?? false,
});
const actualFrom = parentSequence ? parentSequence.relativeFrom : 0;
const duration = parentSequence
? Math.min(parentSequence.durationInFrames, durationInFrames)
: durationInFrames;
const preloadedSrc = usePreload(src as string);
const actualSrc = useAppendVideoFragment({
actualSrc: preloadedSrc,
actualFrom,
duration,
fps,
});
useImperativeHandle(ref, () => {
return videoRef.current as HTMLVideoElement;
}, []);
useState(() =>
playbackLogging({
logLevel,
message: `Mounting video with source = ${actualSrc}, v=${VERSION}, user agent=${
typeof navigator === 'undefined' ? 'server' : navigator.userAgent
}`,
tag: 'video',
mountTime,
}),
);
useEffect(() => {
const {current} = videoRef;
if (!current) {
return;
}
const errorHandler = () => {
if (current.error) {
// eslint-disable-next-line no-console
console.error('Error occurred in video', current?.error);
// If user is handling the error, we don't cause an unhandled exception
if (onError) {
const err = new MediaPlaybackError({
message: `Code ${current.error.code}: ${current.error.message}`,
src: src as string,
});
onError(err);
return;
}
throw new MediaPlaybackError({
message: `The browser threw an error while playing the video ${src}: Code ${current.error.code} - ${current?.error?.message}. See https://remotion.dev/docs/media-playback-error for help. Pass an onError() prop to handle the error.`,
src: src as string,
});
} else {
// If user is handling the error, we don't cause an unhandled exception
if (onError) {
const err = new MediaPlaybackError({
message: `The browser threw an error while playing the video ${src}`,
src: src as string,
});
onError(err);
return;
}
throw new MediaPlaybackError({
message: 'The browser threw an error while playing the video',
src: src as string,
});
}
};
current.addEventListener('error', errorHandler, {once: true});
return () => {
current.removeEventListener('error', errorHandler);
};
}, [onError, src]);
useEmitVideoFrame({ref: videoRef, onVideoFrame});
useMediaDuration(videoRef, onDuration, src as string, false);
useEffect(() => {
const {current} = videoRef;
if (!current) {
return;
}
// Without this, on iOS Safari, the video cannot be seeked.
// if a seek is triggered before `loadedmetadata` is fired,
// the video will not seek, even if `loadedmetadata` is fired afterwards.
// Also, this needs to happen in a useEffect, because otherwise
// the SSR props will be applied.
if (isIosSafari()) {
current.preload = 'metadata';
} else {
current.preload = 'auto';
}
}, []);
const actualStyle: React.CSSProperties = useMemo(() => {
return {
...style,
};
}, [style]);
const crossOriginValue = getCrossOriginValue({
crossOrigin,
requestsVideoFrame: Boolean(onVideoFrame),
isClientSideRendering: false,
});
const video = (
<video
{...nativeProps}
ref={videoRef}
muted={isMutedForPlayback}
playsInline
src={actualSrc}
loop={_remotionInternalNativeLoopPassed}
style={actualStyle}
disableRemotePlayback
crossOrigin={crossOriginValue}
controls={false}
/>
);
return video;
};
const VideoForPreviewContent = forwardRef(
VideoForDevelopmentRefForwardingFunction,
);
const VideoForPreviewRefForwardingFunction: React.ForwardRefRenderFunction<
HTMLVideoElement,
VideoForPreviewProps
> = (props, ref) => {
const {
volume,
muted,
playbackRate,
src,
name,
_remotionInternalStack,
showInTimeline,
loopVolumeCurveBehavior,
onlyWarnForMediaSeekingError,
} = props;
const [timelineId] = useState(() => String(Math.random()));
const parentSequence = useContext(SequenceContext);
const {isStudio} = useRemotionEnvironment();
const [mediaVolume] = useMediaVolumeState();
const volumePropFrame = useFrameForVolumeProp(
loopVolumeCurveBehavior ?? 'repeat',
);
const userPreferredVolume = evaluateVolume({
frame: volumePropFrame,
volume,
mediaVolume,
});
const {isMutedForTimeline, isMutedForPlayback} = useMediaAudioState({
muted: muted ?? false,
volume: userPreferredVolume,
audioEnabled: true,
});
const getStack = useCallback(() => {
return _remotionInternalStack ?? null;
}, [_remotionInternalStack]);
const {automaticOutlineRef, registration} = useMediaInTimelineRegistration({
mediaType: 'video',
src,
playbackRate: playbackRate ?? 1,
displayName: name ?? null,
id: timelineId,
getStack,
showInTimeline,
premountDisplay: parentSequence?.premountDisplay ?? null,
postmountDisplay: parentSequence?.postmountDisplay ?? null,
loopDisplay: undefined,
documentationLink: onlyWarnForMediaSeekingError
? 'https://www.remotion.dev/docs/offthreadvideo'
: 'https://www.remotion.dev/docs/html5-video',
muted: isMutedForTimeline,
});
const metadata = useMemo<CommittedMetadata | null>(
() =>
isStudio || automaticOutlineRef || registration !== null
? {
type: 'sequence',
id: timelineId,
value: registration,
outlineChildrenRef: automaticOutlineRef,
}
: null,
[automaticOutlineRef, isStudio, registration, timelineId],
);
return (
<CommittedMetadataProvider value={null} _remotionCommitMetadata={metadata}>
<SequenceContent>
<VideoForPreviewContent
{...props}
ref={ref}
timelineId={timelineId}
userPreferredVolume={userPreferredVolume}
isMutedForPlayback={isMutedForPlayback}
/>
</SequenceContent>
</CommittedMetadataProvider>
);
};
export const VideoForPreview = forwardRef(VideoForPreviewRefForwardingFunction);