AWS Polly v3:SynthesizeSpeech 'mp3',AudioStream 不是 instanceOf Buffer
AWS Polly v3: SynthesizeSpeech 'mp3', AudioStream not instanceOf Buffer
我正在将 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;
}
})
以下 JavaScript 代码使用 v3,其来自 synthesizeSpeech()
的响应包含 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;
}
})
我都使用 PollyClient
和 SynthesizeSpeechCommand
尝试了以下 v3,结果相同
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);
});
v3 data.AudioStream
的内容是:
IncomingMessage {
_readableState:
ReadableState {
objectMode: false,
highWaterMark: 16384,
buffer: BufferList { head: null, tail: null, length: 0 },
length: 0,
pipes: null,
***
感谢您的阅读,欢迎任何反馈。
现在是一个 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");
}
//...
您可以在此处找到有关此更改的详细信息:
https://github.com/aws/aws-sdk-js-v3/issues/1096#issuecomment-620900466
就我个人而言,我发现使用 async/await 而不是流回调要容易得多...
import { PollyClient, SynthesizeSpeechCommand, SynthesizeSpeechInput } from "@aws-sdk/client-polly";
import fs from 'fs';
async function go() {
const client = new PollyClient(yourClientOptions)
const command = new SynthesizeSpeechCommand(yourCommandOptions)
try {
let data = await client.send(command)
if (!data || !data.AudioStream) throw Error(`bad response`);
await saveStream(data.AudioStream, "output.mp3")
}
catch(err) {
console.log(err)
}
}
async function saveStream(fromStream, filename) {
return new Promise((resolve, reject) => {
let toStream = fs.createWriteStream(filename)
toStream.on('finish', resolve);
toStream.on('error', reject);
fromStream.pipe(toStream)
})
}
go()
注意:我使用的是 node14,因此可以在顶部使用 import
,但您也可以轻松地使用 require
.
我正在将 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;
}
})
以下 JavaScript 代码使用 v3,其来自 synthesizeSpeech()
的响应包含 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;
}
})
我都使用 PollyClient
和 SynthesizeSpeechCommand
尝试了以下 v3,结果相同
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);
});
v3 data.AudioStream
的内容是:
IncomingMessage {
_readableState:
ReadableState {
objectMode: false,
highWaterMark: 16384,
buffer: BufferList { head: null, tail: null, length: 0 },
length: 0,
pipes: null,
***
感谢您的阅读,欢迎任何反馈。
现在是一个 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");
}
//...
您可以在此处找到有关此更改的详细信息: https://github.com/aws/aws-sdk-js-v3/issues/1096#issuecomment-620900466
就我个人而言,我发现使用 async/await 而不是流回调要容易得多...
import { PollyClient, SynthesizeSpeechCommand, SynthesizeSpeechInput } from "@aws-sdk/client-polly";
import fs from 'fs';
async function go() {
const client = new PollyClient(yourClientOptions)
const command = new SynthesizeSpeechCommand(yourCommandOptions)
try {
let data = await client.send(command)
if (!data || !data.AudioStream) throw Error(`bad response`);
await saveStream(data.AudioStream, "output.mp3")
}
catch(err) {
console.log(err)
}
}
async function saveStream(fromStream, filename) {
return new Promise((resolve, reject) => {
let toStream = fs.createWriteStream(filename)
toStream.on('finish', resolve);
toStream.on('error', reject);
fromStream.pipe(toStream)
})
}
go()
注意:我使用的是 node14,因此可以在顶部使用 import
,但您也可以轻松地使用 require
.