@azure/ai-speech-transcription

中文文档
1.0.0发布于 2026/05/16MITTypeScriptESM + CJS近 30 天下载 8520

生成用于 TranslactionClient 的 SDK .

中文使用文档
文档目录28 节
  1. 开始
  2. 目前得到支持的环境
  3. 先决条件
  4. 安装 @azure/ai-speech-transcription 软件包
  5. 创建和认证 a TranscriptionClient
  6. 服务 API 版本
  7. Java 脚本套装
  8. 关键概念
  9. 译名
  10. 音频格式
  11. 转写选项
  12. 实例
  13. 转写本地音频文件
  14. 从 URL 转换音频
  15. 访问单个转写单词
  16. 确定扬声器的分号
  17. 过滤污名
  18. 用自定义短语提高准确性
  19. 用一种已知语言转写
  20. 使用最精确的增强模式
  21. 用增强模式翻译
  22. 组合多个选项
  23. 解决问题
  24. 共同问题
  25. 日志
  26. 今后的步骤
  27. 捐款
  28. 相关项目

用于 JavaScript 的 Azure AI Speech Transscript 客户端库

Azure AI Speech Transcription客户端库为Azure的语音对文本的转写服务提供了方便的取用,使您能够高精度地将音频转换为文本.

使用客户端库 :

  • 将音频文件转换为文本
  • 支持多种语言和语言
  • 启用扬声器对号以识别不同的扬声器
  • 应用污名过滤
  • 使用自定义语音模式
  • 处理本地文件和远程 URL
  • 使用增强模式进行LLM驱动的转录和翻译

密钥链接 :

开始

目前得到支持的环境

看我们的 支助政策 更多细节.

先决条件

安装 @azure/ai-speech-transcription 软件包

为 JavaScript 安装 Azure AI Speech Transcript 客户端库 npm数字 :

npm install @azure/ai-speech-transcription

创建和认证 a TranscriptionClient

要创建访问 Azure Translaction API 的客户端对象, 您需要 endpoint 您的 Azure 转录资源和 a credential。 。 。 . 您可以在 Azure 门户。 。 。 .

选项1: API 密钥认证

您可以在 Azure 门户。 。 。 .

import { TranscriptionClient } from "@azure/ai-speech-transcription";
import { AzureKeyCredential } from "@azure/core-auth";

const client = new TranscriptionClient("<endpoint>", new AzureKeyCredential("<api-key>"));

对于生产设想,建议使用EntraID认证,并配有管理下的身份或服务负责人. 安装 @azure/identity 软件包 :

npm install @azure/identity

您还需要指定合适的角色(例如“认知服务用户”)为您管理的身份或服务负责人。 更多信息见 Azure AI 服务认证。 。 。 .

使用节点和类似节点的环境,可以使用 DefaultAzureCredential 类来认证客户端.

import { TranscriptionClient } from "@azure/ai-speech-transcription";
import { DefaultAzureCredential } from "@azure/identity";

const client = new TranscriptionClient("<endpoint>", new DefaultAzureCredential());

对于浏览器环境,请使用 InteractiveBrowserCredential 从 @azure/identity 要认证的软件包.

import { InteractiveBrowserCredential } from "@azure/identity";
import { TranscriptionClient } from "@azure/ai-speech-transcription";

const credential = new InteractiveBrowserCredential({
  tenantId: "<YOUR_TENANT_ID>",
  clientId: "<YOUR_CLIENT_ID>",
});
const client = new TranscriptionClient("<endpoint>", credential);

服务 API 版本

客户端库默认针对最新的服务API版本. 您可以在即时切换客户端时选择特定支持的 API 版本 :

import { TranscriptionClient, KnownServiceApiVersions } from "@azure/ai-speech-transcription";
import { AzureKeyCredential } from "@azure/core-auth";

const client = new TranscriptionClient("<endpoint>", new AzureKeyCredential("<api-key>"), {
  serviceVersion: KnownServiceApiVersions.V20251015,
});

Java 脚本套装

要在浏览器中使用此客户端库,首先需要使用捆绑器. 关于如何做到这一点的详情,请参见我们 捆绑文件。 。 。 .

关键概念

译名

TranscriptionClient 是使用 Azure AI Speech Transscription 客户端库开发者的主要界面. 它提供两个超载 transcribe 方法——用于音频二进制数据和音频 URL.

音频格式

该服务支持各种音频格式,包括WAV,MP3,OGG,FLAC等. 音频必须是:

  • 时间短于2小时
  • 体积小于250 MB

转写选项

您可以自定义以下选项:

  • 职业过滤: 控制在抄本中如何处理亵渎行为("None", (中文(简体) ), "Masked", (中文(简体) ), "Removed", (中文(简体) ), "Tags"(中文(简体) )
  • 扬声器斜化:用多语种音频识别不同的发言者(最多36名发言者)
  • 词组列表: 提供特定域的短语来提高准确性
  • 语言检测: 自动检测口语,或指定已知语言
  • 增强模式:通过LLM驱动的处理、翻译和快速定制,提高文本的质量

实例

转写本地音频文件

最基本的操作是从您的本地文件系统中转录一个音频文件:

import { TranscriptionClient } from "@azure/ai-speech-transcription";
import { AzureKeyCredential } from "@azure/core-auth";
import { readFileSync } from "node:fs";

const client = new TranscriptionClient("<endpoint>", new AzureKeyCredential("<api-key>"));
const audioFile = readFileSync("path/to/audio.wav");
const result = await client.transcribe(audioFile);
console.log(`Duration: ${result.durationInMs}ms`);
console.log("Transcription:", result.combinedPhrases[0]?.text);

从 URL 转换音频

您可以直接从可公开访问的 URL 中转录音频, 而不首先下载文件 :

import { TranscriptionClient } from "@azure/ai-speech-transcription";
import { AzureKeyCredential } from "@azure/core-auth";

const client = new TranscriptionClient("<endpoint>", new AzureKeyCredential("<api-key>"));
const result = await client.transcribe("https://example.com/audio/sample.wav", {
  locales: ["en-US"],
});
console.log("Transcription:", result.combinedPhrases[0]?.text);

访问单个转写单词

获取词层细节,包括时间戳、信心分数和个人单词:

import { TranscriptionClient } from "@azure/ai-speech-transcription";
import { AzureKeyCredential } from "@azure/core-auth";
import { readFileSync } from "node:fs";

const client = new TranscriptionClient("<endpoint>", new AzureKeyCredential("<api-key>"));
const audioFile = readFileSync("path/to/audio.wav");
const result = await client.transcribe(audioFile);
for (const phrase of result.phrases) {
  console.log(`Phrase: ${phrase.text}`);
  console.log(`  Offset: ${phrase.offsetInMs}ms | Duration: ${phrase.durationInMs}ms`);
  console.log(`  Confidence: ${phrase.confidence.toFixed(2)}`);
  // Access individual words in the phrase
  for (const word of phrase.words ?? []) {
    console.log(`    Word: '${word.text}' | Offset: ${word.offsetInMs}ms`);
  }
}

确定扬声器的分号

在多语种对话中

import { TranscriptionClient } from "@azure/ai-speech-transcription";
import { AzureKeyCredential } from "@azure/core-auth";
import { readFileSync } from "node:fs";

const client = new TranscriptionClient("<endpoint>", new AzureKeyCredential("<api-key>"));
const audioFile = readFileSync("path/to/conversation.wav");
const result = await client.transcribe(audioFile, {
  diarizationOptions: {
    maxSpeakers: 4, // Expect up to 4 speakers in the conversation
  },
});
for (const phrase of result.phrases) {
  console.log(`Speaker ${phrase.speaker}: ${phrase.text}`);
}

说明:确定发言者的总数永远不会超过 maxSpeakers。如果实际音频中包含的扬声器比指定的多,则服务将合并它们。 如果您不确定准确的计数, 请设置一个合理的上行 .

过滤污名

使用不同的过滤模式控制在复制中如何出现淫秽:

import { TranscriptionClient, KnownProfanityFilterModes } from "@azure/ai-speech-transcription";
import { AzureKeyCredential } from "@azure/core-auth";
import { readFileSync } from "node:fs";

const client = new TranscriptionClient("<endpoint>", new AzureKeyCredential("<api-key>"));
const audioFile = readFileSync("path/to/audio.wav");
const result = await client.transcribe(audioFile, {
  profanityFilterMode: KnownProfanityFilterModes.Masked, // Default - profanity replaced with asterisks
});
console.log("Transcription:", result.combinedPhrases[0]?.text);

可用模式 :

  • "None":没有过滤——污名作为口语出现
  • "Masked": 纯度被星号所取代(例如, f***(中文(简体) )
  • "Removed": 纯度完全从文本中删除
  • "Tags":用XML标签包裹的纯度(如, <profanity>word</profanity>(中文(简体) )

用自定义短语提高准确性

添加自定义短语来帮助服务正确识别特定域名、名称和缩写:

import { TranscriptionClient } from "@azure/ai-speech-transcription";
import { AzureKeyCredential } from "@azure/core-auth";
import { readFileSync } from "node:fs";

const client = new TranscriptionClient("<endpoint>", new AzureKeyCredential("<api-key>"));
const audioFile = readFileSync("path/to/audio.wav");
const result = await client.transcribe(audioFile, {
  phraseList: {
    phrases: ["Contoso", "Jessie", "Rehaan"],
  },
});
console.log("Transcription:", result.combinedPhrases[0]?.text);

用一种已知语言转写

当您知道音频的语言时, 指定一个单一的语域会提高准确度并减少延迟 :

import { TranscriptionClient } from "@azure/ai-speech-transcription";
import { AzureKeyCredential } from "@azure/core-auth";
import { readFileSync } from "node:fs";

const client = new TranscriptionClient("<endpoint>", new AzureKeyCredential("<api-key>"));
const audioFile = readFileSync("path/to/english-audio.mp3");
const result = await client.transcribe(audioFile, {
  locales: ["en-US"],
});
console.log("Transcription:", result.combinedPhrases[0]?.text);

关于语言识别,当您不确定语言时,请指定多个候选语言,服务会自动检测语言:

import { TranscriptionClient } from "@azure/ai-speech-transcription";
import { AzureKeyCredential } from "@azure/core-auth";
import { readFileSync } from "node:fs";

const client = new TranscriptionClient("<endpoint>", new AzureKeyCredential("<api-key>"));
const audioFile = readFileSync("path/to/audio.mp3");
const result = await client.transcribe(audioFile, {
  locales: ["en-US", "es-ES"],
});
for (const phrase of result.phrases) {
  console.log(`[${phrase.locale}] ${phrase.text}`);
}

使用最精确的增强模式

增强模式使用 LLM 驱动的处理来进行最高精度的转录:

import { TranscriptionClient, KnownProfanityFilterModes } from "@azure/ai-speech-transcription";
import { AzureKeyCredential } from "@azure/core-auth";
import { readFileSync } from "node:fs";

const client = new TranscriptionClient("<endpoint>", new AzureKeyCredential("<api-key>"));
const audioFile = readFileSync("path/to/audio.wav");
const result = await client.transcribe(audioFile, {
  // Enhanced mode: LLM-powered speech recognition with prompt customization
  enhancedMode: {
    task: "transcribe",
    prompt: ["Output must be in lexical format."],
  },
  // Existing Fast Transcription options work alongside enhanced mode
  diarizationOptions: {
    maxSpeakers: 2,
  },
  profanityFilterMode: KnownProfanityFilterModes.Masked,
  activeChannels: [0, 1],
});
for (const phrase of result.phrases) {
  console.log(`[Speaker ${phrase.speaker}] ${phrase.text}`);
}

用增强模式翻译

增强模式还支持将语音翻译为目标语言:

import { TranscriptionClient, KnownProfanityFilterModes } from "@azure/ai-speech-transcription";
import { AzureKeyCredential } from "@azure/core-auth";
import { readFileSync } from "node:fs";

const client = new TranscriptionClient("<endpoint>", new AzureKeyCredential("<api-key>"));
const audioFile = readFileSync("path/to/chinese-audio.wav");
const result = await client.transcribe(audioFile, {
  enhancedMode: {
    task: "translate",
    targetLanguage: "ko", // Translate to Korean
  },
  profanityFilterMode: KnownProfanityFilterModes.Masked,
});
console.log("Translated to Korean:", result.combinedPhrases[0]?.text);

组合多个选项

可以结合多个转录功能,用于复杂的情景:

import { TranscriptionClient, KnownProfanityFilterModes } from "@azure/ai-speech-transcription";
import { AzureKeyCredential } from "@azure/core-auth";
import { readFileSync } from "node:fs";

const client = new TranscriptionClient("<endpoint>", new AzureKeyCredential("<api-key>"));
const audioFile = readFileSync("path/to/meeting.wav");
const result = await client.transcribe(audioFile, {
  // Enable speaker diarization
  diarizationOptions: {
    maxSpeakers: 5,
  },
  // Mask profanity
  profanityFilterMode: KnownProfanityFilterModes.Masked,
  // Add custom phrases
  phraseList: {
    phrases: ["action items", "Q4", "KPIs"],
  },
});
console.log("Full Transcript:");
console.log(result.combinedPhrases[0]?.text);
for (const phrase of result.phrases) {
  console.log(`Speaker ${phrase.speaker}: ${phrase.text}`);
}

解决问题

共同问题

  • 认证失败: 验证您的 API 密钥或 Entra ID 证书正确, 且您的 Speech 资源已激活 .
  • 不支持的音频格式:确保您的音频使用支持格式(WAV,MP3,OGG,FLAC等). 该服务自动处理格式检测.
  • 慢速抄录:对于大文件,确保您的网络连接稳定.
  • 准确性差:尝试指定正确的地块,为特定域名添加自定义短语,或者使用增强模式.

日志

允许伐木可能有助于发现有关失败的有用信息。 为了查看 HTTP 请求和回复的日志,设置 AZURE_LOG_LEVEL 环境变量改为 info。或者,可以在运行时拨打 setLogLevel 输入 @azure/logger数字 :

import { setLogLevel } from "@azure/logger";

setLogLevel("info");

关于如何启用日志的更详细指示,您可以查看 @ azure/ logger 包文件。 。 。 .

今后的步骤

探索更多样本,以更多地了解先进特征:

捐款

如果您想为图书馆捐钱 请读一下 贡献指南 学习如何构建和测试密码.

版本与发布记录

@azure/ai-speech-transcription 共发布 3 个版本,首次发布于 2026/03/16;过去 12 个月发布了 3 个版本。

版本发布日期距今
1.0.0latest2026/05/165 个月前
1.0.0-beta.2beta2026/05/145 个月前
1.0.0-beta.12026/03/167 个月前

关于 @azure/ai-speech-transcription 的常见问题

@azure/ai-speech-transcription 是什么?

生成用于 TranslactionClient 的 SDK .

如何安装 @azure/ai-speech-transcription?

在项目目录运行 npm install @azure/ai-speech-transcription,或使用 pnpm add / yarn add / bun add。

@azure/ai-speech-transcription 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@azure/ai-speech-transcription 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@azure/ai-speech-transcription 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@azure/ai-speech-transcription 可以商用吗?

@azure/ai-speech-transcription 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@azure/ai-speech-transcription 的下载量有多少?

根据 npm 官方下载统计,@azure/ai-speech-transcription 在 2026/10/08 – 2026/11/06 这 30 天内被下载 8,520 次,日均约 284 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@azure/ai-speech-transcription 还在维护吗?

最新版本 1.0.0 发布于 2026/05/16(5 个月前);至今共发布 3 个版本,其中过去 12 个月发布了 3 个;首次发布于 2026/03/16。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。