🎥 Make videos programmatically with React
remotion.dev/transcribe: progress
Jonny Burgerc7fc9a0dd5a7
diff --git a/packages/convert/app/components/transcribe/App.tsx b/packages/convert/app/components/transcribe/App.tsxindex 52f5ee3101..65b7137477 100644--- a/packages/convert/app/components/transcribe/App.tsx+++ b/packages/convert/app/components/transcribe/App.tsx@@ -1,18 +1,16 @@import type {DownloadWhisperModelParams} from '@remotion/whisper-web';import React, {useState} from 'react';import type {Source} from '~/lib/convert-state';-import type {Caption} from '../../../../captions/dist';import Display from './display';import DownloadModel from './downloadModel';+import type {TranscriptionState} from './state';import TranscribeAudio from './transcribeAudio';const Transcribe: React.FC<{readonly src: Source;readonly name: string;}> = ({src, name}) => {- const [, setTranscriptionCompleted] = useState(false);- const [result, setResult] = useState<Caption[]>([]);- const [transcribing, setTranscribing] = useState(false);+ const [state, setState] = useState<TranscriptionState>({type: 'idle'});const [selectedModel, setSelectedModel] =useState<DownloadWhisperModelParams['model']>('small.en');@@ -21,25 +19,27 @@ const Transcribe: React.FC<{<><div className="h-8 lg:h-0 lg:w-8" /><div className="w-full lg:w-[350px]">- {transcribing ? null : (+ {state.type === 'idle' || state.type === 'initializing' ? (<><DownloadModelselectedModel={selectedModel}setSelectedModel={setSelectedModel}+ disabled={state.type === 'initializing'}/><div className="h-4" /></>- )}+ ) : null}<TranscribeAudiosource={src}- setResult={setResult}- setTranscriptionCompleted={setTranscriptionCompleted}selectedModel={selectedModel}- setTranscribing={setTranscribing}- transcribing={transcribing}name={name}+ state={state}+ setState={setState}/>- {result ? <Display result={result} /> : null}+ <div className="h-4" />+ {state.type === 'done' || state.type === 'transcribing' ? (+ <Display result={state.result} />+ ) : null}</div></>);diff --git a/packages/convert/app/components/transcribe/display.tsx b/packages/convert/app/components/transcribe/display.tsxindex c86fcaa4cd..5fee9d72be 100644--- a/packages/convert/app/components/transcribe/display.tsx+++ b/packages/convert/app/components/transcribe/display.tsx@@ -10,7 +10,10 @@ export default function Display({result}: {readonly result: Caption[]}) {<Card>{result.map((c) => {return (- <div key={c.text} className="border-b border-b-black py-2">+ <div+ key={`${c.text}-${c.startMs}`}+ className="border-b border-b-black py-2"+ ><div className="px-3"><div className="text-xs font-brand font-medium">{(c.startMs / 1000).toFixed(3)} - {(c.endMs / 1000).toFixed(3)}diff --git a/packages/convert/app/components/transcribe/downloadModel.tsx b/packages/convert/app/components/transcribe/downloadModel.tsxindex d198b4173e..4459b01162 100644--- a/packages/convert/app/components/transcribe/downloadModel.tsx+++ b/packages/convert/app/components/transcribe/downloadModel.tsx@@ -20,11 +20,13 @@ import {export default function DownloadModel({selectedModel,setSelectedModel,+ disabled,}: {readonly selectedModel: DownloadWhisperModelParams['model'];readonly setSelectedModel: (model: DownloadWhisperModelParams['model'],) => void;+ readonly disabled: boolean;}) {const [loadedModels, setLoadedModels] = useState<WhisperWebModel[]>([]);const availableModels = getAvailableModels();@@ -60,6 +62,7 @@ export default function DownloadModel({<div className="grid w-full max-w-sm items-center gap-1.5"><Label htmlFor="model">Whisper model</Label><Select+ disabled={disabled}value={selectedModel}onValueChange={(v) =>setSelectedModel(v as DownloadWhisperModelParams['model'])@@ -73,29 +76,31 @@ export default function DownloadModel({{availableModels.map(({name, downloadSize}) => {const isLoaded = loadedModels.includes(name);return (- <SelectItem key={name} value={name}>- <div className="flex w-full items-center justify-between">- <div className="flex flex-col">- <span className="font-medium">{name}</span>- <span className="text-xs text-muted-foreground">- {formatBytes(downloadSize)}- </span>+ <div key={name} className="flex w-full">+ <SelectItem value={name} className="flex w-full">+ <div className="flex w-full justify-between">+ <div className="text-left">+ <div className="font-medium">{name}</div>+ <div className="text-xs text-muted-foreground">+ {formatBytes(downloadSize)}+ </div>+ </div></div>- {isLoaded && (- <button- type="button"- className="ml-2 rounded bg-red-500 px-2 py-1 text-xs text-white hover:bg-red-600 focus:outline-none focus:ring-2 focus:ring-red-500 focus:ring-offset-1"- onClick={(e) => {- e.preventDefault();- e.stopPropagation();- handleDeleteModel(name);- }}- >- Delete- </button>- )}- </div>- </SelectItem>+ </SelectItem>{' '}+ {isLoaded && (+ <button+ type="button"+ className="rounded text-red-500 px-2 py-1 text-xs hover:bg-gray-100 focus:outline-none focus:ring-2 focus:ring-offset-1"+ onClick={(e) => {+ e.preventDefault();+ e.stopPropagation();+ handleDeleteModel(name);+ }}+ >+ Delete+ </button>+ )}+ </div>);})}</SelectGroup>diff --git a/packages/convert/app/components/transcribe/state.ts b/packages/convert/app/components/transcribe/state.tsnew file mode 100644index 0000000000..89f2e0e2b1--- /dev/null+++ b/packages/convert/app/components/transcribe/state.ts@@ -0,0 +1,21 @@+import type {Caption} from '@remotion/captions';+import type {DownloadWhisperModelProgress} from '@remotion/whisper-web';++export type TranscriptionState =+ | {+ type: 'idle';+ }+ | {+ type: 'initializing';+ }+ | {+ type: 'downloading-model';+ progress: DownloadWhisperModelProgress;+ }+ | {+ type: 'transcribing';+ result: Caption[];+ progress: number;+ }+ | {type: 'done'; result: Caption[]}+ | {type: 'error'};diff --git a/packages/convert/app/components/transcribe/transcribeAudio.tsx b/packages/convert/app/components/transcribe/transcribeAudio.tsxindex fad3ee2d92..59e65c364d 100644--- a/packages/convert/app/components/transcribe/transcribeAudio.tsx+++ b/packages/convert/app/components/transcribe/transcribeAudio.tsx@@ -1,19 +1,16 @@-import type {Caption} from '@remotion/captions';-import type {- DownloadWhisperModelProgress,- WhisperWebModel,-} from '@remotion/whisper-web';+import type {WhisperWebModel} from '@remotion/whisper-web';import {downloadWhisperModel,resampleTo16Khz,toCaptions,transcribe,} from '@remotion/whisper-web';-import {useCallback, useState} from 'react';+import {useCallback} from 'react';import type {Source} from '~/lib/convert-state';import {formatBytes} from '~/lib/format-bytes';import {Button} from '../ui/button';import {Card} from '../ui/card';+import type {TranscriptionState} from './state';const sourceToBlob = (source: Source) => {if (source.type === 'file') {@@ -24,28 +21,22 @@ const sourceToBlob = (source: Source) => {};export default function TranscribeAudio({- setResult,- setTranscriptionCompleted,source,selectedModel,- setTranscribing,- transcribing,name,+ state,+ setState,}: {- readonly setResult: React.Dispatch<React.SetStateAction<Caption[]>>;- readonly setTranscriptionCompleted: (completed: boolean) => void;readonly source: Source;readonly selectedModel: WhisperWebModel;- readonly setTranscribing: (transcribing: boolean) => void;- readonly transcribing: boolean;readonly name: string;+ readonly state: TranscriptionState;+ readonly setState: React.Dispatch<React.SetStateAction<TranscriptionState>>;}) {- const [progress, setProgress] = useState(0);- const [modelDownloadProgress, setModelDownloadProgress] =- useState<DownloadWhisperModelProgress | null>(null);-const onClick = useCallback(async () => {- setTranscribing(true);+ setState(() => ({+ type: 'initializing',+ }));const waveform = await resampleTo16Khz({file: await sourceToBlob(source),@@ -54,105 +45,125 @@ export default function TranscribeAudio({await downloadWhisperModel({model: selectedModel,onProgress: (whisperProgress) =>- setModelDownloadProgress(whisperProgress),+ setState(() => ({+ type: 'downloading-model',+ progress: whisperProgress,+ })),});+ setState(() => ({+ type: 'transcribing',+ result: [],+ progress: 0,+ }));+transcribe({channelWaveform: waveform,model: selectedModel,threads: 9,onTranscriptionChunk: (e) => {- setResult((r) => {- const captions = toCaptions({whisperWebOutput: e});- return [...(r ?? []), ...captions.captions];- });+ setState((prevState) => ({+ type: 'transcribing',+ result: [+ ...(prevState.type === 'transcribing' || prevState.type === 'done'+ ? prevState.result+ : []),+ ...toCaptions({whisperWebOutput: e}).captions,+ ],+ progress: 0,+ }));},- onProgress: (p) => setProgress(p),+ onProgress: (p) =>+ setState((prevState) => {+ if (prevState.type !== 'transcribing') {+ return prevState;+ }++ return {+ type: 'transcribing',+ result: prevState.result,+ progress: p,+ };+ }),}).then(() => {- setTranscribing(false);- setTranscriptionCompleted(true);- setProgress(0);+ setState((prevState) => {+ if (prevState.type !== 'transcribing') {+ return prevState;+ }++ return {+ type: 'done',+ result: prevState.result,+ };+ });});- }, [- selectedModel,- source,- setResult,- setTranscribing,- setTranscriptionCompleted,- setProgress,- ]);+ }, [selectedModel, source, setState]);return (<div>- {transcribing ? (- <>- {modelDownloadProgress ? (- <Card className="overflow-hidden">- <>- <div className="h-5 overflow-hidden">- {modelDownloadProgress ? (- <div- className="w-[50%] h-5 bg-brand"- style={{- width:- (modelDownloadProgress.progress ?? 0) * 100 + '%',- }}- />- ) : null}- </div>- <div className="border-b-2 border-black" />- <div className="p-2">- <div>- <strong className="font-brand ">- Downloading model {selectedModel}- </strong>- </div>- <div className="tabular-nums text-muted-foreground font-brand text-sm">- <span>- {Math.round(modelDownloadProgress.progress * 100)}%{' '}- {formatBytes(modelDownloadProgress.downloadedBytes)}- </span>- </div>- </div>- </>- </Card>- ) : null}- <div className="h-4" />- <Card className="overflow-hidden">- <>- <div className="h-5 overflow-hidden">- {progress ? (- <div- className="w-[50%] h-5 bg-brand"- style={{- width: (progress ?? 0) * 100 + '%',- }}- />- ) : null}+ {state.type === 'downloading-model' ? (+ <Card className="overflow-hidden">+ <>+ <div className="h-5 overflow-hidden">+ <div+ className="w-[50%] h-5 bg-brand"+ style={{+ width: (state.progress.progress ?? 0) * 100 + '%',+ }}+ />+ </div>+ <div className="border-b-2 border-black" />+ <div className="p-2">+ <div>+ <strong className="font-brand ">+ Downloading model {selectedModel}+ </strong>+ </div>+ <div className="tabular-nums text-muted-foreground font-brand text-sm">+ <span>+ {Math.round(state.progress.progress * 100)}%{' '}+ {formatBytes(state.progress.downloadedBytes)}+ </span>+ </div>+ </div>+ </>+ </Card>+ ) : null}+ {state.type === 'transcribing' ? (+ <Card className="overflow-hidden">+ <>+ <div className="h-5 overflow-hidden">+ <div+ className="w-[50%] h-5 bg-brand"+ style={{+ width: (state.progress ?? 0) * 100 + '%',+ }}+ />+ </div>+ <div className="border-b-2 border-black" />+ <div className="p-2">+ <div>+ <strong className="font-brand ">Transcribing {name}</strong></div>- <div className="border-b-2 border-black" />- <div className="p-2">- <div>- <strong className="font-brand ">Transcribing {name}</strong>- </div>- <div className="tabular-nums text-muted-foreground font-brand text-sm">- <span>{Math.round(progress * 100)}%</span>- </div>+ <div className="tabular-nums text-muted-foreground font-brand text-sm">+ <span>{Math.round(state.progress * 100)}%</span></div>- </>- </Card>- </>- ) : (+ </div>+ </>+ </Card>+ ) : null}+ {state.type === 'idle' || state.type === 'initializing' ? (<Buttontype="button"- disabled={transcribing}- className="block w-full"+ className="block w-full disabled:opacity-50"variant="brand"+ disabled={state.type === 'initializing'}onClick={onClick}+ data-disabled={state.type === 'initializing'}>- Transcribe+ {state.type === 'initializing' ? 'Initializing...' : 'Transcribe'}</Button>- )}+ ) : null}</div>);}