首页 > 解决方案 > 使用 NodeJs 从 Amazon Polly 获取语音标记

问题描述

我正在做一个动画项目,为我的角色所说的内容添加字幕。我可以毫无问题地从 AWS Polly 获取 mp3 文件。

但是,当我想分别获取单词的每个部分时,它不起作用。我检查了检查器选项卡,我可以看到一些参数正在传递给polly.aws的请求。知道如何让 json/mark-up 文件知道每个单词和句子的开头和结尾吗?

const AWS = require('aws-sdk')
const Fs = require('fs')

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

// # this part works fine
let params = {
    'Text': 'Hi, my name is Soley. We are building something amazing!',
    'OutputFormat': 'mp3',
    'VoiceId': 'Matthew'
}

// # from chrome's network tab:
// # and is there a way to get mp3 and mark-up text at the same time?
// "text": "Hi, my name is Soley. We are building something amazing!",
//     "textContentType": "text",
//     "voiceId": "Matthew",
//     "languageCode": "en-US",
//     "engine": "standard",
//     "outputFormat": "json-8000",
//     "lexiconNames": [],
//     "speechMarksTypes": [
//         "word",
//         "sentence"
//     ]

Polly.synthesizeSpeech(params, (err, data) => {
    if (err) {
        console.log(err)
    } else if (data) {
        console.log(data)
        if (data.AudioStream instanceof Buffer) {
            Fs.writeFile("speech."+params.OutputFormat, data.AudioStream, function (err) {
                if (err) {
                    return console.log(err)
                }
                console.log("The file was saved!")
            })
        }
    }
})

一些有用的检查链接:https ://aws.amazon.com/blogs/aws/new-amazon-polly-speech-marks/

使用 cli 也可以使用文件:https : //docs.aws.amazon.com/polly/latest/dg/speechmarkexamples.html 但我想在 NodeJs 中使用它

标签: amazon-web-servicestext-to-speechamazon-polly

解决方案


哦,我想我发现了一些东西:

let params = {
    'Text': 'Hi, my name is Soley. We are building something amazing!',
    'OutputFormat': 'json',
    'VoiceId': 'Matthew',
    'SpeechMarkTypes': ['word', 'sentence']
}

感谢javahttps ://docs.aws.amazon.com/polly/latest/dg/SynthesizeSpeechMarksSample.html


推荐阅读