Skip to main content

transcribe()v4.0.131

通过利用 Whisper.cpp 转录媒体文件。
您应该首先安装 Whisper.cpp,例如通过 installWhisperCpp()

note

此功能仅适用于 Whisper.cpp 版本 1.5.5 或更高版本,除非将 tokenLevelTimestamps 设置为 false。

transcribe.mjs
tsx
import path from "path";
import { transcribe } from "@remotion/install-whisper-cpp";
 
const { transcription } = await transcribe({
inputPath: "/path/to/audio.wav",
whisperPath: path.join(process.cwd(), "whisper.cpp"),
model: "medium.en",
tokenLevelTimestamps: true,
});
 
for (const token of transcription) {
console.log(token.timestamps.from, token.timestamps.to, token.text);
}
transcribe.mjs
tsx
import path from "path";
import { transcribe } from "@remotion/install-whisper-cpp";
 
const { transcription } = await transcribe({
inputPath: "/path/to/audio.wav",
whisperPath: path.join(process.cwd(), "whisper.cpp"),
model: "medium.en",
tokenLevelTimestamps: true,
});
 
for (const token of transcription) {
console.log(token.timestamps.from, token.timestamps.to, token.text);
}

选项

inputPath

要从中提取文本的文件路径。

文件必须是 16KHz wav 文件。您可以通过以下命令从视频或音频文件中提取 16KHz wav 文件,例如利用 FFmpeg:

bash
ffmpeg -i input.mp4 -ar 16000 output.wav -y
bash
ffmpeg -i input.mp4 -ar 16000 output.wav -y

如果您不想安装 FFmpeg,您也可以使用 Remotion 提供的 更小的 FFmpeg 二进制文件

bash
npx remotion ffmpeg -i input.mp4 -ar 16000 output.wav -y
bash
npx remotion ffmpeg -i input.mp4 -ar 16000 output.wav -y

whisperPath

您的 whisper.cpp 文件夹路径。
如果您尚未安装 Whisper.cpp,您可以通过 installWhisperCpp() 进行安装,并使用相同的 folder

tokenLevelTimestampsv4.0.131

--dtw 标志传递给 Whisper.cpp 以生成更准确的时间戳,这些时间戳将在 t_dtw 字段下返回。
建议用于获取实际准确的时间,但仅适用于 Whisper.cpp 版本 1.0.55 之后的版本。
如果您使用 Whisper.cpp 的旧版本,请将其设置为 false

model?

可选 - 默认值:base.en

为转录指定特定的 Whisper 模型。

可能的值:tinytiny.enbasebase.ensmallsmall.enmediummedium.enlarge-v1large-v2large-v3

确保您要使用的模型存在于您的 whisper.cpp/models 文件夹中。您可以通过利用 downloadWhisperModel() API 确保特定模型在本地可用。

modelFolder?

可选 - 默认值: whisperPath/models

如果您将 Whisper 模型保存到特定文件夹,请在此处传递其路径。

默认情况下,使用通过 whisperPath 定义的位置上的 whisper.cpp/models 文件夹。

translateToEnglish?

可选 - 默认值: false

如果您想要获取提供文件的翻译文本的翻译版本,请将此布尔标志设置为 true。 请确保不使用 *.en 模型,因为它们无法将外语翻译为英语。

note

我们建议至少使用 medium 模型以获得令人满意的翻译结果。

printOutput?v4.0.132

是否将转录过程的输出打印到控制台。默认为 true

tokensPerItem?v4.0.141

可选 - 默认值: 1

每个转录项中包含的最大令牌数量。

将此标志设置为 null,以使用 whisper.cpp 的默认令牌分组(用于生成类似电影风格的转录)。

info

只有在将 tokenLevelTimestamps 设置为 false 时才能设置 tokensPerItem

language?v4.0.142

可选 - 默认值: null

-l 标志传递给 Whisper.cpp,指定音频文件的特定口语语言。

