使用 WebSockets 转录音频文件(Node.js)

此示例显示如何使用 IBM Watson Speech to Text 服务识别音频文件的类型,并在该文件中生成语音文本的转录。

此示例需要 Speech to Text 服务凭据Node.js

  1. Watson Developer Cloud Node.js SDK 安装 npm 模块 :
$ npm install watson-developer-cloud
  1. 创建一个 JavaScript 文件(例如, app.js )并将以下代码复制到其中。确保为 Speech to Text 服务实例输入 usernamepassword
var SpeechToTextV1 = require('watson-developer-cloud/speech-to-text/v1');
var fs = require('fs');

var speech_to_text = new SpeechToTextV1({
  username: 'INSERT YOUR USERNAME FOR THE SERVICE HERE',
  password: 'INSERT YOUR PASSWORD FOR THE SERVICE HERE',
  url: 'https://stream.watsonplatform.net/speech-to-text/api'
});

var params = {
  content_type: 'audio/flac'
};

// Create the stream,
var recognizeStream = speech_to_text.createRecognizeStream(params);

// pipe in some audio,
fs.createReadStream('0001.flac').pipe(recognizeStream);

// and pipe out the transcription.
recognizeStream.pipe(fs.createWriteStream('transcription.txt'));

// To get strings instead of Buffers from received `data` events:
recognizeStream.setEncoding('utf8');

// Listen for 'data' events for just the final text.
// Listen for 'results' events to get the raw JSON with interim results, timings, etc.   
['data', 'results', 'error', 'connection-close'].forEach(function(eventName) {
  recognizeStream.on(eventName, console.log.bind(console, eventName + ' event: '));
});
  1. 将示例音频文件 0001.flac 保存到同一目录。此示例代码设置为处理 FLAC 文件,但你可以修改示例代码的 params 部分以从其他格式的音频文件获取转录。支持的格式包括 WAV (类型 audio/wav), OGG (类型 audio/ogg)等。有关完整列表,请参阅 Speech to Text API 参考

  2. 运行应用程序(使用包含示例代码的文件的名称)

$ node app.js

运行应用程序后,你将在运行应用程序的目录中的文件 transcription.txt 中找到音频文件中的转录文本。