Skip to content

🎥 Make videos programmatically with React

5.2 KB
Raw Blame
import type {ComponentType, LazyExoticComponent} from 'react';
import React from 'react';
import type {AnyZodObject} from './any-zod-type.js';
import type {CalculateMetadataFunction} from './Composition.js';
import type {DownloadBehavior} from './download-behavior.js';
import type {EffectDefinition} from './effects/effect-types.js';
import type {InteractivitySchema} from './interactivity-schema.js';
import type {InferProps, PropsIfHasProps} from './props-if-has-props.js';
import type {
	RuntimeValueSnapshot,
	RuntimeValueStore,
} from './runtime-value-store.js';
import type {TimelineTrackItem} from './timeline-track-context.js';
import type {VideoConfigValues} from './video-config.js';

export type TComposition<
	Schema extends AnyZodObject,
	Props extends Record<string, unknown>,
> = {
	width: number | undefined;
	height: number | undefined;
	fps: number | undefined;
	durationInFrames: number | undefined;
	id: string;
	folderName: string | null;
	parentFolderName: string | null;
	component: LazyExoticComponent<ComponentType<Props>> | ComponentType<Props>;
	componentFromProps?: unknown;
	order: number | null;
	schema: Schema | null;
	calculateMetadata: CalculateMetadataFunction<
		InferProps<Schema, Props>
	> | null;
	stack: string | null;
} & PropsIfHasProps<Schema, Props>;

export type AnyComposition = TComposition<
	AnyZodObject,
	Record<string, unknown>
>;

export type TCompMetadataWithCalcFunction<
	Schema extends AnyZodObject,
	Props extends Record<string, unknown>,
> = Pick<
	TComposition<Schema, Props>,
	| 'id'
	| 'height'
	| 'width'
	| 'fps'
	| 'durationInFrames'
	| 'defaultProps'
	| 'calculateMetadata'
>;

export type TCompMetadata<
	Schema extends AnyZodObject,
	Props extends Record<string, unknown>,
> = Pick<
	TComposition<Schema, Props>,
	'id' | 'height' | 'width' | 'fps' | 'durationInFrames' | 'defaultProps'
>;

export type AnyCompMetadata = TCompMetadata<
	AnyZodObject,
	Record<string, unknown>
>;

type EnhancedTSequenceData =
	| {
			type: 'sequence';
	  }
	| {
			type: 'audio';
			src: string;
			muted: boolean;
			startMediaFrom: number;
			mediaFrameAtSequenceZero: number | null;
			playbackRate: number;
			frozenMediaFrame: number | null;
	  }
	| {
			type: 'video';
			src: string;
			muted: boolean;
			startMediaFrom: number;
			mediaFrameAtSequenceZero: number | null;
			playbackRate: number;
			frozenMediaFrame: number | null;
	  }
	| {
			type: 'image';
			src: string;
	  };

export type LoopDisplay = {
	numberOfTimes: number;
	startOffset: number;
	durationInFrames: number;
};

export type JsxComponentIdentity = string;

export type SequenceRegistrationControls = {
	schema: InteractivitySchema;
	runtimeValues: RuntimeValueStore;
	videoConfigValues: VideoConfigValues | null;
	overrideId: string;
	supportsEffects: boolean;
	componentIdentity: JsxComponentIdentity | null;
	componentName: string;
};

export type SequenceControls = SequenceRegistrationControls & {
	currentRuntimeValueDotNotation: RuntimeValueSnapshot;
};

export type TSequence = {
	/** This sequence's clock multiplier; ancestor rates are applied by the timeline. */
	sequencePlaybackRate: number;
	from: number;
	trimBefore: number | null;
	duration: number;
	/** Infer the timeline display duration from children without changing playback. */
	readonly autoDuration?: boolean | null;
	/** Whether the wrapper can restore its duration from its scenes. */
	readonly canInferDuration?: boolean | null;
	/** Full scene duration in the parent clock, before container clipping. */
	readonly unclippedDuration?: number | null;
	id: string;
	displayName: string;
	documentationLink: string | null;
	parent: string | null;
	showInTimeline: boolean;
	timelineOrder: number | null;
	timelineTrack?: TimelineTrackItem;
	loopDisplay: LoopDisplay | undefined;
	getStack: () => string | null;
	/** Read the committed child clock without updating timeline metadata each frame. */
	getCurrentFrame?: (() => number) | null;
	premountDisplay: number | null;
	postmountDisplay: number | null;
	controls: SequenceRegistrationControls | null;
	refForOutline: React.RefObject<Element | null> | null;
	effects: readonly EffectDefinition<unknown>[];
	effectRuntimeValues: readonly RuntimeValueStore[] | null;
	isInsideSeries: boolean;
	frozenFrame: number | null;
	singleChildComponent?: unknown;
} & EnhancedTSequenceData;

export type AudioOrVideoAsset = {
	type: 'audio' | 'video';
	src: string;
	id: string;
	frame: number;
	volume: number;
	mediaFrame: number;
	playbackRate: number;
	toneFrequency: number;
	audioStartFrame: number;
	audioStreamIndex: number;
	preservePitch: boolean | null;
};

export type InlineAudioAsset = {
	type: 'inline-audio';
	id: string;
	audio: Int16Array | number[];
	frame: number;
	startInVideo: number | null;
	timestamp: number;
	duration: number;
	toneFrequency: number;
};

type DiscriminatedArtifact =
	| {
			contentType: 'binary';
			content: string;
	  }
	| {
			contentType: 'text';
			content: string;
	  }
	| {
			contentType: 'thumbnail';
	  };

export type ArtifactAsset = {
	type: 'artifact';
	id: string;
	filename: string;
	frame: number;
	downloadBehavior: DownloadBehavior | null;
} & DiscriminatedArtifact;

export type TRenderAsset = AudioOrVideoAsset | ArtifactAsset | InlineAudioAsset;

export const compositionsRef = React.createRef<{
	getCompositions: () => AnyComposition[];
}>();