我正在使用Node.js中的谷歌TextToSpeech应用程序接口从文本生成语音。我能够获得一个与为演讲生成的文本同名的输出文件。但是,我需要对此进行一些调整。我希望我可以同时生成多个文件。重点是,例如,我有5个单词(或句子)要生成,例如猫,狗,房子,天空,太阳。我想将它们分别生成到一个单独的文件中: 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上找到了一个关于如何做到这一点的教程,并对其进行了稍微的修改,以便保存的文件的名称与生成的文本相同。
如果你能帮忙,我将不胜感激。阿雷克
发布于 2021-04-28 00:58:14
这就对了--我还没有测试过它,但是这应该展示了如何读取一个文本文件,拆分成每一行,然后使用设置的并发性对其运行tts。它使用p-any和filenamify npm包,您需要将它们添加到您的项目中。请注意,google可能有API节流或速率限制,我在这里没有考虑到-如果这是一个问题,可以考虑使用p-throttle库。
// https://www.npmjs.com/package/p-map
const pMap = require('p-map');
// https://github.com/sindresorhus/filenamify
const filenamify = require('filenamify');
const textToSpeech = require('@google-cloud/text-to-speech');
const fs = require('fs');
const path = require('path');
const projectId = 'forward-dream-295509'
const keyFilename = 'myauth.json'
const client = new textToSpeech.TextToSpeechClient({ projectId, keyFilename });
const rawSettings = fs.readFileSync('setting.json', { encoding: 'utf8'});
// base data for all requests (voice, etc)
const yourSetting = JSON.parse(rawSettings);
// where wav files will be put
const outputDirectory = '.';
async function Text2Speech(text, outputPath) {
// include the settings in settings.json, but change text input
const request = {
...yourSetting,
input: { text }
};
const [response] = await client.synthesizeSpeech(request);
await fs.promises.writeFile(outputPath, response.audioContent, 'binary');
console.log(`Audio content written to file: ${text} = ${outputPath}`);
// not really necessary, but you could return something if you wanted to
return response;
}
// process a line of text - write to file and report result (success/error)
async function processLine(text, index) {
// create output path based on text input (use library to ensure it's filename safe)
const outputPath = path.join(outputDirectory, filenamify(text) + '.wav');
const result = {
text,
lineNumber: index,
path: outputPath,
isSuccess: null,
error: null
};
try {
const response = await Text2Speech(text, outputPath);
result.isSuccess = true;
} catch (error) {
console.warn(`Failed: ${text}`, error);
result.isSuccess = false;
result.error = error;
}
return result;
}
async function processInputFile(filepath, concurrency = 3) {
const rawText = fs.readFileSync(filepath, { encoding: 'utf8'});
const lines = rawText
// split into one item per line
.split(/[\r\n]+/)
// remove surrounding whitespace
.map(s => s.trim())
// remove empty lines
.filter(Boolean);
const results = await pMap(lines, processLine, { concurrency });
console.log('Done!');
console.table(results);
}
// create sample text file
const sampleText = `Hello World
cat
dog
another line of text`;
fs.writeFileSync('./my-text-lines.txt', sampleText);
// process each line in the text file, 3 at a time
processInputFile('./my-text-lines.txt', 3);https://stackoverflow.com/questions/67245989
复制相似问题