javascript - Google Text-to-speech - 如何从文件中读取文本输入?
问题描述
我在 Node.js 中使用 Google TextToSpeech API 从文本生成语音。我能够获得与为语音生成的文本同名的输出文件。但是,我需要稍微调整一下。我希望我可以同时生成多个文件。关键是我有,例如,5 个单词(或句子)要生成,例如 cat、dog、house、sky、sun。我想将它们分别生成到一个单独的文件中:cat.wav、dog.wav 等。
我还希望应用程序能够从 * .txt 文件中读取这些单词(每个单词/句子在 * .txt 文件的单独行上)。
有这种可能吗?下面我将粘贴 * .js 文件代码和我正在使用的 * .json 文件代码。
*.js
const textToSpeech = require('@google-cloud/text-to-speech');
const fs = require('fs');
const util = require('util');
const projectId = 'forward-dream-295509'
const keyFilename = 'myauth.json'
const client = new textToSpeech.TextToSpeechClient({ projectId, keyFilename });
const YourSetting = fs.readFileSync('setting.json');
async function Text2Speech(YourSetting) {
const [response] = await client.synthesizeSpeech(JSON.parse(YourSetting));
const writeFile = util.promisify(fs.writeFile);
await writeFile(JSON.parse(YourSetting).input.text + '.wav', response.audioContent, 'binary');
console.log(`Audio content written to file: ${JSON.parse(YourSetting).input.text}`);
}
Text2Speech(YourSetting);
*.json
{
"audioConfig": {
"audioEncoding": "LINEAR16",
"pitch": -2,
"speakingRate": 1
},
"input": {
"text": "Text to Speech"
},
"voice": {
"languageCode": "en-US",
"name": "en-US-Wavenet-D"
}
}
我不太擅长编程。我在 google 上找到了一个关于如何执行此操作的教程,并对其进行了一些修改,以便保存文件的名称与生成的文本相同。
我将非常感谢您的帮助。阿雷克
解决方案
推荐阅读
- node.js - module.exports 与环境变量
- javascript - 我可以从应用程序将 cors 扩展到路由器吗?
- html - 带有箭头图标的角度自定义排序
- reactjs - 对未安装的组件做出反应状态更新。这是一个无操作,但它表明您的应用程序中存在内存泄漏
- python - VSCode Python 中未解决的导入
- ios - 无法使用 AVPlayerViewController 显示电影
- javascript - 如何在javascript中过滤后添加对象值
- python - 为列表中的相似值返回相同位置
- php - 如何在 bwowser 刷新时仍然加载 vueRouter 路由?
- r - 有没有办法使用列在 R 中标记我的变量