AWS Polly v3:SynthesizeSpeech 'mp3',AudioStream 不是 instanceOf Buffer

Jef*_*ner 6 node.js amazon-polly

我正在将Javascript(节点)中的AWS Polly代码从v2转换为v3。

使用AWS Polly v3 synthesizeSpeech()时,如何将返回的值转换data.AudioStream为 的实例Buffer?

以下 JavaScript 代码使用v2运行,返回的audio.AudioStream是 的实例Buffer:

const { Polly } = require('aws-sdk');

// Create an Polly client
const polly = new Polly({
    signatureVersion: 'v4',
    region: process.env.AWS_DEFAULT_REGION
});

const params = {
    OutputFormat: 'mp3',
    VoiceId: voiceId,
    TextType: 'ssml',
    Text: '<speak><prosody rate="85%">Hello World</prosody></speak>'
};

// ***

return await polly
    .synthesizeSpeech(params)
    .promise()
    .then(data => {
         if (data.AudioStream instanceof Buffer) {
             return data.AudioStream
         } else {
             console.error('AudioStream not instanceof Buffer');
             return null;
         }
     })
Run Code Online (Sandbox Code Playgroud)

以下使用v3 的JavaScript 代码,其来自synthesizeSpeech()contains的响应audio.AudioStream不是 的实例Buffer,而是以 开头IncomingMessage:

const {
    Polly
} = require("@aws-sdk/client-polly");

// Create an Polly client
const polly = new Polly({
    signatureVersion: 'v4',
    region: process.env.AWS_DEFAULT_REGION
});

// ***

const params = {
    OutputFormat: 'mp3',
    VoiceId: voiceId,
    TextType: 'ssml',
    Text: '<speak><prosody rate="85%">Hello World</prosody></speak>'
};

return await polly
    .synthesizeSpeech(params)
    .then(data => {
         if (data.AudioStream instanceof Buffer) {
             return data.AudioStream
         } else {
             console.error('AudioStream not instanceof Buffer');
             return null;
         }
     })
Run Code Online (Sandbox Code Playgroud)

我都使用and尝试了以下v3,结果相同PollyClientSynthesizeSpeechCommand

const {
    PollyClient,
    SynthesizeSpeechCommand
} = require("@aws-sdk/client-polly");

const client = new PollyClient({
    region: process.env.AWS_DEFAULT_REGION
});

// ***

const command = new SynthesizeSpeechCommand({
    OutputFormat: 'mp3',
    Text:'hello world, this is alex',
    VoiceId: 'Kimberly',
    TextType: 'text'
});

return client
    .send(command)
    .then((data) => {
        if (data.AudioStream instanceof Buffer) {
            return data.AudioStream
        } else {
            console.error('AudioStream not instanceof Buffer');
            console.log('data.AudioStream:', data.AudioStream);
            return null;
        }
    })
    .catch((error) => {
        console.error(error);
    });
Run Code Online (Sandbox Code Playgroud)

v3 的内容data.AudioStream是:

IncomingMessage {
  _readableState:
   ReadableState {
     objectMode: false,
     highWaterMark: 16384,
     buffer: BufferList { head: null, tail: null, length: 0 },
     length: 0,
     pipes: null,
     ***
Run Code Online (Sandbox Code Playgroud)

感谢您的阅读,如有任何反馈,我们将不胜感激。

小智 5

现在是一个 ReadableStream 对象。

   import { Readable } from "stream";
    //...
    if (data.AudioStream instanceof Readable) {
      console.log("Readable");
      data.AudioStream.pipe(fs.createWriteStream("fileName.mp3"));
    } else {
      console.log("no Readable");
    }
    //...
Run Code Online (Sandbox Code Playgroud)

您可以在此处找到有关此更改的详细信息: https://github.com/aws/aws-sdk-js-v3/issues/1096#issuecomment-620900466