Skip to content

🎥 Make videos programmatically with React

alright

Jonny Burgera20a3662d5a9
diff --git a/packages/convert/app/components/transcribe/App.tsx b/packages/convert/app/components/transcribe/App.tsx
index 602494a5d7..eadcc9f7d2 100644
--- a/packages/convert/app/components/transcribe/App.tsx
+++ b/packages/convert/app/components/transcribe/App.tsx
@@ -12,16 +12,8 @@ const Transcribe = () => {
return (
<>
- <div
- style={{
- padding: 15,
- left: 20,
- borderRadius: 15,
- gap: 30,
- alignItems: 'center',
- top: 20,
- }}
- >
+ <div className="h-8 lg:h-0 lg:w-8" />
+ <div className="w-full lg:w-[350px]">
<DownloadModel setDownloadingModel={setModelDownloading} />
<TranscribeAudio
{...{
@@ -32,8 +24,10 @@ const Transcribe = () => {
setTranscriptionCompleted,
}}
/>
+ {transcriptionCompleted && result && (
+ <Display audio={file} result={result} />
+ )}
</div>
- {transcriptionCompleted && <Display audio={file} result={result} />}
</>
);
};
diff --git a/packages/convert/app/components/transcribe/display.tsx b/packages/convert/app/components/transcribe/display.tsx
index 10cdae6ab6..307faa7238 100644
--- a/packages/convert/app/components/transcribe/display.tsx
+++ b/packages/convert/app/components/transcribe/display.tsx
@@ -1,82 +1,31 @@
-import type {
- TranscriptionItemWithTimestamp,
- TranscriptionJson,
-} from '@remotion/whisper-web';
-import {useEffect, useMemo, useRef, useState} from 'react';
+import {toCaptions, type TranscriptionJson} from '@remotion/whisper-web';
+import {useMemo} from 'react';
+import {Card} from '../ui/card';
export default function Display({
result,
- audio,
}: {
- readonly result: TranscriptionJson | null;
- readonly audio: File | null;
+ readonly result: TranscriptionJson;
}) {
- const audioRef = useRef<HTMLAudioElement | null>(null);
- const [transcriptionTextIndex, setTranscriptionTextIndex] = useState(0);
- const audioUrl = useMemo(
- () => (audio ? URL.createObjectURL(audio) : undefined),
- [audio],
- );
-
- useEffect(() => {
- if (!audioRef.current) {
- return;
- }
-
- audioRef.current.ontimeupdate = () => {
- if (!audioRef.current) return;
- const activeTime = audioRef.current.currentTime * 1000;
- let timedTranscriptionIndex = -1;
- if (result && result.transcription) {
- timedTranscriptionIndex = result.transcription.findIndex(
- (v: TranscriptionItemWithTimestamp) =>
- v.offsets.from <= activeTime && v.offsets.to >= activeTime,
- );
- }
-
- if (timedTranscriptionIndex >= 0) {
- setTranscriptionTextIndex(timedTranscriptionIndex);
- }
- };
- }, [audioRef, result]);
-
- if (!result) {
- return '';
- }
+ const remotionCaptions = useMemo(() => {
+ return toCaptions({whisperWebOutput: result!});
+ }, [result]);
return (
- <div
- style={{
- borderRadius: 20,
- padding: 20,
- paddingRight: 40,
- paddingLeft: 40,
- justifyContent: 'center',
- justifyItems: 'center',
- gap: 20,
- alignItems: 'center',
- gridAutoFlow: 'row',
- display: 'grid',
- background: 'rgb(20, 20, 20, 0.8)',
- backdropFilter: 'blur(20px)',
- width: 'fit-content',
- }}
- >
- <h2>Transcription</h2>
- <audio ref={audioRef} controls autoPlay src={audioUrl} />
- <p>
- <span style={{opacity: 1, fontWeight: 900}}>
- {result.transcription
- .slice(0, transcriptionTextIndex + 1)
- .map(({text}) => text)}
- </span>
- <span style={{opacity: 0.5, fontWeight: 900}}>
- {result.transcription
- .slice(transcriptionTextIndex + 1)
- .map(({text}) => text)
- .join(' ')}
- </span>
- </p>
- </div>
+ <Card>
+ {remotionCaptions.captions.map((c, i) => {
+ return (
+ <div key={c.text} 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)}
+ </div>
+ <p>{c.text}</p>
+ <p>{c.timestampMs}</p>
+ </div>
+ </div>
+ );
+ })}
+ </Card>
);
}
diff --git a/packages/convert/app/components/transcribe/transcribeAudio.tsx b/packages/convert/app/components/transcribe/transcribeAudio.tsx
index 7b7f4cf09d..352335f4f0 100644
--- a/packages/convert/app/components/transcribe/transcribeAudio.tsx
+++ b/packages/convert/app/components/transcribe/transcribeAudio.tsx
@@ -6,6 +6,7 @@ import {
type TranscriptionJson,
} from '@remotion/whisper-web';
import {useEffect, useRef, useState} from 'react';
+import {Button} from '../ui/button';
import SelectTranscriptionModel from './selectTranscriptionModel';
export default function TranscribeAudio({
@@ -29,6 +30,7 @@ export default function TranscribeAudio({
>(undefined);
const [transcribing, setTranscribing] = useState(false);
const [progress, setProgress] = useState(0);
+
useEffect(() => {}, [modelForTranscription]);
useEffect(() => {
// sync the file between two input sources
@@ -46,7 +48,6 @@ export default function TranscribeAudio({
return (
<div
style={{
- display: 'flex',
gap: 15,
alignItems: 'center',
}}
@@ -110,7 +111,7 @@ export default function TranscribeAudio({
loadingLoadedModels={loadingLoadedModels}
/>
- <button
+ <Button
type="button"
disabled={
modelDownloading ||
@@ -119,7 +120,8 @@ export default function TranscribeAudio({
!file ||
loadingLoadedModels
}
- style={{background: '#0b84f3'}}
+ className="block w-full"
+ variant="brand"
onClick={() => {
if (!modelForTranscription) return;
setTranscribing(true);
@@ -138,7 +140,7 @@ export default function TranscribeAudio({
}}
>
Transcribe
- </button>
+ </Button>
{transcribing && <progress max={1} value={progress} />}
</div>
</div>
diff --git a/packages/whisper-web/src/to-captions.ts b/packages/whisper-web/src/to-captions.ts
index 04045c62fe..a84737890f 100644
--- a/packages/whisper-web/src/to-captions.ts
+++ b/packages/whisper-web/src/to-captions.ts
@@ -19,14 +19,15 @@ export const toCaptions = (input: ToCaptionsInput): ToCaptionsOutput => {
continue;
}
- captions.push({
- text: captions.length === 0 ? item.text.trimStart() : item.text,
- startMs: item.offsets.from,
- endMs: item.offsets.to,
- timestampMs:
- item.tokens[0].t_dtw === -1 ? null : item.tokens[0].t_dtw * 10,
- confidence: item.tokens[0].p,
- });
+ for (const token of item.tokens) {
+ captions.push({
+ text: captions.length === 0 ? token.text.trimStart() : token.text,
+ startMs: token.offsets.from,
+ endMs: token.offsets.to,
+ timestampMs: token.t_dtw === -1 ? null : token.t_dtw * 10,
+ confidence: token.p,
+ });
+ }
}
return {captions};