Skip to content

🎥 Make videos programmatically with React

awesome

Jonny Burger4c8a5f8ea062
diff --git a/packages/captions/src/captions.ts b/packages/captions/src/captions.ts
index 190ef16fd3..ae7c9cb806 100644
--- a/packages/captions/src/captions.ts
+++ b/packages/captions/src/captions.ts
@@ -1,6 +1,6 @@
export type Caption = {
text: string;
timestamp: number;
- from: number;
- to: number;
+ startMs: number;
+ endMs: number;
};
diff --git a/packages/captions/src/create-tiktok-style-captions.ts b/packages/captions/src/create-tiktok-style-captions.ts
new file mode 100644
index 0000000000..3ca629046c
--- /dev/null
+++ b/packages/captions/src/create-tiktok-style-captions.ts
@@ -0,0 +1,63 @@
+import {Caption} from './captions';
+
+export type MergedCaption = {
+ text: string;
+ startInSeconds: number;
+};
+
+export function createTikTokStyleCaptions({
+ transcription,
+ combineTokensWithinMilliseconds,
+}: {
+ transcription: Caption[];
+ combineTokensWithinMilliseconds: number;
+}): {captions: MergedCaption[]} {
+ const merged: MergedCaption[] = [];
+ let currentText = '';
+ let currentFrom = 0;
+ let currentTo = 0;
+ let currentTokenLevelTimestamp = 0;
+
+ transcription.forEach((item, index) => {
+ const {text} = item;
+ // If text starts with a space, push the currentText (if it exists) and start a new one
+ if (
+ text.startsWith(' ') &&
+ currentTo - currentFrom > combineTokensWithinMilliseconds
+ ) {
+ if (currentText !== '') {
+ merged.push({
+ text: currentText,
+ startInSeconds: currentTokenLevelTimestamp / 1000,
+ });
+ }
+
+ // Start a new sentence
+ currentText = text.trimStart();
+ currentFrom = item.startMs;
+ currentTo = item.endMs;
+ currentTokenLevelTimestamp = item.timestamp;
+ } else {
+ // Continuation or start of a new sentence without leading space
+ if (currentText === '') {
+ // It's the start of the document or after a sentence that started with a space
+ currentFrom = item.startMs;
+ currentTokenLevelTimestamp = item.timestamp;
+ }
+
+ currentText += text;
+ currentText = currentText.trimStart();
+ currentTo = item.endMs;
+ }
+
+ // Ensure the last sentence is added
+ if (index === transcription.length - 1 && currentText !== '') {
+ merged.push({
+ text: currentText,
+ startInSeconds: currentTokenLevelTimestamp / 1000,
+ });
+ }
+ });
+
+ return {captions: merged};
+}
diff --git a/packages/captions/src/index.ts b/packages/captions/src/index.ts
index 4c6bd20250..ead8db4666 100644
--- a/packages/captions/src/index.ts
+++ b/packages/captions/src/index.ts
@@ -1 +1,2 @@
export {Caption} from './captions';
+export {createTikTokStyleCaptions} from './create-tiktok-style-captions';
diff --git a/packages/docs/vercel.json b/packages/docs/vercel.json
index a2b5bced31..e914e37109 100644
--- a/packages/docs/vercel.json
+++ b/packages/docs/vercel.json
@@ -327,6 +327,16 @@
"destination": "/docs/recorder",
"permanent": false
},
+ {
+ "source": "/install-whisper-cpp",
+ "destination": "/docs/install-whisper-cpp",
+ "permanent": false
+ },
+ {
+ "source": "/captions",
+ "destination": "/docs/captions",
+ "permanent": false
+ },
{
"source": "/docs/miscellaneous/snippets/adding-animations",
"destination": "/docs/animation-math",
diff --git a/packages/install-whisper-cpp/package.json b/packages/install-whisper-cpp/package.json
index a55e76b18a..bcffda714c 100644
--- a/packages/install-whisper-cpp/package.json
+++ b/packages/install-whisper-cpp/package.json
@@ -20,7 +20,9 @@
],
"author": "Jonny Burger <[email protected]>",
"license": "SEE LICENSE IN LICENSE.md",
- "dependencies": {},
+ "dependencies": {
+ "@remotion/captions": "workspace:*"
+ },
"peerDependencies": {},
"devDependencies": {},
"keywords": [
diff --git a/packages/install-whisper-cpp/src/convert.ts b/packages/install-whisper-cpp/src/convert.ts
new file mode 100644
index 0000000000..1566aefadd
--- /dev/null
+++ b/packages/install-whisper-cpp/src/convert.ts
@@ -0,0 +1,27 @@
+import type {Caption} from '@remotion/captions';
+import type {TranscriptionJson} from './transcribe';
+
+type ConvertInput = {
+ whisperCppOutput: TranscriptionJson<true>;
+};
+
+type ConvertOutput = {
+ captions: Caption[];
+};
+
+export const convert = (input: ConvertInput): ConvertOutput => {
+ const {transcription} = input.whisperCppOutput;
+
+ const captions: Caption[] = [];
+
+ for (const item of transcription) {
+ captions.push({
+ text: item.text,
+ startMs: item.offsets.from,
+ endMs: item.offsets.to,
+ timestamp: item.tokens[0].t_dtw * 10,
+ });
+ }
+
+ return {captions};
+};
diff --git a/packages/template-tiktok/whisper-config.mjs b/packages/template-tiktok/whisper-config.mjs
index c888e32813..d7cafccea9 100644
--- a/packages/template-tiktok/whisper-config.mjs
+++ b/packages/template-tiktok/whisper-config.mjs
@@ -1,10 +1,10 @@
-import path from 'node:path';
+import path from "node:path";
// Where to install Whisper.cpp to
-export const WHISPER_PATH = path.join(process.cwd(), 'whisper.cpp');
+export const WHISPER_PATH = path.join(process.cwd(), "whisper.cpp");
// The version of Whisper.cpp to install
-export const WHISPER_VERSION = '1.5.5';
+export const WHISPER_VERSION = "1.6.0";
// Which model to use.
// | Model | Disk | Mem |
@@ -19,12 +19,15 @@ export const WHISPER_VERSION = '1.5.5';
// | medium.en | 1.5 GB | ~2.6 GB |
// | large-v1 | 2.9 GB | ~4.7 GB |
// | large-v2 | 2.9 GB | ~4.7 GB |
+// | large-v3 | 2.9 GB | ~4.7 GB |
+// | large-v3-turbo | 2.9 GB | ~4.7 GB |
+// | large-v2 | 2.9 GB | ~4.7 GB |
// | large | 2.9 GB | ~4.7 GB |
/**
* @type {import('@remotion/install-whisper-cpp').WhisperModel}
*/
-export const WHISPER_MODEL = 'medium.en';
+export const WHISPER_MODEL = "large-v3";
// Language to transcribe
// If you set another language than 'en', remove .en from the WHISPER_MODEL
@@ -32,4 +35,4 @@ export const WHISPER_MODEL = 'medium.en';
/**
* @type {import('@remotion/install-whisper-cpp').Language}
*/
-export const WHISPER_LANG = 'en';
+export const WHISPER_LANG = "en";
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 11d73153c6..d030297030 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -1033,7 +1033,11 @@ importers:
specifier: 3.3.3
version: 3.3.3
- packages/install-whisper-cpp: {}
+ packages/install-whisper-cpp:
+ dependencies:
+ '@remotion/captions':
+ specifier: workspace:*
+ version: link:../captions
packages/it-tests:
devDependencies: