@aws-sdk/client-transcribe-streaming

中文文档
3.1164.0发布于 2026/11/07Apache-2.0TypeScriptESM + CJS近 30 天下载 162.3万

JavaScript Translating Clients for Node.js 的 AWS SDK, 浏览器和 React Industrial 的应用程序

中文使用文档
文档目录14 节
  1. 导言
  2. 安装
  3. 开始
  4. 构建服务客户端
  5. 获取语音流
  6. 获得Node.js Stream API
  7. 从浏览器获取
  8. 发送语音流
  9. 处理文本流
  10. 管道转写流
  11. 处理错误
  12. 本地反应的注释
  13. 捐款
  14. 许可证

@aws- sdk/ 客户端- translatz- sreaming

NPM version NPM downloads

导言

Amazon Translative streaming使您能够发送音频流并实时接收回一流的文本. API使得开发者很容易在他们的应用程序中加入实时语音到文本的能力. 可以用来 为了各种目的。 例如:

  • 流转的转录可以产生实时字幕,供直播媒体使用.
  • 在法庭作证期间,律师可以在流传的录音记录上做实时说明.
  • 电子游戏聊天可以实时转录,使主机可以温和内容或运行实时分析.
  • 流转记录可以帮助听力受损者.

JavaScript SDK Translating 客户端将 API 封装成一个可以运行的 JavaScript 库 浏览器、节点.js和 潜在的 反应原生. 默认情况下,客户端在Node.js上使用HTTP/2连接,并使用 WebSocket 在浏览器和 React Industrial 上连接.

安装

要安装此软件包, 只需输入或安装@aws- sdk/ client- translate- sreaming 使用您最喜欢的软件包管理器 :

  • npm install @aws-sdk/client-transcribe-streaming
  • yarn add @aws-sdk/client-transcribe-streaming
  • pnpm add @aws-sdk/client-transcribe-streaming

开始

在文中,我们将用一个使用 startStreamTranscription 方法 将英语发言转译为文本.

如果你没有,请读 根读取 用于创建样本应用程序的指导 安装。 安装后,在 index.js,您可以导入 Translax Streaming 客户端,例如:

// ES5 example
const { TranscribeStreamingClient, StartStreamTranscriptionCommand } = require("@aws-sdk/client-transcribe-streaming");

若为 require 无法在您正在工作的平台( 浏览器) 上访问 。 您可以导入客户端 :

// ES6+ example
import {
  TranscribeStreamingClient,
  StartMedicalStreamTranscriptionCommand,
} from "@aws-sdk/client-transcribe-streaming";

构建服务客户端

您可以创建一个服务客户端, 如 Bellow :

const client = new TranscribeStreamingClient({
  region,
  credentials,
});
// region and credentials are optional in Node.js

获取语音流

Translatising 客户端接受流式语音输入为 可同步执行。您可以从同步生成器或使用 Symbol.asyncIterable 以释放二进制块.

以下是使用同步生成器的例子:

const audioStream = async function* () {
  await device.start();
  while (device.ends !== true) {
    const chunk = await device.read();
    yield chunk; /* yield binary chunk */
  }
};

然后,您需要将二进制块构建成一个SDK可以识别的音频块形状:

const audioStream = async function* () {
  for await (const chunk of audioSource()) {
    yield { AudioEvent: { AudioChunk: chunk } };
  }
};

获得Node.js Stream API

在Node.js,你将主要获得演讲在 流动 API,来自 HTTP 请求或设备。 在节点.js中流出 API (XQ) 10.0.0)本身是同源活性. 您可以提供流入 SDK 输入而无需明确转换 。 你个 只需要构造 SDK 可以识别的音频块形状 :

const audioSource = req; //Incoming message
const audioStream = async function* () {
  for await (const payloadChunk of audioSource) {
    yield { AudioEvent: { AudioChunk: payloadChunk } };
  }
};

如果您看到, 请不要限制客户端的块大小, 例如, 溪流来自 fs,你可能会看到 The chunk is too big 转写流出错误 。 通过设置 HighWaterMark数字 :

const { PassThrough } = require("stream");
const { createReadStream } = require("fs");
const audioSource = createReadStream("path/to/speech.wav");
const audioPayloadStream = new PassThrough({ highWaterMark: 1 * 1024 }); // Stream chunk less than 1 KB
audioSource.pipe(audioPayloadStream);
const audioStream = async function* () {
  for await (const payloadChunk of audioPayloadStream) {
    yield { AudioEvent: { AudioChunk: payloadChunk } };
  }
};

根据音频来源,您可能需要 PCM 编码 你的声音块.

从浏览器获取

TranListing SDK客户端也支持从浏览器中流出. 你可以通过 getUserMedia API 密码。请注意,该API得到以下支持: 浏览器的子集。 。 。 . 这是获取麦克风音频流的代码片段 microphone-stream

const mic = require("microphone-stream");
// this part should be put into an async function
micStream.setStream(
  await window.navigator.mediaDevices.getUserMedia({
    video: false,
    audio: true,
  })
);
const audioStream = async function* () {
  for await (const chunk of micStream) {
    yield { AudioEvent: { AudioChunk: pcmEncodeChunk(chunk) /* pcm Encoding is optional depending on the source */ } };
  }
};

你可以找到一个完整的前端例子 这里

PCM 编码

目前 Translatising 服务只接受 移动电话 编码 如果您的音频源尚未编码, 您需要将块编码为 PCM 。 举个例子:

