🎥 Make videos programmatically with React
remotion.dev/convert: Add transcription JSON downloads
Jonny Burger0c7238f2e18a
diff --git a/packages/convert/app/components/transcribe/state.ts b/packages/convert/app/components/transcribe/state.tsindex ee0ea31c56..4f378bc754 100644--- a/packages/convert/app/components/transcribe/state.ts+++ b/packages/convert/app/components/transcribe/state.ts@@ -1,5 +1,8 @@import type {Caption} from '@remotion/captions';-import type {WhisperWebGpuModelLoadProgress} from '@remotion/whisper-webgpu';+import type {+ WhisperWebGpuModelLoadProgress,+ WhisperWebGpuTranscription,+} from '@remotion/whisper-webgpu';export type TranscriptionState =| {@@ -18,5 +21,6 @@ export type TranscriptionState =| {type: 'done';result: Caption[];+ whisperWebGpuOutput: WhisperWebGpuTranscription;}| {type: 'error'; message: string};diff --git a/packages/convert/app/components/transcribe/transcribeAudio.tsx b/packages/convert/app/components/transcribe/transcribeAudio.tsxindex 93eebc2ca4..159b68b9db 100644--- a/packages/convert/app/components/transcribe/transcribeAudio.tsx+++ b/packages/convert/app/components/transcribe/transcribeAudio.tsx@@ -20,6 +20,17 @@ const sourceToBlob = (source: Source) => {return fetch(source.url).then((r) => r.blob());};+const downloadJson = (value: unknown, filename: string) => {+ const blob = new Blob([JSON.stringify(value, null, 2)], {+ type: 'application/json',+ });+ const a = document.createElement('a');+ a.href = URL.createObjectURL(blob);+ a.download = filename;+ a.click();+ URL.revokeObjectURL(a.href);+};+export default function TranscribeAudio({source,selectedModel,@@ -64,6 +75,7 @@ export default function TranscribeAudio({setState(() => ({type: 'done',result: toCaptions({whisperWebGpuOutput: transcription}).captions,+ whisperWebGpuOutput: transcription,}));} catch (error) {setState({@@ -131,6 +143,26 @@ export default function TranscribeAudio({Transcribed with WebGPU{state.result.length === 0 ? ' · No speech detected' : null}</Card>+ <div className="h-2" />+ <Button+ type="button"+ className="block w-full"+ variant="brand"+ onClick={() => downloadJson(state.result, 'captions.json')}+ >+ Download captions.json+ </Button>+ <div className="h-2" />+ <Button+ type="button"+ className="block w-full"+ variant="brandsecondary"+ onClick={() =>+ downloadJson(state.whisperWebGpuOutput, 'whisper-output.json')+ }+ >+ Download raw Whisper output+ </Button><div className="h-4" /></>) : null}diff --git a/packages/convert/test/transcribe-webgpu.test.tsx b/packages/convert/test/transcribe-webgpu.test.tsxindex 41f7a3b2e4..dc4c2d6f4d 100644--- a/packages/convert/test/transcribe-webgpu.test.tsx+++ b/packages/convert/test/transcribe-webgpu.test.tsx@@ -1,6 +1,7 @@import {afterEach, expect, test} from 'bun:test';-import {cleanup, render} from '@testing-library/react';+import {cleanup, fireEvent, render} from '@testing-library/react';import ModelSelector from '../app/components/transcribe/modelSelector';+import TranscribeAudio from '../app/components/transcribe/transcribeAudio';afterEach(() => cleanup());@@ -18,3 +19,90 @@ test('offers WebGPU model downloads', () => {expect(model.textContent).toContain('119.7 MB WebGPU');expect(model.textContent).toContain('Downloaded');});++test('downloads captions and raw Whisper output as JSON', async () => {+ const originalCreateObjectUrl = URL.createObjectURL;+ const originalRevokeObjectUrl = URL.revokeObjectURL;+ const originalAnchorClick = HTMLAnchorElement.prototype.click;+ const blobs: Blob[] = [];+ const revokedUrls: string[] = [];+ const downloads: Array<{filename: string; url: string}> = [];++ URL.createObjectURL = (blob) => {+ if (!(blob instanceof Blob)) {+ throw new Error('Expected a Blob');+ }++ blobs.push(blob);+ return `blob:transcription-${blobs.length}`;+ };+ URL.revokeObjectURL = (url) => revokedUrls.push(url);+ HTMLAnchorElement.prototype.click = function () {+ downloads.push({filename: this.download, url: this.href});+ };++ const captions = [+ {+ text: 'Hello',+ startMs: 0,+ endMs: 500,+ timestampMs: 250,+ confidence: null,+ },+ ];+ const whisperWebGpuOutput = {+ text: 'Hello',+ words: [+ {+ text: 'Hello',+ startInSeconds: 0,+ endInSeconds: 0.5,+ },+ ],+ model: 'tiny.en' as const,+ };++ try {+ const rendered = render(+ <TranscribeAudio+ source={{type: 'file', file: new File([], 'audio.mp3')}}+ selectedModel="tiny.en"+ name="audio.mp3"+ state={{+ type: 'done',+ result: captions,+ whisperWebGpuOutput,+ }}+ setState={() => undefined}+ />,+ );++ fireEvent.click(+ rendered.getByRole('button', {name: 'Download captions.json'}),+ );+ fireEvent.click(+ rendered.getByRole('button', {name: 'Download raw Whisper output'}),+ );++ expect(downloads).toEqual([+ {filename: 'captions.json', url: 'blob:transcription-1'},+ {filename: 'whisper-output.json', url: 'blob:transcription-2'},+ ]);+ expect(blobs.map((blob) => blob.type)).toEqual([+ 'application/json',+ 'application/json',+ ]);+ expect(await blobs[0]?.text()).toBe(JSON.stringify(captions, null, 2));+ expect(await blobs[1]?.text()).toBe(+ JSON.stringify(whisperWebGpuOutput, null, 2),+ );+ expect(revokedUrls).toEqual([+ 'blob:transcription-1',+ 'blob:transcription-2',+ ]);+ } finally {+ URL.createObjectURL = originalCreateObjectUrl;+ URL.revokeObjectURL = originalRevokeObjectUrl;+ HTMLAnchorElement.prototype.click = originalAnchorClick;+ }+});