🎥 Make videos programmatically with React
import {
optimisticAddSequenceKeyframe,
optimisticUpdateForPropStatuses,
type CaptionPatch,
type SaveSequencePropSourceEdit,
} from '@remotion/studio-shared';
import type {
CanUpdateSequencePropsResponse,
SequencePropsSubscriptionKey,
InteractivitySchema,
} from 'remotion';
import {saveSequenceProps as saveSequencePropsApi} from '../sequence-props-api';
import type {AddSequenceKeyframeChange} from './call-add-keyframe';
import {
applyOptimisticKeyframeMoves,
type MoveEffectKeyframeChange,
type MoveSequenceKeyframeChange,
} from './call-move-keyframe';
import {enqueueSavePropChange} from './save-prop-queue';
export type SetPropStatuses = (
nodePath: SequencePropsSubscriptionKey,
values: (
prev: CanUpdateSequencePropsResponse,
) => CanUpdateSequencePropsResponse,
) => void;
export type SaveSequencePropChange = {
fileName: string;
nodePath: SequencePropsSubscriptionKey;
fieldKey: string;
value: unknown;
defaultValue: string | null;
schema: InteractivitySchema;
sourceEdit?: SaveSequencePropSourceEdit;
};
type SaveSequencePropsOptions = {
changes: SaveSequencePropChange[];
addedKeyframes: AddSequenceKeyframeChange[] | null;
movedKeyframes: {
sequenceKeyframes: MoveSequenceKeyframeChange[];
effectKeyframes: MoveEffectKeyframeChange[];
} | null;
setPropStatuses: SetPropStatuses;
clientId: string;
undoLabel: string;
redoLabel: string;
};
const serializeSequencePropValue = (value: unknown) => {
if (value === undefined) {
return {type: 'undefined' as const};
}
return {type: 'json' as const, serialized: JSON.stringify(value)};
};
export const saveInlineCaptionPatches = ({
fileName,
nodePath,
schema,
patches,
nextCaptions,
setPropStatuses,
clientId,
undoLabel,
redoLabel,
}: {
fileName: string;
nodePath: SequencePropsSubscriptionKey;
schema: InteractivitySchema;
patches: CaptionPatch[];
nextCaptions: unknown;
setPropStatuses: SetPropStatuses;
clientId: string;
undoLabel: string;
redoLabel: string;
}): Promise<void> => {
return enqueueSavePropChange({
nodePath,
setPropStatuses,
applyOptimistic: (previous) =>
optimisticUpdateForPropStatuses({
previous,
fieldKey: 'captions',
value: nextCaptions,
defaultValue: null,
schema,
}),
apiCall: () =>
saveSequencePropsApi({
edits: [],
captionPatches: [{fileName, nodePath, schema, patches}],
addedKeyframes: null,
movedKeyframes: null,
clientId,
undoLabel,
redoLabel,
}),
errorLabel: 'Could not save captions',
});
};
export const saveSequenceProps = ({
changes,
addedKeyframes,
movedKeyframes,
setPropStatuses,
clientId,
undoLabel,
redoLabel,
}: SaveSequencePropsOptions): Promise<void> => {
const keyframesToAdd = addedKeyframes === null ? [] : addedKeyframes;
const sequenceKeyframes =
movedKeyframes === null ? [] : movedKeyframes.sequenceKeyframes;
const effectKeyframes =
movedKeyframes === null ? [] : movedKeyframes.effectKeyframes;
if (
changes.length === 0 &&
keyframesToAdd.length === 0 &&
sequenceKeyframes.length === 0 &&
effectKeyframes.length === 0
) {
return Promise.resolve();
}
if (
changes.length === 1 &&
keyframesToAdd.length === 0 &&
sequenceKeyframes.length === 0 &&
effectKeyframes.length === 0
) {
const change = changes[0];
if (change === undefined) {
throw new Error('Expected a sequence prop change');
}
return enqueueSavePropChange({
nodePath: change.nodePath,
setPropStatuses,
// Font edits prepare their assets inside the source-mutation queue.
// Keep the current value until that preparation and persistence finish.
applyOptimistic: (prev) =>
change.sourceEdit?.type === 'clipboard-param' ||
change.sourceEdit?.type === 'google-font'
? prev
: optimisticUpdateForPropStatuses({
previous: prev,
fieldKey: change.fieldKey,
value: change.value,
defaultValue: change.defaultValue,
schema: change.schema,
}),
apiCall: () =>
saveSequencePropsApi({
edits: [
{
fileName: change.fileName,
nodePath: change.nodePath,
key: change.fieldKey,
value: serializeSequencePropValue(change.value),
defaultValue: change.defaultValue,
schema: change.schema,
sourceEdit: change.sourceEdit ?? null,
},
],
addedKeyframes: null,
movedKeyframes: null,
clientId,
undoLabel,
redoLabel,
}),
errorLabel: 'Could not save sequence prop',
});
}
applyOptimisticKeyframeMoves({
sequenceKeyframes,
effectKeyframes,
setPropStatuses,
});
for (const keyframe of keyframesToAdd) {
setPropStatuses(keyframe.nodePath, (prev) =>
optimisticAddSequenceKeyframe({
videoConfigValues: keyframe.nodePath.videoConfigValues,
previous: prev,
fieldKey: keyframe.fieldKey,
frame: keyframe.sourceFrame,
value: keyframe.value,
schema: keyframe.schema,
}),
);
}
for (const change of changes) {
if (
change.sourceEdit?.type === 'clipboard-param' ||
change.sourceEdit?.type === 'google-font'
) {
continue;
}
setPropStatuses(change.nodePath, (prev) =>
optimisticUpdateForPropStatuses({
previous: prev,
fieldKey: change.fieldKey,
value: change.value,
defaultValue: change.defaultValue,
schema: change.schema,
}),
);
}
return saveSequencePropsApi({
edits: changes.map((change) => {
return {
fileName: change.fileName,
nodePath: change.nodePath,
key: change.fieldKey,
value: serializeSequencePropValue(change.value),
defaultValue: change.defaultValue,
schema: change.schema,
sourceEdit: change.sourceEdit ?? null,
};
}),
addedKeyframes:
addedKeyframes === null
? null
: addedKeyframes.map((keyframe) => ({
fileName: keyframe.fileName,
nodePath: keyframe.nodePath,
key: keyframe.fieldKey,
frame: keyframe.sourceFrame,
value: JSON.stringify(keyframe.value),
schema: keyframe.schema,
})),
movedKeyframes:
movedKeyframes === null
? null
: {
sequenceKeyframes: sequenceKeyframes.map((keyframe) => ({
fileName: keyframe.fileName,
nodePath: keyframe.nodePath,
key: keyframe.fieldKey,
fromFrame: keyframe.fromFrame,
toFrame: keyframe.toFrame,
schema: keyframe.schema,
})),
effectKeyframes: effectKeyframes.map((keyframe) => ({
fileName: keyframe.fileName,
sequenceNodePath: keyframe.nodePath,
effectIndex: keyframe.effectIndex,
key: keyframe.fieldKey,
fromFrame: keyframe.fromFrame,
toFrame: keyframe.toFrame,
schema: keyframe.schema,
})),
},
clientId,
undoLabel,
redoLabel,
}).then(() => undefined);
};