🎥 Make videos programmatically with React
import {Internals, type LogLevel} from 'remotion';
import type {MediaCache} from '../caches';
import {getTimeInSeconds} from '../get-time-in-seconds';
import type {MediaRequestInit} from '../request-init';
type ExtractFrameResult =
| {
type: 'success';
frame: VideoFrame | null;
rotation: number;
durationInSeconds: number | null;
}
| {type: 'cannot-decode'; durationInSeconds: number | null; error: Error}
| {type: 'cannot-decode-prores'; durationInSeconds: number | null}
| {type: 'cannot-decode-alpha'; durationInSeconds: number | null}
| {type: 'unknown-container-format'}
| {type: 'network-error'};
type ExtractFrameParams = {
src: string;
timeInSeconds: number;
logLevel: LogLevel;
loop: boolean;
trimAfter: number | undefined;
trimBefore: number | undefined;
playbackRate: number;
fps: number;
maxCacheSize: number;
credentials: RequestCredentials | undefined;
requestInit?: MediaRequestInit;
mediaCache: MediaCache;
};
const extractFrameInternal = async ({
src,
timeInSeconds: unloopedTimeInSeconds,
logLevel,
loop,
trimAfter,
trimBefore,
playbackRate,
fps,
maxCacheSize,
credentials,
requestInit,
mediaCache,
}: ExtractFrameParams): Promise<ExtractFrameResult> => {
const sink = await mediaCache.sinkManager.getSink(
src,
logLevel,
credentials,
requestInit,
);
const [video, mediaDurationInSecondsRaw] = await Promise.all([
sink.getVideo(),
loop ? sink.getDuration() : Promise.resolve(null),
]);
const mediaDurationInSeconds: number | null = loop
? mediaDurationInSecondsRaw
: null;
if (video === 'no-video-track') {
throw new Error(`No video track found for ${src}`);
}
if (video === 'cannot-decode') {
return {
type: 'cannot-decode',
durationInSeconds: mediaDurationInSeconds,
error: new Error(
`The video track in ${src} has a codec or configuration the browser cannot decode.`,
),
};
}
if (video === 'cannot-decode-prores') {
return {
type: 'cannot-decode-prores',
durationInSeconds: mediaDurationInSeconds,
};
}
if (video === 'unknown-container-format') {
return {type: 'unknown-container-format'};
}
if (video === 'network-error') {
return {type: 'network-error'};
}
if (video === 'cannot-decode-alpha') {
return {
type: 'cannot-decode-alpha',
durationInSeconds: mediaDurationInSeconds,
};
}
const timeInSeconds = getTimeInSeconds({
loop,
mediaDurationInSeconds,
unloopedTimeInSeconds,
src,
trimAfter,
playbackRate,
trimBefore,
fps,
ifNoMediaDuration: 'fail',
});
if (timeInSeconds === null) {
return {
type: 'success',
frame: null,
rotation: 0,
durationInSeconds: await sink.getDuration(),
};
}
// Must catch https://github.com/Vanilagy/mediabunny/issues/235
// https://discord.com/channels/@me/1127949286789881897/1455728482150518906
// Should be able to remove once upgraded to Chrome 145
try {
const keyframeBank = await mediaCache.keyframeManager.requestKeyframeBank({
videoSampleSink: video.sampleSink,
timeResolution: video.timeResolution,
timestamp: timeInSeconds,
src,
logLevel,
maxCacheSize,
fps,
});
if (!keyframeBank) {
return {
type: 'success',
frame: null,
rotation: 0,
durationInSeconds: await sink.getDuration(),
};
}
const frame = await keyframeBank.getFrameFromTimestamp(timeInSeconds, fps);
const rotation = frame?.rotation ?? 0;
return {
type: 'success',
frame: frame?.toVideoFrame() ?? null,
rotation,
durationInSeconds: await sink.getDuration(),
};
} catch (err) {
Internals.Log.info(
{logLevel, tag: '@remotion/media'},
`Error decoding ${src} at time ${timeInSeconds}: ${err}`,
err,
);
return {
type: 'cannot-decode',
durationInSeconds: mediaDurationInSeconds,
error: new Error(
`Video decoding failed for ${src} at ${timeInSeconds}s: ${err}`,
{cause: err},
),
};
}
};
type ExtractFrameReturnType = Awaited<ReturnType<typeof extractFrameInternal>>;
export const extractFrame = (
params: ExtractFrameParams,
): Promise<ExtractFrameReturnType> => {
return params.mediaCache.queueFrameExtraction(() =>
extractFrameInternal(params),
);
};