Skip to content

🎥 Make videos programmatically with React

14.0 KB
Raw Blame
import type {ChromeMode} from '@remotion/renderer';
import {BrowserSafeApis} from '@remotion/renderer/client';
import type {UiOpenGlOptions} from '@remotion/studio-shared';
import type {ChangeEvent} from 'react';
import React, {useCallback, useMemo} from 'react';
import {Checkmark} from '../../icons/Checkmark';
import {Checkbox} from '../Checkbox';
import {Spacing} from '../layout';
import {VERTICAL_SCROLLBAR_CLASSNAME} from '../Menu/is-menu-item';
import type {ComboboxValue} from '../NewComposition/ComboBox';
import {Combobox} from '../NewComposition/ComboBox';
import {RemotionInput} from '../NewComposition/RemInput';
import {input, label, optionRow, rightRow} from './layout';
import {NumberSetting} from './NumberSetting';
import {OptionExplainerBubble} from './OptionExplainerBubble';
import {RenderModalHr} from './RenderModalHr';

export type RenderType = 'still' | 'video' | 'audio' | 'sequence';

const container: React.CSSProperties = {
	flex: 1,
	overflowY: 'auto',
};

export const RenderModalAdvanced: React.FC<{
	readonly renderMode: RenderType;
	readonly minConcurrency: number;
	readonly maxConcurrency: number;
	readonly setConcurrency: React.Dispatch<React.SetStateAction<number>>;
	readonly concurrency: number;
	readonly delayRenderTimeout: number;
	readonly setDelayRenderTimeout: React.Dispatch<React.SetStateAction<number>>;
	readonly setDisableWebSecurity: React.Dispatch<React.SetStateAction<boolean>>;
	readonly setIgnoreCertificateErrors: React.Dispatch<
		React.SetStateAction<boolean>
	>;
	readonly setHeadless: React.Dispatch<React.SetStateAction<boolean>>;
	readonly headless: boolean;
	readonly ignoreCertificateErrors: boolean;
	readonly disableWebSecurity: boolean;
	readonly openGlOption: UiOpenGlOptions;
	readonly setOpenGlOption: React.Dispatch<
		React.SetStateAction<UiOpenGlOptions>
	>;
	readonly chromeModeOption: ChromeMode;
	readonly setChromeModeOption: React.Dispatch<
		React.SetStateAction<ChromeMode>
	>;
	readonly offthreadVideoCacheSizeInBytes: number | null;
	readonly setOffthreadVideoCacheSizeInBytes: React.Dispatch<
		React.SetStateAction<number | null>
	>;
	readonly mediaCacheSizeInBytes: number | null;
	readonly setMediaCacheSizeInBytes: React.Dispatch<
		React.SetStateAction<number | null>
	>;
	readonly offthreadVideoThreads: number | null;
	readonly setOffthreadVideoThreads: React.Dispatch<
		React.SetStateAction<number | null>
	>;
	readonly enableMultiProcessOnLinux: boolean;
	readonly darkMode: boolean;
	readonly setDarkMode: React.Dispatch<React.SetStateAction<boolean>>;
	readonly setChromiumMultiProcessOnLinux: React.Dispatch<
		React.SetStateAction<boolean>
	>;
	readonly userAgent: string | null;
	readonly setUserAgent: React.Dispatch<React.SetStateAction<string | null>>;
	readonly beep: boolean;
	readonly setBeep: React.Dispatch<React.SetStateAction<boolean>>;
	readonly repro: boolean;
	readonly setRepro: React.Dispatch<React.SetStateAction<boolean>>;
}> = ({
	renderMode,
	maxConcurrency,
	minConcurrency,
	setConcurrency,
	concurrency,
	delayRenderTimeout,
	setDelayRenderTimeout,
	setDisableWebSecurity,
	setIgnoreCertificateErrors,
	setHeadless,
	headless,
	ignoreCertificateErrors,
	disableWebSecurity,
	openGlOption,
	setOpenGlOption,
	setMediaCacheSizeInBytes,
	mediaCacheSizeInBytes,
	offthreadVideoCacheSizeInBytes,
	setOffthreadVideoCacheSizeInBytes,
	offthreadVideoThreads,
	setOffthreadVideoThreads,
	enableMultiProcessOnLinux,
	setChromiumMultiProcessOnLinux,
	setUserAgent,
	userAgent,
	beep,
	setBeep,
	repro,
	setRepro,
	chromeModeOption,
	setChromeModeOption,
	darkMode,
	setDarkMode,
}) => {
	const extendedOpenGlOptions: UiOpenGlOptions[] = useMemo(() => {
		return [
			'angle',
			'egl',
			'swangle',
			'swiftshader',
			'vulkan',
			'angle-egl',
			'default',
		];
	}, []);

	const toggleCustomMediaCacheSizeInBytes = useCallback(() => {
		setMediaCacheSizeInBytes((previous) => {
			if (previous === null) {
				return 1000 * 1000 * 1000;
			}

			return null;
		});
	}, [setMediaCacheSizeInBytes]);

	const toggleCustomOffthreadVideoCacheSizeInBytes = useCallback(() => {
		setOffthreadVideoCacheSizeInBytes((previous) => {
			if (previous === null) {
				return 512 * 1024 * 1024;
			}

			return null;
		});
	}, [setOffthreadVideoCacheSizeInBytes]);

	const toggleCustomOffthreadVideoThreads = useCallback(() => {
		setOffthreadVideoThreads((previous) => {
			if (previous === null) {
				return 2;
			}

			return null;
		});
	}, [setOffthreadVideoThreads]);

	const toggleCustomUserAgent = useCallback(() => {
		setUserAgent((previous) => {
			if (previous === null) {
				return 'Mozilla/5.0 (Remotion)';
			}

			return null;
		});
	}, [setUserAgent]);

	const onDisableWebSecurityChanged = useCallback(
		(e: ChangeEvent<HTMLInputElement>) => {
			setDisableWebSecurity(e.target.checked);
		},
		[setDisableWebSecurity],
	);

	const onEnableMultiProcessOnLinux = useCallback(
		(e: ChangeEvent<HTMLInputElement>) => {
			setChromiumMultiProcessOnLinux(e.target.checked);
		},
		[setChromiumMultiProcessOnLinux],
	);

	const onIgnoreCertificatErrors = useCallback(
		(e: ChangeEvent<HTMLInputElement>) => {
			setIgnoreCertificateErrors(e.target.checked);
		},
		[setIgnoreCertificateErrors],
	);

	const onHeadless = useCallback(
		(e: ChangeEvent<HTMLInputElement>) => {
			setHeadless(e.target.checked);
		},
		[setHeadless],
	);

	const onUserAgentChanged: React.ChangeEventHandler<HTMLInputElement> =
		useCallback(
			(e) => {
				setUserAgent(e.target.value);
			},
			[setUserAgent],
		);

	const onDarkMode = useCallback(
		(e: ChangeEvent<HTMLInputElement>) => {
			setDarkMode(e.target.checked);
		},
		[setDarkMode],
	);

	const onPlayBeepSound = useCallback(
		(e: ChangeEvent<HTMLInputElement>) => {
			setBeep(e.target.checked);
		},
		[setBeep],
	);

	const onReproToggle = useCallback(
		(e: ChangeEvent<HTMLInputElement>) => {
			setRepro(e.target.checked);
		},
		[setRepro],
	);

	const openGlOptions = useMemo((): ComboboxValue[] => {
		return extendedOpenGlOptions.map((option) => {
			return {
				label: option === 'default' ? 'Default' : option,
				onClick: () => setOpenGlOption(option),
				key: option,
				leftItem: openGlOption === option ? <Checkmark /> : null,
				id: option,
				keyHint: null,
				quickSwitcherLabel: null,
				subMenu: null,
				type: 'item',
				value: option,
			};
		});
	}, [extendedOpenGlOptions, openGlOption, setOpenGlOption]);

	const chromeModeOptions = useMemo((): ComboboxValue[] => {
		return BrowserSafeApis.validChromeModeOptions.map((option) => {
			return {
				label: option,
				onClick: () => setChromeModeOption(option),
				key: option,
				leftItem: chromeModeOption === option ? <Checkmark /> : null,
				id: option,
				keyHint: null,
				quickSwitcherLabel: null,
				subMenu: null,
				type: 'item',
				value: option,
			};
		});
	}, [chromeModeOption, setChromeModeOption]);

	const changeMediaCacheSizeInBytes: React.Dispatch<
		React.SetStateAction<number>
	> = useCallback(
		(cb) => {
			setMediaCacheSizeInBytes((prev) => {
				if (prev === null) {
					throw new TypeError('Expected previous value');
				}

				if (typeof cb === 'function') {
					return cb(prev);
				}

				return cb;
			});
		},
		[setMediaCacheSizeInBytes],
	);

	const changeOffthreadVideoCacheSizeInBytes: React.Dispatch<
		React.SetStateAction<number>
	> = useCallback(
		(cb) => {
			setOffthreadVideoCacheSizeInBytes((prev) => {
				if (prev === null) {
					throw new TypeError('Expected previous value');
				}

				if (typeof cb === 'function') {
					return cb(prev);
				}

				return cb;
			});
		},
		[setOffthreadVideoCacheSizeInBytes],
	);

	const changeOffthreadVideoThreads: React.Dispatch<
		React.SetStateAction<number>
	> = useCallback(
		(cb) => {
			setOffthreadVideoThreads((prev) => {
				if (prev === null) {
					throw new TypeError('Expected previous value');
				}

				if (typeof cb === 'function') {
					return cb(prev);
				}

				return cb;
			});
		},
		[setOffthreadVideoThreads],
	);

	return (
		<div style={container} className={VERTICAL_SCROLLBAR_CLASSNAME}>
			{renderMode === 'still' ? null : (
				<NumberSetting
					min={minConcurrency}
					max={maxConcurrency}
					step={1}
					name="Concurrency"
					formatter={(w) => `${w}x`}
					onValueChanged={setConcurrency}
					value={concurrency}
				/>
			)}
			<NumberSetting
				// Also appears in packages/renderer/src/validate-puppeteer-timeout.ts
				min={7_000}
				max={900_000}
				name="delayRender() timeout"
				onValueChanged={setDelayRenderTimeout}
				formatter={(w) => `${w}ms`}
				step={1000}
				hint="delayRenderTimeoutInMillisecondsOption"
				value={delayRenderTimeout}
			/>
			{renderMode === 'audio' ? null : (
				<div style={optionRow}>
					<div style={label}>
						Custom @remotion/media cache size
						<Spacing x={0.5} />
						<OptionExplainerBubble id="mediaCacheSizeInBytesOption" />
					</div>
					<div style={rightRow}>
						<Checkbox
							checked={mediaCacheSizeInBytes !== null}
							onChange={toggleCustomMediaCacheSizeInBytes}
							name="media-cache-size"
						/>
					</div>
				</div>
			)}
			{renderMode === 'audio' || mediaCacheSizeInBytes === null ? null : (
				<NumberSetting
					min={0}
					max={2000 * 1024 * 1024}
					step={1024}
					name="@remotion/media cache size"
					formatter={(w) => `${w} bytes`}
					onValueChanged={changeMediaCacheSizeInBytes}
					value={mediaCacheSizeInBytes}
				/>
			)}
			{renderMode === 'audio' ? null : (
				<div style={optionRow}>
					<div style={label}>
						Custom OffthreadVideo cache
						<Spacing x={0.5} />
						<OptionExplainerBubble id="offthreadVideoCacheSizeInBytesOption" />
					</div>
					<div style={rightRow}>
						<Checkbox
							checked={offthreadVideoCacheSizeInBytes !== null}
							onChange={toggleCustomOffthreadVideoCacheSizeInBytes}
							name="custom-audio-bitrate"
						/>
					</div>
				</div>
			)}
			{renderMode === 'audio' ||
			offthreadVideoCacheSizeInBytes === null ? null : (
				<NumberSetting
					min={0}
					max={2000 * 1024 * 1024}
					step={1024}
					name="OffthreadVideo cache size"
					formatter={(w) => `${w} bytes`}
					onValueChanged={changeOffthreadVideoCacheSizeInBytes}
					value={offthreadVideoCacheSizeInBytes}
				/>
			)}
			{renderMode === 'audio' ? null : (
				<div style={optionRow}>
					<div style={label}>
						OffthreadVideo threads <Spacing x={0.5} />
						<OptionExplainerBubble id="offthreadVideoThreadsOption" />
					</div>
					<div style={rightRow}>
						<Checkbox
							checked={offthreadVideoThreads !== null}
							onChange={toggleCustomOffthreadVideoThreads}
							name="offthread-video-threads"
						/>
					</div>
				</div>
			)}
			{renderMode === 'audio' || offthreadVideoThreads === null ? null : (
				<NumberSetting
					min={0}
					max={16}
					step={1}
					name="OffthreadVideo threads"
					formatter={(w) => `${w}x`}
					onValueChanged={changeOffthreadVideoThreads}
					value={offthreadVideoThreads}
				/>
			)}
			<RenderModalHr />
			<div style={optionRow}>
				<div style={label}>Disable web security</div>
				<div style={rightRow}>
					<Checkbox
						checked={disableWebSecurity}
						onChange={onDisableWebSecurityChanged}
						name="disable-web-security"
					/>
				</div>
			</div>
			<div style={optionRow}>
				<div style={label}>Ignore certificate errors</div>
				<div style={rightRow}>
					<Checkbox
						checked={ignoreCertificateErrors}
						onChange={onIgnoreCertificatErrors}
						name="ignore-certificate-errors"
					/>
				</div>
			</div>
			<div style={optionRow}>
				<div style={label}>
					Headless mode
					<Spacing x={0.5} />
					<OptionExplainerBubble id="headlessOption" />
				</div>
				<div style={rightRow}>
					<Checkbox checked={headless} onChange={onHeadless} name="headless" />
				</div>
			</div>
			<div style={optionRow}>
				<div style={label}>
					Chrome Mode <Spacing x={0.5} />
					<OptionExplainerBubble id="chromeModeOption" />
				</div>

				<div style={rightRow}>
					<Combobox
						values={chromeModeOptions}
						selectedId={chromeModeOption}
						aria-label="Chrome mode"
					/>
				</div>
			</div>
			<div style={optionRow}>
				<div style={label}>
					OpenGL render backend <Spacing x={0.5} />
					<OptionExplainerBubble id="glOption" />
				</div>

				<div style={rightRow}>
					<Combobox
						values={openGlOptions}
						selectedId={openGlOption}
						aria-label="OpenGl option"
					/>
				</div>
			</div>
			<div style={optionRow}>
				<div style={label}>
					Multi-process Chrome on Linux
					<Spacing x={0.5} />
					<OptionExplainerBubble id="enableMultiprocessOnLinuxOption" />
				</div>
				<div style={rightRow}>
					<Checkbox
						checked={enableMultiProcessOnLinux}
						onChange={onEnableMultiProcessOnLinux}
						name="enable-multi-process-on-linux"
					/>
				</div>
			</div>
			<div style={optionRow}>
				<div style={label}>
					Dark Mode
					<Spacing x={0.5} />
					<OptionExplainerBubble id="darkModeOption" />
				</div>
				<div style={rightRow}>
					<Checkbox checked={darkMode} onChange={onDarkMode} name="dark-mode" />
				</div>
			</div>
			<div style={optionRow}>
				<div style={label}>Custom User Agent</div>
				<div style={rightRow}>
					<Checkbox
						checked={userAgent !== null}
						onChange={toggleCustomUserAgent}
						name="custom-user-agent"
					/>
				</div>
			</div>
			{userAgent === null ? null : (
				<div style={optionRow}>
					<div style={label}>User Agent</div>
					<div style={rightRow}>
						<div>
							<RemotionInput
								style={input}
								value={userAgent}
								onChange={onUserAgentChanged}
								status="ok"
								rightAlign
							/>
						</div>
					</div>
				</div>
			)}
			<div style={optionRow}>
				<div style={label}>
					Create a reproduction <OptionExplainerBubble id="reproOption" />
				</div>

				<div style={rightRow}>
					<Checkbox checked={repro} onChange={onReproToggle} name="repro" />
				</div>
			</div>
			<div style={optionRow}>
				<div style={label}>
					Beep when finished <OptionExplainerBubble id="beepOnFinishOption" />
				</div>

				<div style={rightRow}>
					<Checkbox
						checked={beep}
						onChange={onPlayBeepSound}
						name="beep-when-finished"
					/>
				</div>
			</div>
		</div>
	);
};