🎥 Make videos programmatically with React
Add model management to Studio settings
JonnyBurgere5db7996b7f0
diff --git a/packages/studio/src/components/InstallPackage.tsx b/packages/studio/src/components/InstallPackage.tsxindex 4d01fc00b5..534d63d4b7 100644--- a/packages/studio/src/components/InstallPackage.tsx+++ b/packages/studio/src/components/InstallPackage.tsx@@ -21,6 +21,7 @@ import {Flex, Row} from './layout';import {VERTICAL_SCROLLBAR_CLASSNAME} from './Menu/is-menu-item';import {ModalButton} from './ModalButton';import {ModalFooterContainer} from './ModalFooter';+import {markOptionalPackageInstalled} from './OptionalPackageModal';const container: React.CSSProperties = {boxSizing: 'border-box',@@ -110,12 +111,10 @@ export const InstallPackageSettings: React.FC<{setState({type: 'installing'});try {await installPackages(selectedPackageSpecs);- window.remotion_installedPackages = Array.from(- new Set([- ...(window.remotion_installedPackages ?? []),- ...selectedPackages,- ]),- );+ for (const packageName of selectedPackages) {+ markOptionalPackageInstalled(packageName);+ }+setState({type: 'done'});} catch (err) {setState({type: 'error', error: err as Error});diff --git a/packages/studio/src/components/ModelsSettings.tsx b/packages/studio/src/components/ModelsSettings.tsxnew file mode 100644index 0000000000..3535ccc6a9--- /dev/null+++ b/packages/studio/src/components/ModelsSettings.tsx@@ -0,0 +1,160 @@+import React, {Suspense, useCallback, useState} from 'react';+import {BLUE, LIGHT_TEXT, WARNING_COLOR, WHITE} from '../helpers/colors';+import {Button} from './Button';+import {+ installOptionalPackage,+ useOptionalPackageInstalled,+} from './OptionalPackageModal';+import {WHISPER_WEBGPU_PACKAGE} from './Transcription/whisper-webgpu-capability';+import {VIDEO_MATTING_PACKAGE} from './VideoMatting/video-matting-capability';++const LazyWhisperModels = React.lazy(async () => {+ const {Models} = await import('./Transcription/Models');+ return {default: Models};+});++const LazyVideoMattingModels = React.lazy(async () => {+ const {Models} = await import('./VideoMatting/Models');+ return {default: Models};+});++const container: React.CSSProperties = {+ alignSelf: 'flex-start',+ boxSizing: 'border-box',+ fontFamily: 'sans-serif',+ minWidth: 0,+ padding: 16,+ width: '100%',+};++const section: React.CSSProperties = {+ borderBottom: '1px solid rgba(255, 255, 255, 0.12)',+ paddingBottom: 16,+};++const lastSection: React.CSSProperties = {+ paddingTop: 16,+};++const title: React.CSSProperties = {+ color: WHITE,+ fontSize: 14,+ fontWeight: 600,+ lineHeight: 1.4,+ margin: 0,+};++const packageNameStyle: React.CSSProperties = {+ color: LIGHT_TEXT,+ fontFamily: 'monospace',+ fontSize: 12,+ lineHeight: 1.4,+ margin: '2px 0 0',+};++const missingRow: React.CSSProperties = {+ alignItems: 'center',+ display: 'flex',+ gap: 12,+ justifyContent: 'space-between',+ marginTop: 14,+};++const missingText: React.CSSProperties = {+ color: LIGHT_TEXT,+ fontSize: 13,+ lineHeight: 1.5,+ margin: 0,+};++const installButton: React.CSSProperties = {+ backgroundColor: BLUE,+ color: WHITE,+ flexShrink: 0,+};++const errorStyle: React.CSSProperties = {+ color: WARNING_COLOR,+ fontSize: 12,+ lineHeight: 1.4,+ margin: '8px 0 0',+};++type InstallState =+ | {type: 'idle'}+ | {type: 'installing'}+ | {type: 'error'; message: string};++const OptionalModelPackage: React.FC<{+ readonly children: React.ReactNode;+ readonly label: string;+ readonly packageName: string;+ readonly style: React.CSSProperties;+}> = ({children, label, packageName, style}) => {+ const installed = useOptionalPackageInstalled(packageName);+ const [installState, setInstallState] = useState<InstallState>({+ type: 'idle',+ });++ const install = useCallback(() => {+ setInstallState({type: 'installing'});+ installOptionalPackage(packageName).catch((error) => {+ setInstallState({+ type: 'error',+ message: error instanceof Error ? error.message : String(error),+ });+ });+ }, [packageName]);++ return (+ <section style={style}>+ <h3 style={title}>{label}</h3>+ <p style={packageNameStyle}>{packageName}</p>+ {installed ? (+ <Suspense fallback={<p style={missingText}>Loading models…</p>}>+ {children}+ </Suspense>+ ) : (+ <>+ <div style={missingRow}>+ <p style={missingText}>+ Install the package to download and manage its models.+ </p>+ <Button+ disabled={installState.type === 'installing'}+ onClick={install}+ size="compact"+ style={installButton}+ >+ {installState.type === 'installing' ? 'Installing…' : 'Install'}+ </Button>+ </div>+ {installState.type === 'error' ? (+ <p style={errorStyle}>{installState.message}</p>+ ) : null}+ </>+ )}+ </section>+ );+};++export const ModelsSettings: React.FC = () => {+ return (+ <div style={container}>+ <OptionalModelPackage+ label="Transcription"+ packageName={WHISPER_WEBGPU_PACKAGE}+ style={section}+ >+ <LazyWhisperModels visible />+ </OptionalModelPackage>+ <OptionalModelPackage+ label="Video matting"+ packageName={VIDEO_MATTING_PACKAGE}+ style={lastSection}+ >+ <LazyVideoMattingModels visible />+ </OptionalModelPackage>+ </div>+ );+};diff --git a/packages/studio/src/components/OptionalPackageModal.tsx b/packages/studio/src/components/OptionalPackageModal.tsxindex 43c06c0361..aa8f75e60b 100644--- a/packages/studio/src/components/OptionalPackageModal.tsx+++ b/packages/studio/src/components/OptionalPackageModal.tsx@@ -56,6 +56,22 @@ export const isOptionalPackageInstalled = (packageName: string) =>const OPTIONAL_PACKAGE_INSTALLED_EVENT = 'remotion-optional-package-installed';+export const markOptionalPackageInstalled = (packageName: string) => {+ window.remotion_installedPackages = Array.from(+ new Set([...(window.remotion_installedPackages ?? []), packageName]),+ );+ window.dispatchEvent(+ new CustomEvent(OPTIONAL_PACKAGE_INSTALLED_EVENT, {+ detail: packageName,+ }),+ );+};++export const installOptionalPackage = async (packageName: string) => {+ await installPackages([{name: packageName, version: null}]);+ markOptionalPackageInstalled(packageName);+};+export const useOptionalPackageInstalled = (packageName: string) => {const [installed, setInstalled] = useState(() =>isOptionalPackageInstalled(packageName),@@ -98,15 +114,7 @@ export const OptionalPackageModal: React.FC<{const install = useCallback(async () => {setInstallState({type: 'installing'});try {- await installPackages([{name: packageName, version: null}]);- window.remotion_installedPackages = Array.from(- new Set([...(window.remotion_installedPackages ?? []), packageName]),- );- window.dispatchEvent(- new CustomEvent(OPTIONAL_PACKAGE_INSTALLED_EVENT, {- detail: packageName,- }),- );+ await installOptionalPackage(packageName);} catch (error) {setInstallState({type: 'error', error: error as Error});}diff --git a/packages/studio/src/components/SettingsModal.tsx b/packages/studio/src/components/SettingsModal.tsxindex f2c808c929..e1a0ae992c 100644--- a/packages/studio/src/components/SettingsModal.tsx+++ b/packages/studio/src/components/SettingsModal.tsx@@ -13,6 +13,7 @@ import {canShowUpdates} from '../helpers/can-show-updates';import {StudioServerConnectionCtx} from '../helpers/client-id';import {AppsIcon} from '../icons/apps';import {CloudDownloadIcon} from '../icons/cloud-download';+import {CubeIcon} from '../icons/cube';import {KeyboardIcon} from '../icons/keyboard';import {LicenseIcon} from '../icons/license';import {PackageIcon} from '../icons/package';@@ -26,6 +27,7 @@ import {InstallPackageSettings} from './InstallPackage';import {KeyboardShortcutsSettings} from './KeyboardShortcutsSettings';import {VERTICAL_SCROLLBAR_CLASSNAME} from './Menu/is-menu-item';import {ModalHeader} from './ModalHeader';+import {ModelsSettings} from './ModelsSettings';import {DismissableModal} from './NewComposition/DismissableModal';import {RenderingSettings} from './RenderingSettings';import {@@ -50,6 +52,7 @@ type SettingsTab =| 'rendering'| 'studio'| 'packages'+ | 'models'| 'shortcuts'| 'skills'| 'license'@@ -201,6 +204,18 @@ export const SettingsModal: React.FC<{Packages</VerticalTab>) : null}+ <VerticalTab+ style={horizontalTab}+ selected={tab === 'models'}+ onClick={() => selectTab('models')}+ renderIcon={(color) => (+ <div style={iconContainer}>+ <CubeIcon color={color} style={icon} />+ </div>+ )}+ >+ Models+ </VerticalTab>{isBrowserStudio ? null : (<VerticalTabstyle={horizontalTab}@@ -274,6 +289,14 @@ export const SettingsModal: React.FC<{<DefaultEditorSettings /></div>) : null}+ {openedTabs.includes('models') ? (+ <div+ style={tab === 'models' ? settingsOptionsPanel : hiddenPanel}+ className={VERTICAL_SCROLLBAR_CLASSNAME}+ >+ <ModelsSettings />+ </div>+ ) : null}{openedTabs.includes('license') ? (<divstyle={tab === 'license' ? settingsOptionsPanel : hiddenPanel}diff --git a/packages/studio/src/state/modals.ts b/packages/studio/src/state/modals.tsindex bfd2abad41..e5954ead73 100644--- a/packages/studio/src/state/modals.ts+++ b/packages/studio/src/state/modals.ts@@ -230,6 +230,7 @@ export type ModalState =| 'rendering'| 'studio'| 'packages'+ | 'models'| 'shortcuts'| 'skills'| 'updates'diff --git a/packages/studio/src/test/models-settings.test.tsx b/packages/studio/src/test/models-settings.test.tsxnew file mode 100644index 0000000000..6186d390ac--- /dev/null+++ b/packages/studio/src/test/models-settings.test.tsx@@ -0,0 +1,45 @@+import {afterEach, expect, spyOn, test} from 'bun:test';+import {cleanup, fireEvent, render, waitFor} from '@testing-library/react';+import {ModelsSettings} from '../components/ModelsSettings';+import {WHISPER_WEBGPU_PACKAGE} from '../components/Transcription/whisper-webgpu-capability';+import {VIDEO_MATTING_PACKAGE} from '../components/VideoMatting/video-matting-capability';++const originalInstalledPackages = window.remotion_installedPackages;+const originalIsStudio = window.remotion_isStudio;+const originalStaticBase = window.remotion_staticBase;++afterEach(() => {+ cleanup();+ window.remotion_installedPackages = originalInstalledPackages;+ window.remotion_isStudio = originalIsStudio;+ window.remotion_staticBase = originalStaticBase;+});++test('offers to install missing model packages and loads models in place', async () => {+ window.remotion_installedPackages = [];+ window.remotion_isStudio = true;+ window.remotion_staticBase = '';+ const fetchSpy = spyOn(globalThis, 'fetch').mockResolvedValue(+ new Response(JSON.stringify({success: true, data: {}})),+ );++ try {+ const {getAllByRole, getByRole} = render(<ModelsSettings />);+ expect(getAllByRole('button', {name: 'Install'})).toHaveLength(2);++ fireEvent.click(getAllByRole('button', {name: 'Install'})[0]);+ await waitFor(() => {+ expect(window.remotion_installedPackages).toContain(+ WHISPER_WEBGPU_PACKAGE,+ );+ expect(getByRole('list', {name: 'Whisper models'})).toBeDefined();+ });+ expect(window.remotion_installedPackages).not.toContain(+ VIDEO_MATTING_PACKAGE,+ );+ expect(getAllByRole('button', {name: 'Install'})).toHaveLength(1);+ expect(fetchSpy).toHaveBeenCalledTimes(1);+ } finally {+ fetchSpy.mockRestore();+ }+});