🎥 Make videos programmatically with React
finish transcribe API
Jonny Burger3f1290dc890f
diff --git a/packages/docs/docs/install-whisper-cpp/index.mdx b/packages/docs/docs/install-whisper-cpp/index.mdxindex 412798d78a..8d250c43d1 100644--- a/packages/docs/docs/install-whisper-cpp/index.mdx+++ b/packages/docs/docs/install-whisper-cpp/index.mdx@@ -24,6 +24,7 @@ import path from "path";import {downloadWhisperModel,installWhisperCpp,+ transcribe,} from "@remotion/install-whisper-cpp";const to = path.join(process.cwd(), "whisper.cpp");@@ -37,6 +38,16 @@ await downloadWhisperModel({model: "base.en",folder: to,});++const { transcription } = await transcribe({+ model: "base.en",+ whisperPath: to,+ inputPath: "/path/to/audio.wav",+});++for (const token of transcription) {+ console.log(token.timestamps.from, token.timestamps.to, token.text);+}```## Functionsdiff --git a/packages/docs/docs/install-whisper-cpp/transcribe.mdx b/packages/docs/docs/install-whisper-cpp/transcribe.mdxindex 4b76481bd3..e5c56775a6 100644--- a/packages/docs/docs/install-whisper-cpp/transcribe.mdx+++ b/packages/docs/docs/install-whisper-cpp/transcribe.mdx@@ -1,12 +1,12 @@----# image: /generated/articles-docs-install-whisper-cpp-download-whisper-model.png+image: /generated/articles-docs-install-whisper-cpp-transcribe.pngtitle: transcribe()crumb: "@remotion/install-whisper-cpp"----# downloadWhisperModel()<AvailableFrom v="4.0.125 "/>+# transcribe()<AvailableFrom v="4.0.125 "/>-Transcribes a media file by utilizing Whisper.cpp.+Transcribes a media file by utilizing Whisper.cpp.You should first install Whisper.cpp, for example through [`installWhisperCpp()`](/docs/install-whisper-cpp/install-whisper-cpp).```tsx twoslash title="transcribe.mjs"@@ -16,15 +16,19 @@ import path from "path";import { transcribe } from "@remotion/install-whisper-cpp";const { transcription } = await transcribe({- filePath: "pathToYourFile",+ inputPath: "/path/to/audio.wav",whisperPath: path.join(process.cwd(), "whisper.cpp"),model: "base.en",});++for (const token of transcription) {+ console.log(token.timestamps.from, token.timestamps.to, token.text);+}```## Options-### `filePath`+### `inputPath`The path to the file you want extract text from.@@ -35,7 +39,7 @@ example by utilizing FFmpeg with the following command:ffmpeg -i input.mp4 -ar 16000 output.wav -y```-If you don't want to install FFmpeg, you can also use the smaller FFmpeg binary provided by Remotion.+If you don't want to install FFmpeg, you can also use the [smaller FFmpeg binary](/docs/cli/ffmpeg) provided by Remotion.```bashnpx remotion ffmpeg -i input.mp4 -ar 16000 output.wav -y@@ -43,7 +47,8 @@ npx remotion ffmpeg -i input.mp4 -ar 16000 output.wav -y### `whisperPath`-The path to your Whisper.cpp folder. If you haven't installed Whisper.cpp, you can do so for example through [installWhisperCpp()](docs/install-whisper-cpp/install-whisper-cpp).+The path to your `whisper.cpp` folder.+If you haven't installed Whisper.cpp, you can do so for example through [`installWhisperCpp()`](/docs/install-whisper-cpp/install-whisper-cpp) and use the same `folder`.### `model?`@@ -53,7 +58,7 @@ Specify a specific Whisper model for the transcription.Possible values: `tiny`, `tiny.en`, `base`, `base.en`, `small`, `small.en`, `medium`, `medium.en`, `large-v1`, `large-v2`, `large-v3`.-Make sure the model you want to use exists in your `whisper.cpp/models` folder. You can ensure a specific model is available locally by utilizing the [downloadWhisperModel()](docs/install-whisper-cpp/download-whisper-model) API.+Make sure the model you want to use exists in your `whisper.cpp/models` folder. You can ensure a specific model is available locally by utilizing the [downloadWhisperModel()](/docs/install-whisper-cpp/download-whisper-model) API.### `modelFolder?`@@ -80,13 +85,17 @@ We recommend using at least the `medium` model to get satisfactory results.An object containing all the metadata and transcriptions resulting from the transcription process.-```ts-type TranscriptionJson = {- systeminfo: string;- model: Model;- params: Params;- result: Result;- transcription: TranscriptionItem[];+```ts twoslash+type TranscriptionItem = {+ timestamps: {+ from: string;+ to: string;+ };+ offsets: {+ from: number;+ to: number;+ };+ text: string;};type Model = {@@ -109,30 +118,18 @@ type Model = {ftype: number;};-type Params = {- model: string;- language: string;- translate: boolean;-};--type TranscriptionItem = {- timestamps: Timestamps;- offsets: Offsets;- text: string;-};--type Result = {- language: string;-};--type Timestamps = {- from: string;- to: string;-};--type Offsets = {- from: number;- to: number;+type TranscriptionJson = {+ systeminfo: string;+ model: Model;+ params: {+ model: string;+ language: string;+ translate: boolean;+ };+ result: {+ language: string;+ };+ transcription: TranscriptionItem[];};```diff --git a/packages/docs/src/data/articles.ts b/packages/docs/src/data/articles.tsindex b2c8db07b4..b1dc3d58a7 100644--- a/packages/docs/src/data/articles.ts+++ b/packages/docs/src/data/articles.ts@@ -93,7 +93,7 @@ export const articles = [{id: "audio",title: "<Audio>",- relativePath: "docs/audio.md",+ relativePath: "docs/audio.mdx",compId: "articles-docs-audio",crumb: "How to",},@@ -867,13 +867,6 @@ export const articles = [compId: "articles-docs-install-whisper-cpp-download-whisper-model",crumb: "@remotion/install-whisper-cpp",},- {- id: "install-whisper-cpp/transcribe",- title: "downloadWhisperModel()",- relativePath: "docs/install-whisper-cpp/transcribe.mdx",- compId: "articles-docs-install-whisper-cpp-transcribe",- crumb: "@remotion/install-whisper-cpp",- },{id: "install-whisper-cpp/index",title: "@remotion/install-whisper-cpp",@@ -888,6 +881,13 @@ export const articles = [compId: "articles-docs-install-whisper-cpp-install-whisper-cpp",crumb: "@remotion/install-whisper-cpp",},+ {+ id: "install-whisper-cpp/transcribe",+ title: "transcribe()",+ relativePath: "docs/install-whisper-cpp/transcribe.mdx",+ compId: "articles-docs-install-whisper-cpp-transcribe",+ crumb: "@remotion/install-whisper-cpp",+ },{id: "interpolate-colors",title: "interpolateColors()",@@ -3098,7 +3098,7 @@ export const articles = [{id: "video",title: "<Video>",- relativePath: "docs/video.md",+ relativePath: "docs/video.mdx",compId: "articles-docs-video",crumb: "API",},diff --git a/packages/docs/static/generated/articles-docs-install-whisper-cpp-transcribe.png b/packages/docs/static/generated/articles-docs-install-whisper-cpp-transcribe.pngnew file mode 100644index 0000000000..778718997bBinary files /dev/null and b/packages/docs/static/generated/articles-docs-install-whisper-cpp-transcribe.png differdiff --git a/packages/install-whisper-cpp/src/download-whisper-model.ts b/packages/install-whisper-cpp/src/download-whisper-model.tsindex dd8c8258f1..f964d239f4 100644--- a/packages/install-whisper-cpp/src/download-whisper-model.ts+++ b/packages/install-whisper-cpp/src/download-whisper-model.ts@@ -20,6 +20,10 @@ const models = [export type WhisperModel = (typeof models)[number];export type OnProgress = (downloadedBytes: number, totalBytes: number) => void;+export const getModelPath = (folder: string, model: WhisperModel) => {+ return path.join(folder, `ggml-${model}.bin`);+};+export const downloadWhisperModel = async ({model,folder,@@ -39,7 +43,8 @@ export const downloadWhisperModel = async ({);}- const filePath = path.join(folder, `ggml-${model}.bin`);+ const filePath = getModelPath(folder, model);+if (existsSync(filePath)) {if (printOutput) {console.log(`Model already exists at ${filePath}`);diff --git a/packages/install-whisper-cpp/src/transcribe.ts b/packages/install-whisper-cpp/src/transcribe.tsindex 9626b745a5..add47795f2 100644--- a/packages/install-whisper-cpp/src/transcribe.ts+++ b/packages/install-whisper-cpp/src/transcribe.ts@@ -4,6 +4,7 @@ import os from 'node:os';import path from 'node:path';import util from 'node:util';import type {WhisperModel} from './download-whisper-model';+import {getModelPath} from './download-whisper-model';type Timestamps = {from: string;@@ -59,8 +60,8 @@ export type TranscriptionJson = {transcription: TranscriptionItem[];};-const isWavFile = (filePath: string) => {- const splitted = filePath.split('.');+const isWavFile = (inputPath: string) => {+ const splitted = inputPath.split('.');if (!splitted) {return false;}@@ -74,31 +75,29 @@ const readJson = async (jsonPath: string) => {return jsonData;};-const transcribeToTempJSON = async (- fileToTranscribe: string,- whisperPath: string,- model: WhisperModel,- tmpJSONPath: string,- modelFolder: string | null,- translate: boolean,-) => {+const transcribeToTempJSON = async ({+ fileToTranscribe,+ whisperPath,+ model,+ tmpJSONPath,+ modelFolder,+ translate,+}: {+ fileToTranscribe: string;+ whisperPath: string;+ model: WhisperModel;+ tmpJSONPath: string;+ modelFolder: string | null;+ translate: boolean;+}) => {const promisifiedExec = util.promisify(exec);- const modelExecutableName = `ggml-${model}.bin`;- const defaultModelPath = path.join(- whisperPath,- 'models',- modelExecutableName,- );-- const modelPath = modelFolder- ? path.join(modelFolder, modelExecutableName)- : defaultModelPath;+ const modelPath = getModelPath(modelFolder ?? whisperPath, model);if (!fs.existsSync(modelPath)) {throw new Error(`Error: Model ${model} does not exist at ${- modelFolder ? modelFolder : defaultModelPath+ modelFolder ? modelFolder : modelPath}. Check out the downloadWhisperMode() API at https://www.remotion.dev/docs/install-whisper-cpp/download-whisper-model to see how to install whisper models`,);}@@ -122,55 +121,49 @@ const transcribeToTempJSON = async (};export const transcribe = async ({- filePath,+ inputPath,whisperPath,- model = 'base.en',+ model,modelFolder,translateToEnglish = false,}: {- filePath: string;+ inputPath: string;whisperPath: string;- model?: WhisperModel;+ model: WhisperModel;modelFolder?: string;translateToEnglish?: boolean;-}): Promise<{transcription: TranscriptionJson}> => {+}): Promise<TranscriptionJson> => {if (!existsSync(whisperPath)) {throw new Error(`Whisper does not exist at ${whisperPath}. Double-check the passed whisperPath. If you havent installed whisper, check out the installWhisperCpp() API at https://www.remotion.dev/docs/install-whisper-cpp/install-whisper-cpp to see how to install whisper programatically.`,);}- if (!existsSync(filePath)) {- throw new Error(`Input file does not exist at ${filePath}`);+ if (!existsSync(inputPath)) {+ throw new Error(`Input file does not exist at ${inputPath}`);}- if (!isWavFile(filePath)) {+ if (!isWavFile(inputPath)) {throw new Error('Invalid inputFile type. The provided file is not a wav file!',);}- const tmpJSONPath = path.join(process.cwd(), 'tmp');+ const tmpJSONDir = path.join(process.cwd(), 'tmp');- await transcribeToTempJSON(- filePath,+ await transcribeToTempJSON({+ fileToTranscribe: inputPath,whisperPath,model,- tmpJSONPath,- modelFolder ?? null,- translateToEnglish,- );-- const json = await readJson(tmpJSONPath + '.json');-- fs.unlink(tmpJSONPath + '.json', (err) => {- if (err) {- console.error(- 'An error occured while deleting the temporary json file: ',- err,- );- }+ tmpJSONPath: tmpJSONDir,+ modelFolder: modelFolder ?? null,+ translate: translateToEnglish,});- return Promise.resolve({transcription: json});+ const tmpJSONPath = `${tmpJSONDir}.json`;++ const json = (await readJson(tmpJSONPath)) as TranscriptionJson;+ fs.unlinkSync(tmpJSONPath);++ return json;};