Skip to main content

AssemblyAI 音频转录文本

本文介绍如何使用 AssemblyAI API 从文件中加载音频(和视频)转录文本作为文档对象。

使用方法

首先,你需要安装官方的 AssemblyAI 包:

npm install @langchain/community @langchain/core assemblyai

要使用加载器,你需要一个 AssemblyAI 账户,并从仪表板获取你的 AssemblyAI API 密钥

然后,将 API 密钥配置为 ASSEMBLYAI_API_KEY 环境变量,或者通过 apiKey 选项参数传入。

import {
AudioTranscriptLoader,
// AudioTranscriptParagraphsLoader,
// AudioTranscriptSentencesLoader
} from "@langchain/community/document_loaders/web/assemblyai";

// You can also use a local file path and the loader will upload it to AssemblyAI for you.
const audioUrl = "https://storage.googleapis.com/aai-docs-samples/espn.m4a";

// Use `AudioTranscriptParagraphsLoader` or `AudioTranscriptSentencesLoader` for splitting the transcript into paragraphs or sentences
const loader = new AudioTranscriptLoader(
{
audio: audioUrl,
// any other parameters as documented here: https://www.assemblyai.com/docs/api-reference/transcripts/submit
},
{
apiKey: "<ASSEMBLYAI_API_KEY>", // or set the `ASSEMBLYAI_API_KEY` env variable
}
);
const docs = await loader.load();
console.dir(docs, { depth: Infinity });

API Reference:

提示

  • 你可以使用 AudioTranscriptParagraphsLoaderAudioTranscriptSentencesLoader 将转录文本按段落或句子拆分。
  • audio 参数可以是 URL、本地文件路径、buffer 或流。
  • audio 也可以是视频文件。支持的文件类型请参阅常见问题文档
  • 如果没有传入 apiKey 选项,加载器将使用 ASSEMBLYAI_API_KEY 环境变量。
  • 你可以添加除 audio 外的更多属性。完整的请求参数列表请参阅 AssemblyAI API 文档

你还可以使用 AudioSubtitleLoader 来获取 srtvtt 格式的字幕作为文档。

import { AudioSubtitleLoader } from "@langchain/community/document_loaders/web/assemblyai";

// You can also use a local file path and the loader will upload it to AssemblyAI for you.
const audioUrl = "https://storage.googleapis.com/aai-docs-samples/espn.m4a";

const loader = new AudioSubtitleLoader(
{
audio: audioUrl,
// any other parameters as documented here: https://www.assemblyai.com/docs/api-reference/transcripts/submit
},
"srt", // srt or vtt
{
apiKey: "<ASSEMBLYAI_API_KEY>", // or set the `ASSEMBLYAI_API_KEY` env variable
}
);

const docs = await loader.load();
console.dir(docs, { depth: Infinity });

API Reference:


Was this page helpful?


You can also leave detailed feedback on GitHub.