Skip to content

πŸŽ₯ Make videos programmatically with React

cool

Jonny Burger81af4f2565a3
diff --git a/packages/convert/app/components/FileAvailable.tsx b/packages/convert/app/components/FileAvailable.tsx
index 29b2fe29e0..48e0978ea5 100644
--- a/packages/convert/app/components/FileAvailable.tsx
+++ b/packages/convert/app/components/FileAvailable.tsx
@@ -125,7 +125,9 @@ export const FileAvailable: React.FC<{
</div>
</>
) : null}
- {routeAction.type === 'transcribe' ? <Transcribe /> : null}
+ {routeAction.type === 'transcribe' ? (
+ <Transcribe src={src} name={probeResult.name ?? ''} />
+ ) : null}
</div>
<div className="h-16" />
<Footer routeAction={routeAction} />
diff --git a/packages/convert/app/components/WhyRemotionConvert.tsx b/packages/convert/app/components/WhyRemotionConvert.tsx
index 8dd3cb3eff..ae3144e126 100644
--- a/packages/convert/app/components/WhyRemotionConvert.tsx
+++ b/packages/convert/app/components/WhyRemotionConvert.tsx
@@ -61,7 +61,7 @@ export const WhyRemotionConvert: React.FC = () => {
</div>
</div>
<div className="h-20" />
- <Footer />
+ <Footer routeAction={{type: 'generic-convert'}} />
</div>
<div className="h-10" />
</div>
diff --git a/packages/convert/app/components/transcribe/App.tsx b/packages/convert/app/components/transcribe/App.tsx
index 57f1f58254..52f5ee3101 100644
--- a/packages/convert/app/components/transcribe/App.tsx
+++ b/packages/convert/app/components/transcribe/App.tsx
@@ -1,35 +1,45 @@
-import type {TranscriptionJson} from '@remotion/whisper-web';
-import {useState} from 'react';
+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 TranscribeAudio from './transcribeAudio';
-const Transcribe = () => {
- const [transcriptionCompleted, setTranscriptionCompleted] = useState(false);
- const [modelDownloading, setModelDownloading] = useState(false);
- const [result, setResult] = useState<TranscriptionJson | null>(null);
- const [file, setFile] = useState<File | null>(null);
+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 [selectedModel, setSelectedModel] =
+ useState<DownloadWhisperModelParams['model']>('small.en');
return (
<>
<div className="h-8 lg:h-0 lg:w-8" />
<div className="w-full lg:w-[350px]">
- {transcriptionCompleted && result ? (
- <Display result={result} />
- ) : (
+ {transcribing ? null : (
<>
- <DownloadModel setDownloadingModel={setModelDownloading} />
- <TranscribeAudio
- {...{
- setFile,
- setResult,
- modelDownloading,
- file,
- setTranscriptionCompleted,
- }}
+ <DownloadModel
+ selectedModel={selectedModel}
+ setSelectedModel={setSelectedModel}
/>
+ <div className="h-4" />
</>
)}
+ <TranscribeAudio
+ source={src}
+ setResult={setResult}
+ setTranscriptionCompleted={setTranscriptionCompleted}
+ selectedModel={selectedModel}
+ setTranscribing={setTranscribing}
+ transcribing={transcribing}
+ name={name}
+ />
+ {result ? <Display result={result} /> : null}
</div>
</>
);
diff --git a/packages/convert/app/components/transcribe/display.tsx b/packages/convert/app/components/transcribe/display.tsx
index 307faa7238..c86fcaa4cd 100644
--- a/packages/convert/app/components/transcribe/display.tsx
+++ b/packages/convert/app/components/transcribe/display.tsx
@@ -1,19 +1,14 @@
-import {toCaptions, type TranscriptionJson} from '@remotion/whisper-web';
-import {useMemo} from 'react';
+import type {Caption} from '@remotion/captions';
import {Card} from '../ui/card';
-export default function Display({
- result,
-}: {
- readonly result: TranscriptionJson;
-}) {
- const remotionCaptions = useMemo(() => {
- return toCaptions({whisperWebOutput: result!});
- }, [result]);
+export default function Display({result}: {readonly result: Caption[]}) {
+ if (result.length === 0) {
+ return null;
+ }
return (
<Card>
- {remotionCaptions.captions.map((c, i) => {
+ {result.map((c) => {
return (
<div key={c.text} className="border-b border-b-black py-2">
<div className="px-3">
diff --git a/packages/convert/app/components/transcribe/downloadModel.tsx b/packages/convert/app/components/transcribe/downloadModel.tsx
index 1db8410206..26e434dce3 100644
--- a/packages/convert/app/components/transcribe/downloadModel.tsx
+++ b/packages/convert/app/components/transcribe/downloadModel.tsx
@@ -1,9 +1,4 @@
-import {
- downloadWhisperModel,
- type DownloadWhisperModelParams,
-} from '@remotion/whisper-web';
-import {useState} from 'react';
-import {Button} from '../ui/button';
+import {type DownloadWhisperModelParams} from '@remotion/whisper-web';
import {Label} from '../ui/label';
import {
Select,
@@ -15,9 +10,13 @@ import {
} from '../ui/select';
export default function DownloadModel({
- setDownloadingModel,
+ selectedModel,
+ setSelectedModel,
}: {
- readonly setDownloadingModel: (downloading: boolean) => void;
+ readonly selectedModel: DownloadWhisperModelParams['model'];
+ readonly setSelectedModel: (
+ model: DownloadWhisperModelParams['model'],
+ ) => void;
}) {
const modelOptions: DownloadWhisperModelParams['model'][] = [
'tiny.en',
@@ -28,10 +27,6 @@ export default function DownloadModel({
'base.en',
];
- const [selectedModel, setSelectedModel] =
- useState<DownloadWhisperModelParams['model']>('tiny.en');
- const [downloadModelProgress, setDownloadProgress] = useState<number>(0);
-
return (
<div className="flex flex-col gap-5">
<div className="flex items-end gap-2">
@@ -57,23 +52,7 @@ export default function DownloadModel({
</SelectContent>
</Select>
</div>
- <Button
- variant="secondary"
- onClick={() => {
- setDownloadingModel(true);
- downloadWhisperModel({
- model: selectedModel,
- onProgress: (v) => setDownloadProgress(v.progress),
- }).then(() => setDownloadingModel(false));
- }}
- >
- Download model
- </Button>
</div>
- {downloadModelProgress !== 0 && downloadModelProgress !== 1 && (
- <progress value={downloadModelProgress} max={1} />
- )}
- {downloadModelProgress === 1 && <span>done βœ…</span>}
</div>
);
}
diff --git a/packages/convert/app/components/transcribe/selectTranscriptionModel.tsx b/packages/convert/app/components/transcribe/selectTranscriptionModel.tsx
deleted file mode 100644
index 356129502b..0000000000
--- a/packages/convert/app/components/transcribe/selectTranscriptionModel.tsx
+++ /dev/null
@@ -1,65 +0,0 @@
-import {
- getLoadedModels,
- type DownloadWhisperModelParams,
-} from '@remotion/whisper-web';
-import {useEffect, useState} from 'react';
-
-interface SelectTranscriptionModelProps {
- readonly loadingLoadedModels: boolean;
- readonly setLoadingLoadedModels: (value: boolean) => void;
- readonly setModelForTranscription: (
- value: DownloadWhisperModelParams['model'] | undefined,
- ) => void;
- readonly modelDownloading: boolean;
- readonly transcribing: boolean;
-}
-
-export default function SelectTranscriptionModel({
- loadingLoadedModels,
- modelDownloading,
- setLoadingLoadedModels,
- transcribing,
- setModelForTranscription,
-}: SelectTranscriptionModelProps) {
- const [modelsAvailable, setModelsAvailable] = useState<
- DownloadWhisperModelParams['model'][]
- >(['tiny.en']);
- useEffect(() => {
- if (modelDownloading) {
- return;
- }
-
- const loader = () => {
- setLoadingLoadedModels(true);
- getLoadedModels().then((data) => {
- setModelsAvailable(data);
- setLoadingLoadedModels(false);
- if (data.length === 0) {
- setModelForTranscription(undefined);
- } else {
- setModelForTranscription(data[0]);
- }
- });
- };
-
- loader();
-
- // eslint-disable-next-line
- }, [modelDownloading]);
- return (
- <select
- onChange={(e) =>
- setModelForTranscription(
- e.target.value as DownloadWhisperModelParams['model'],
- )
- }
- disabled={loadingLoadedModels || transcribing}
- >
- {modelsAvailable.map((model) => (
- <option key={model} value={model}>
- {model}
- </option>
- ))}
- </select>
- );
-}
diff --git a/packages/convert/app/components/transcribe/transcribeAudio.tsx b/packages/convert/app/components/transcribe/transcribeAudio.tsx
index 352335f4f0..fad3ee2d92 100644
--- a/packages/convert/app/components/transcribe/transcribeAudio.tsx
+++ b/packages/convert/app/components/transcribe/transcribeAudio.tsx
@@ -1,148 +1,158 @@
-/* eslint-disable no-console */
+import type {Caption} from '@remotion/captions';
+import type {
+ DownloadWhisperModelProgress,
+ WhisperWebModel,
+} from '@remotion/whisper-web';
import {
+ downloadWhisperModel,
resampleTo16Khz,
+ toCaptions,
transcribe,
- type DownloadWhisperModelParams,
- type TranscriptionJson,
} from '@remotion/whisper-web';
-import {useEffect, useRef, useState} from 'react';
+import {useCallback, useState} from 'react';
+import type {Source} from '~/lib/convert-state';
+import {formatBytes} from '~/lib/format-bytes';
import {Button} from '../ui/button';
-import SelectTranscriptionModel from './selectTranscriptionModel';
+import {Card} from '../ui/card';
+
+const sourceToBlob = (source: Source) => {
+ if (source.type === 'file') {
+ return source.file;
+ }
+
+ return fetch(source.url).then((r) => r.blob());
+};
export default function TranscribeAudio({
setResult,
setTranscriptionCompleted,
- setFile,
- file,
- modelDownloading,
+ source,
+ selectedModel,
+ setTranscribing,
+ transcribing,
+ name,
}: {
- readonly setResult: (result: TranscriptionJson | null) => void;
+ readonly setResult: React.Dispatch<React.SetStateAction<Caption[]>>;
readonly setTranscriptionCompleted: (completed: boolean) => void;
- readonly setFile: (file: File) => void;
- readonly file: File | null;
- readonly modelDownloading: boolean;
+ readonly source: Source;
+ readonly selectedModel: WhisperWebModel;
+ readonly setTranscribing: (transcribing: boolean) => void;
+ readonly transcribing: boolean;
+ readonly name: string;
}) {
- const fileref = useRef<HTMLInputElement>(null);
- const [waveForm, setWaveForm] = useState<Float32Array>(new Float32Array());
- const [loadingLoadedModels, setLoadingLoadedModels] = useState(false);
- const [modelForTranscription, setModelForTranscription] = useState<
- DownloadWhisperModelParams['model'] | undefined
- >(undefined);
- const [transcribing, setTranscribing] = useState(false);
const [progress, setProgress] = useState(0);
+ const [modelDownloadProgress, setModelDownloadProgress] =
+ useState<DownloadWhisperModelProgress | null>(null);
- useEffect(() => {}, [modelForTranscription]);
- useEffect(() => {
- // sync the file between two input sources
- if (!file || !fileref?.current) {
- return;
- }
-
- if (file && fileref.current.files && fileref.current.files.length === 0) {
- const dataTransfer = new DataTransfer();
- dataTransfer.items.add(file);
- fileref.current.files = dataTransfer.files;
- }
- }, [fileref, file]);
+ const onClick = useCallback(async () => {
+ setTranscribing(true);
- return (
- <div
- style={{
- gap: 15,
- alignItems: 'center',
- }}
- >
- <div
- style={{
- display: 'flex',
- flexFlow: 'column',
- gap: 10,
- }}
- >
- <input
- ref={fileref}
- type="file"
- onChange={(ch: React.ChangeEvent<HTMLInputElement>) => {
- setResult(null);
- setTranscriptionCompleted(false);
- const _file = ch.target?.files?.[0];
- if (_file) {
- setFile(_file);
- resampleTo16Khz({
- file: _file,
-
- onProgress: (e) => console.log(e),
- }).then((waveform) => setWaveForm(waveform));
- }
- }}
- />
+ const waveform = await resampleTo16Khz({
+ file: await sourceToBlob(source),
+ });
- <div
- onDrop={(e) => {
- e.preventDefault();
- setResult(null);
- setTranscriptionCompleted(false);
- const _file = e.dataTransfer.files?.[0];
- if (_file) {
- setFile(_file);
- resampleTo16Khz({
- file: _file,
+ await downloadWhisperModel({
+ model: selectedModel,
+ onProgress: (whisperProgress) =>
+ setModelDownloadProgress(whisperProgress),
+ });
- onProgress: (err) => console.log(err),
- }).then((waveform) => setWaveForm(waveform));
- }
- }}
- onDragOver={(e) => e.preventDefault()}
- style={{
- border: '2px dashed rgb(230, 230, 230, 0.5)',
- padding: 2,
- marginBottom: 8,
- }}
- >
- {file ? `Selected file: ${file.name}` : 'Drop here'}
- </div>
- </div>
- <div style={{display: 'grid', gridAutoFlow: 'row', gap: 10}}>
- <SelectTranscriptionModel
- transcribing={transcribing}
- modelDownloading={modelDownloading}
- setModelForTranscription={setModelForTranscription}
- setLoadingLoadedModels={setLoadingLoadedModels}
- loadingLoadedModels={loadingLoadedModels}
- />
+ transcribe({
+ channelWaveform: waveform,
+ model: selectedModel,
+ threads: 9,
+ onTranscriptionChunk: (e) => {
+ setResult((r) => {
+ const captions = toCaptions({whisperWebOutput: e});
+ return [...(r ?? []), ...captions.captions];
+ });
+ },
+ onProgress: (p) => setProgress(p),
+ }).then(() => {
+ setTranscribing(false);
+ setTranscriptionCompleted(true);
+ setProgress(0);
+ });
+ }, [
+ selectedModel,
+ source,
+ setResult,
+ setTranscribing,
+ setTranscriptionCompleted,
+ setProgress,
+ ]);
+ 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}
+ </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>
+ </>
+ </Card>
+ </>
+ ) : (
<Button
type="button"
- disabled={
- modelDownloading ||
- !modelForTranscription ||
- transcribing ||
- !file ||
- loadingLoadedModels
- }
+ disabled={transcribing}
className="block w-full"
variant="brand"
- onClick={() => {
- if (!modelForTranscription) return;
- setTranscribing(true);
- transcribe({
- channelWaveform: waveForm,
- model: modelForTranscription,
- threads: 9,
- onTranscriptionChunk: (e) => console.log(e),
- onProgress: (p) => setProgress(p),
- }).then((result) => {
- setResult(result);
- setTranscribing(false);
- setTranscriptionCompleted(true);
- setProgress(0);
- });
- }}
+ onClick={onClick}
>
Transcribe
</Button>
- {transcribing && <progress max={1} value={progress} />}
- </div>
+ )}
</div>
);
}
diff --git a/packages/convert/package.json b/packages/convert/package.json
index 6435fa9826..8161bb58a4 100644
--- a/packages/convert/package.json
+++ b/packages/convert/package.json
@@ -25,6 +25,7 @@
"@remix-run/node": "^2.15.2",
"@remix-run/react": "^2.15.2",
"@remix-run/serve": "^2.15.2",
+ "@remotion/captions": "workspace:*",
"@remotion/media-parser": "workspace:*",
"@remotion/whisper-web": "workspace:*",
"@remotion/shapes": "workspace:*",
diff --git a/packages/whisper-web/src/to-captions.ts b/packages/whisper-web/src/to-captions.ts
index a84737890f..717e21b65e 100644
--- a/packages/whisper-web/src/to-captions.ts
+++ b/packages/whisper-web/src/to-captions.ts
@@ -1,8 +1,8 @@
import type {Caption} from '@remotion/captions';
-import type {TranscriptionJson} from './result';
+import type {TranscriptionItemWithTimestamp, TranscriptionJson} from './result';
type ToCaptionsInput = {
- whisperWebOutput: TranscriptionJson;
+ whisperWebOutput: TranscriptionJson | TranscriptionItemWithTimestamp[];
};
type ToCaptionsOutput = {
@@ -10,7 +10,10 @@ type ToCaptionsOutput = {
};
export const toCaptions = (input: ToCaptionsInput): ToCaptionsOutput => {
- const {transcription} = input.whisperWebOutput;
+ const transcription =
+ 'transcription' in input.whisperWebOutput
+ ? input.whisperWebOutput.transcription
+ : input.whisperWebOutput;
const captions: Caption[] = [];
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 411ae83c91..5ea281c9e0 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -394,6 +394,9 @@ importers:
'@remix-run/serve':
specifier: ^2.15.2
version: 2.15.2([email protected])
+ '@remotion/captions':
+ specifier: workspace:*
+ version: link:../captions
'@remotion/media-parser':
specifier: workspace:*
version: link:../media-parser