Skip to content

🎥 Make videos programmatically with React

3.4 KB
Raw Blame
import {
	BrowserStudio,
	createBlankTemplateProject,
	loadGitHubRepository,
	type VirtualProject,
} from '@remotion/browser-studio';
import {StudioProtocolInternals} from '@remotion/studio-protocol';
import {createRoot} from 'react-dom/client';
import {VERSION} from 'remotion/version';

const initialElementPayload = StudioProtocolInternals.parseBrowserStudioHash(
	window.location.hash,
);
(
	window as typeof window & {__browserStudioRemotionVersion: string}
).__browserStudioRemotionVersion = VERSION;
const render = async () => {
	const source = new URLSearchParams(window.location.search).get('source');
	const project = new URLSearchParams(window.location.search).has('github')
		? await loadGitHubRepository({
				repoUrl: 'https://github.com/remotion-dev/opfs-fixture',
			})
		: createBlankTemplateProject();
	(
		window as typeof window & {
			__browserStudioProject: VirtualProject;
			__browserStudioRemotionVersion: string;
		}
	).__browserStudioProject = project;
	if (!new URLSearchParams(window.location.search).has('github')) {
		project.files['/project/src/index.ts'] =
			`import {fade} from '@remotion/transitions/fade';
import {registerRoot} from 'remotion';
import {RemotionRoot} from './Root';

void fade;
registerRoot(RemotionRoot);
${
	source === 'fallback' ||
	source === 'transformers-override' ||
	source === 'transformers-vendor'
		? `(globalThis as typeof globalThis & {__loadBrowserStudioTransformers: () => Promise<unknown>}).__loadBrowserStudioTransformers = () => import('@huggingface/transformers');`
		: ''
}
`;
	}

	if (source === 'source-expressions') {
		project.files['/project/src/Composition.tsx'] = await (
			await fetch('/source-expressions.tsx')
		).text();
		project.files['/project/src/Root.tsx'] =
			`import {Composition} from 'remotion';
import {SourceSubscriptionChurn} from './Composition';
export const RemotionRoot = () => <Composition id="SourceSubscriptionChurn" component={SourceSubscriptionChurn} durationInFrames={600} fps={30} width={1280} height={720} />;`;
	}

	const root = document.getElementById('root');
	if (!root) {
		throw new Error('Could not find root element');
	}

	createRoot(root).render(
		<BrowserStudio
			dependencyResolver={
				source === 'fallback'
					? ({name, version}) => (name === 'react' ? version : null)
					: source === 'transformers-override'
						? ({name}) =>
								name === '@huggingface/transformers'
									? {
											url: new URL(
												'/fake-transformers.mjs',
												window.location.href,
											).href,
										}
									: null
						: undefined
			}
			initialElement={
				initialElementPayload === null
					? null
					: {payload: initialElementPayload, sourceOrigin: null}
			}
			project={project}
			readOnly={false}
			onProjectChange={(nextProject) => {
				(
					window as typeof window & {__browserStudioProject: VirtualProject}
				).__browserStudioProject = nextProject;
			}}
			remotionPackageSource={
				source === 'release'
					? {
							baseUrl: new URL(
								`/__remotion_browser_studio_release__/${VERSION}/`,
								window.location.href,
							).href,
							type: 'release',
							version: VERSION,
						}
					: {
							baseUrl: new URL(
								'/__remotion_browser_studio_workspace__/commits/e2e/',
								window.location.href,
							).href,
							commit: 'e2e',
							type: 'workspace',
						}
			}
		/>,
	);
};

render().catch((error) => {
	setTimeout(() => {
		throw error;
	}, 0);
});