我正在尝试将Google云文本到语音API音频文件写入Firebase存储。Node.js
documentation
显示如何使用
fs.writeFile
file.writefile
或
fs.createWriteStream
我修改了
文档
例如,Firebase Cloud函数正在写入Firebase存储:
exports.Google_T2S = functions.firestore.document('Users/{userID}/Spanish/T2S_Request').onUpdate((change, context) => {
if (change.after.data().word != undefined) {
const word = change.after.data().word;
const longLanguage = 'Spanish';
const audioFormat = '.mp3';
const fs = require('fs');
const util = require('util');
const textToSpeech = require('@google-cloud/text-to-speech'); // Imports the Google Cloud client library
const client = new textToSpeech.TextToSpeechClient(); // Creates a client
const text = word; // The text to synthesize
let myWordFile = word.replace(/ /g,"_"); // replace spaces with underscores in the file name
myWordFile = myWordFile.toLowerCase(); // convert the file name to lower case
myWordFile = myWordFile + audioFormat; // append .mp3 to the file name;
// boilerplate copied from https://cloud.google.com/blog/products/gcp/use-google-cloud-client-libraries-to-store-files-save-entities-and-log-data
const {Storage} = require('@google-cloud/storage');
const storage = new Storage();
const bucket = storage.bucket('myApp.appspot.com');
const file = bucket.file('Audio/' + longLanguage + '/' + myWordFile);
const request = { // Construct the request
input: {text: text},
// Select the language and SSML Voice Gender (optional)
voice: {languageCode: 'es-ES', ssmlGender: 'FEMALE'},
// Select the type of audio encoding
audioConfig: {audioEncoding: 'MP3'},
};
async function test() {
try {
// Performs the Text-to-Speech request
const [response] = await client.synthesizeSpeech(request);
// Write the binary audio content to a local file
const writeFile = util.promisify(fs.writeFile);
await writeFile(file, response.audioContent, 'binary');
console.log('Audio content written to file: output.mp3');
} catch (error) {
console.error(error);
}
}
test();
}
});
TypeError: path must be a string or Buffer
at fs.writeFile (fs.js:1277:6)
这是说问题在于参数
file
排队
await writeFile(file, response.audioContent, 'binary');
根据
documentation
写入文件
fs.createWriteStream文件
? 或者我应该改变
写入文件
到
file.writeFile
?
另外,为什么Google云的文本到语音API
文档
使用
await
async
?