跳转到内容

Node.js 客户端

使用 video-to-text-js 上传媒体、创建转录任务并轮询结果。

Node.js 客户端封装了公开上传接口和任务接口,并提供类型化辅助方法。

Terminal window
pnpm add video-to-text-js
import { VideoToTextClient } from 'video-to-text-js'
const client = new VideoToTextClient({
apiKey: process.env.VTT_API_KEY!,
})
import { readFile } from 'node:fs/promises'
const file = await readFile('meeting.mp4')
const task = await client.transcriptions.transcribeFile(file, {
filename: 'meeting.mp4',
mimetype: 'video/mp4',
language: 'Auto',
timestampMode: 'CHUNK',
transcriptionMode: 'balanced',
timeoutMs: 10 * 60 * 1000,
})
console.log(task.fullText)

当准确性比学分成本更重要时,可以使用 precision

const upload = await client.uploads.create({
filename: 'meeting.mp4',
mimetype: 'video/mp4',
})
await client.uploads.put(upload.uploadUrl, file, {
mimetype: 'video/mp4',
})
const asset = await client.uploads.complete(upload.uploadId, {
fileKey: upload.fileKey,
fileUrl: upload.fileUrl,
filename: 'meeting.mp4',
mimetype: 'video/mp4',
fileSize: file.byteLength,
})
const created = await client.tasks.create({
assetId: asset.assetId,
language: 'Auto',
timestampMode: 'CHUNK',
transcriptionMode: 'balanced',
})
const result = await client.tasks.wait(created.transcriptId)
console.log(result.fullText)

tasks.wait() 会在任务进入 SUCCEEDED 时返回。如果任务进入 FAILEDCANCELED,客户端会抛出 APIError,并优先使用 showError 作为错误详情。

任务详情包含 fullTextchunkswordssourceDurationMsresultLanguagetranscriptionModebilledCredits

import { APIError, RateLimitError, UploadError } from 'video-to-text-js'
try {
await client.transcriptions.transcribeFile(file, {
filename: 'meeting.mp4',
mimetype: 'video/mp4',
})
} catch (error) {
if (error instanceof RateLimitError) {
console.error('Please retry later.')
} else if (error instanceof UploadError) {
console.error('The media upload did not complete.')
} else if (error instanceof APIError) {
console.error(error.errorCode, error.detail)
}
}

源代码、问题反馈和贡献请查看 video-to-text-js GitHub 仓库。最新版本可从 npm 上的 video-to-text-js 安装。