const pcmEncodeChunk = (chunk) => {
  const input = mic.toRaw(chunk);
  var offset = 0;
  var buffer = new ArrayBuffer(input.length * 2);
  var view = new DataView(buffer);
  for (var i = 0; i < input.length; i++, offset += 2) {
    var s = Math.max(-1, Math.min(1, input[i]));
    view.setInt16(offset, s < 0 ? s * 0x8000 : s * 0x7fff, true);
  }
  return Buffer.from(buffer);
};

发送语音流

const command = new StartStreamTranscriptionCommand({
  // The language code for the input audio. Valid values are en-GB, en-US, es-US, fr-CA, and fr-FR
  LanguageCode: "en-US",
  // The encoding used for the input audio. The only valid value is pcm.
  MediaEncoding: "pcm",
  // The sample rate of the input audio in Hertz. We suggest that you use 8000 Hz for low-quality audio and 16000 Hz for
  // high-quality audio. The sample rate must match the sample rate in the audio file.
  MediaSampleRateHertz: 44100,
  AudioStream: audioStream(),
});
const response = await client.send(command);

处理文本流

如果请求成功,您将会收到包含流传记录的回复,如: 这个。 。 。 . 就像输入语音流, 记录流是一个 可同步执行 排放部分 笔录. 以下是访问记录的代码片段

// This snippet should be put into an async function
for await (const event of response.TranscriptResultStream) {
  if (event.TranscriptEvent) {
    const message = event.TranscriptEvent;
    // Get multiple possible results
    const results = event.TranscriptEvent.Transcript.Results;
    // Print all the possible transcripts
    results.map((result) => {
      (result.Alternatives || []).map((alternative) => {
        const transcript = alternative.Items.map((item) => item.Content).join(" ");
        console.log(transcript);
      });
    });
  }
}

管道转写流

在Node.js,你可以管这个 TranscriptResultStream 很容易到达其他目的地 from API 密码数字 :

const { Readable } = require("stream");
const transcriptsStream = Readable.from(response.TranscriptResultStream);
transcriptsStream.pipe(/* some destinations */);

处理错误

使用时 async...await 样式代码,您可以用 try...catch 块。 有两个 可抛出例外类别:

  • 在开始复制前立即提出例外,如签名 例外、无效参数例外和网络错误;
  • 笔录后流出例外 开始,像 InternalFailureException 或者说 ConflictException。 。 。 .

对于即时例外,SDK客户端会重试请求,如果错误可以重试,比如网络错误. 你们可以 指定客户端按你的意愿行事.

对于流出例外,因为流出记录已经 已启动, 客户端无法自动重试请求 。 客户端会丢弃这些例外,用户可以处理 流行为相应.

以下是错误处理流程的例子:

try {
  const response = await client.send(command);
  await handleResponse(response);
} catch (e) {
  if (e instanceof InternalFailureException) {
    /* handle InternalFailureException */
  } else if (e instanceof ConflictException) {
    /* handle ConflictException */
  }
} finally {
  /* clean resources like input stream */
}

本地反应的注释

此软件包与React Industrial(QQ 0.60)兼容. 但是,它没有经过任何反应土著图书馆的测试 将麦克风记录转换成流数据。 整合流式麦克风记录数据的社区投入是 欢迎.

谢谢你读了这本指南。 如果你想知道更多有关流是如何被编码, 如何建立连接, 请参考 服务 API 指南。 。 。 .

捐款

此客户端代码是自动生成的. 任何修改在下次修改时都会被覆盖 @aws-sdk/client-transcribe-streaming 软件包正在更新。 为了帮助客户,你可以检查我们 生成客户端脚本。 。 。 .

许可证

此 SDK 分布于 Apache 许可证,2.0版本, (中文(简体) ), 更多信息见LICENSE

版本与发布记录

@aws-sdk/client-transcribe-streaming 共发布 748 个版本,首次发布于 2020/01/08;过去 12 个月发布了 221 个版本。

版本发布日期距今
3.1164.0latest2026/11/07今天
3.1163.02026/11/061 天前
3.1162.02026/11/052 天前
3.1161.02026/10/318 天前
3.1160.02026/10/309 天前
3.1159.02026/10/2910 天前

其他发布通道:alpha → 1.0.0-alpha.24,beta → 1.0.0-beta.5,gamma → 1.0.0-gamma.11,rc → 3.5.1-rc.0。

关于 @aws-sdk/client-transcribe-streaming 的常见问题

@aws-sdk/client-transcribe-streaming 是什么?

JavaScript Translating Clients for Node.js 的 AWS SDK, 浏览器和 React Industrial 的应用程序

如何安装 @aws-sdk/client-transcribe-streaming?

在项目目录运行 npm install @aws-sdk/client-transcribe-streaming,或使用 pnpm add / yarn add / bun add。

@aws-sdk/client-transcribe-streaming 支持 TypeScript 吗?

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

@aws-sdk/client-transcribe-streaming 支持 ESM 还是 CommonJS?

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

@aws-sdk/client-transcribe-streaming 需要什么版本的 Node.js?

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

@aws-sdk/client-transcribe-streaming 可以商用吗?

@aws-sdk/client-transcribe-streaming 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@aws-sdk/client-transcribe-streaming 的下载量有多少?

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

@aws-sdk/client-transcribe-streaming 还在维护吗?

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