可能的值: Afrikaans, Albanian, Amharic, Arabic, Armenian, Assamese, Azerbaijani, Bashkir, Basque, Belarusian, Bengali, Bosnian, Breton, Bulgarian, Burmese, Castilian, Catalan, Chinese, Croatian, Czech, Danish, Dutch, English, Estonian, Faroese, Finnish, Flemish, French, Galician, Georgian, German, Greek, Gujarati, Haitian, Haitian Creole, Hausa, Hawaiian, Hebrew, Hindi, Hungarian, Icelandic, Indonesian, Italian, Japanese, Javanese, Kannada, Kazakh, Khmer, Korean, Lao, Latin, Latvian, Letzeburgesch, Lingala, Lithuanian, Luxembourgish, Macedonian, Malagasy, Malay, Malayalam, Maltese, Maori, Marathi, Moldavian, Moldovan, Mongolian, Myanmar, Nepali, Norwegian, Nynorsk, Occitan, Panjabi, Pashto, Persian, Polish, Portuguese, Punjabi, Pushto, Romanian, Russian, Sanskrit, Serbian, Shona, Sindhi, Sinhala, Sinhalese, Slovak, Slovenian, Somali, Spanish, Sundanese, Swahili, Swedish, Tagalog, Tajik, Tamil, Tatar, Telugu, Thai, Tibetan, Turkish, Turkmen, Ukrainian, Urdu, Uzbek, Valencian, Vietnamese, Welsh, Yiddish, Yoruba, Zuluaf, am, ar, as, az, ba, be, bg, bn, bo, br, bs, ca, cs, cy, da, de, el, en, es, et, eu, fa, fi, fo, fr, gl, gu, ha, haw, he, hi, hr, ht, hu, hy, id, is, it, ja, jw, ka, kk, km, kn, ko, la, lb, ln, lo, lt, lv, mg, mi, mk, ml, mn, mr, ms, mt, my, ne, nl, nn, no, oc, pa, pl, ps, pt, ro, ru, sa, sd, si, sk, sl, sn, so, sq, sr, su, sv, sw, ta, te, tg, th, tk, tl, tr, tt, uk, ur, uz, vi, yi, yo, zhauto

signal?v4.0.156

来自AbortController的信号,用于取消转录过程。

onProgress?v4.0.156

监听转录过程中的进度更新。
进度是介于 01 之间的数字。

tsx
import type { TranscribeOnProgress } from "@remotion/install-whisper-cpp";
 
const onProgress: TranscribeOnProgress = (progress) => {
console.log(`Transcription progress: ${progress * 100}%`);
};
tsx
import type { TranscribeOnProgress } from "@remotion/install-whisper-cpp";
 
const onProgress: TranscribeOnProgress = (progress) => {
console.log(`Transcription progress: ${progress * 100}%`);
};

返回值

TranscriptionJson

包含转录过程中所有元数据和转录结果的对象。

ts
type Timestamps = {
from: string;
to: string;
};
 
type Offsets = {
from: number;
to: number;
};
 
type WordLevelToken = {
t_dtw: number;
text: string;
timestamps: Timestamps;
offsets: Offsets;
id: number;
p: number;
};
 
type TranscriptionItem = {
timestamps: Timestamps;
offsets: Offsets;
text: string;
};
 
type TranscriptionItemWithTimestamp = TranscriptionItem & {
tokens: WordLevelToken[];
};
 
type Model = {
type: string;
multilingual: boolean;
vocab: number;
audio: {
ctx: number;
state: number;
head: number;
layer: number;
};
text: {
ctx: number;
state: number;
head: number;
layer: number;
};
mels: number;
ftype: number;
};
 
type Params = {
model: string;
language: string;
translate: boolean;
};
 
type Result = {
language: string;
};
 
export type TranscriptionJson<WithTokenLevelTimestamp extends boolean> = {
systeminfo: string;
model: Model;
params: Params;
result: Result;
transcription: true extends WithTokenLevelTimestamp
? TranscriptionItemWithTimestamp[]
: TranscriptionItem[];
};
ts
type Timestamps = {
from: string;
to: string;
};
 
type Offsets = {
from: number;
to: number;
};
 
type WordLevelToken = {
t_dtw: number;
text: string;
timestamps: Timestamps;
offsets: Offsets;
id: number;
p: number;
};
 
type TranscriptionItem = {
timestamps: Timestamps;
offsets: Offsets;
text: string;
};
 
type TranscriptionItemWithTimestamp = TranscriptionItem & {
tokens: WordLevelToken[];
};
 
type Model = {
type: string;
multilingual: boolean;
vocab: number;
audio: {
ctx: number;
state: number;
head: number;
layer: number;
};
text: {
ctx: number;
state: number;
head: number;
layer: number;
};
mels: number;
ftype: number;
};
 
type Params = {
model: string;
language: string;
translate: boolean;
};
 
type Result = {
language: string;
};
 
export type TranscriptionJson<WithTokenLevelTimestamp extends boolean> = {
systeminfo: string;
model: Model;
params: Params;
result: Result;
transcription: true extends WithTokenLevelTimestamp
? TranscriptionItemWithTimestamp[]
: TranscriptionItem[];
};

建议使用 t_dtw 值而不是 offsets 来获取准确的时间戳。
使用 convertToCaptions() 来使用我们对字幕后处理的建议。

参见