如何在Amazon Polly中将语音标记输出转换为JSON对象数组?

问题描述

我已经成功地将语音标记输出写入文件。 但是我想知道如何将输出内容转换为JSON对象,以便可以在代码中使用它们。

这是我的工作代码,可将语音标记输出写入文件

const AWS = require('aws-sdk')
var fs = require('fs');

const Polly = new AWS.Polly({
    signatureversion: 'v4',region: 'us-east-1'
})

const params = {
    'Text': 'Hi,my name is John','OutputFormat': 'json','VoiceId': 'Kimberly','SpeechMarkTypes': ['word']
}

Polly.synthesizeSpeech(params,(err,data) => {
    if (err) {
        console.log(err.code);
    } else if (data) {
        if (data.AudioStream instanceof Buffer) {
            fs.writeFile("speech.json",data.AudioStream,function (err) {
                if (err) {
                    return console.log(err);
                }
                console.log("The file was saved!");
            });
            // How can I get an array of JSON objects from data.AudioStream?
        }
    }
});

如果运行代码,它将写入文件speech.json,其内容如下:

{"time":6,"type":"word","start":0,"end":2,"value":"Hi"}
{"time":587,"start":4,"end":6,"value":"my"}
{"time":754,"start":7,"end":11,"value":"name"}
{"time":1147,"start":12,"end":14,"value":"is"}
{"time":1305,"start":15,"end":19,"value":"John"}

如何将其转换为JSON对象数组,以便可以在代码中使用它或将其发送给客户端?

解决方法

我找到了答案:

Polly.synthesizeSpeech(params,(err,data) => {
    if (err) {
        console.log(err.code);
    } else if (data) {
        if (data.AudioStream instanceof Buffer) {
            const buf = Buffer.from(data.AudioStream);
            const content = buf.toString();
            const lines = content.split("\n");
            if (!lines[lines.length -1]) {
                lines.pop();
            }
            for (line of lines) {
                const obj = JSON.parse(line);
                console.log(obj);
            }
        }
    }
});

结果:

{ time: 6,type: 'word',start: 0,end: 2,value: 'Hi' }
{ time: 587,start: 4,end: 6,value: 'my' }
{ time: 754,start: 7,end: 11,value: 'name' }
{ time: 1147,start: 12,end: 14,value: 'is' }
{ time: 1305,start: 15,end: 19,value: 'John' }