我正在与Google Text To Speech (TTS)合作,以便将生成的音频文件保存到Google Cloud Storage (GCS)。我可以将 TTS 的结果成功写入文件,因为我可以在日志中看到结果。问题是我无法访问已写入的文件并将该文件上传到 GCS 。
我正在使用path.join(os.tmpdir(), fileName)
尝试检索以functions-samples/quickstarts/thumbnails作为参考写入的文件,因为它会将文件上传到 GCP here
Error: Cannot parse JSON response
at ApiError (/user_code/node_modules/@google-cloud/storage/node_modules/@google-cloud/common/build/src/util.js:43:9)
at Util.parseHttpRespBody (/user_code/node_modules/@google-cloud/storage/node_modules/@google-cloud/common/build/src/util.js:167:42)
at Util.handleResp (/user_code/node_modules/@google-cloud/storage/node_modules/@google-cloud/common/build/src/util.js:116:117)
at retryRequest (/user_code/node_modules/@google-cloud/storage/node_modules/@google-cloud/common/build/src/util.js:403:22)
at onResponse (/user_code/node_modules/@google-cloud/storage/node_modules/retry-request/index.js:200:7)
at /user_code/node_modules/@google-cloud/storage/node_modules/teeny-request/build/src/index.js:158:17
at process._tickDomainCallback (internal/process/next_tick.js:135:7)
const admin = require('firebase-admin');
const functions = require('firebase-functions');
const path = require('path');
const os = require('os');
const {Storage} = require('@google-cloud/storage');
const projectId = 'coinverse-media-staging';
const storage = new Storage({
projectId: projectId,
});
const fs = require('fs');
const textToSpeech = require('@google-cloud/text-to-speech');
const client = new textToSpeech.TextToSpeechClient();
admin.initializeApp();
const text = 'Hello, world!';
const request = {
input: {text: text},
// Select the language and SSML Voice Gender (optional)
voice: {languageCode: 'en-US', ssmlGender: 'NEUTRAL'},
// Select the type of audio encoding
audioConfig: {audioEncoding: 'MP3'},
};
exports.getAudiocast = functions.https.onCall((data, context) => {
var bucket = storage.bucket('gs://[project-name].appspot.com/content/feeds/en-audio/');
client.synthesizeSpeech(request, (err, response) => {
if (err) {
console.error('ERROR:', err);
return;
}
const fileName = (data.id + '.mp3');
const tempFile = path.join(os.tmpdir(), fileName);
var mp3File = fs.writeFile(tempFile, response.auioContent, 'binary', err => {
if (err) {
console.error('ERROR:', err);
return;
}
console.log('Audio content written to file: ' + data.id + '.mpeg');
});
var filePathToUpload = path.join(os.tmpdir(), fileName)
bucket.upload(filePathToUpload), function(err, file) {
if (!err) {
console.log('Audiocast uploaded!');
} else {
console.error('Audiocast upload error: ' + err.message);
}
};
});
return {
filePath: "[somePath]",
};
});
/tmp
目录。 const fileName = ('/tmp/' + data.id + '.mp3');
fs.writeFile(fileName, response.auioContent, 'binary', err => {
if (err) {
console.error('ERROR:', err);
return;
}
console.log('Audio content written to file: ' + fileName);
});
bucket.upload(fileName), function(err, file) {
if (!err) {
console.log('Audiocast uploaded!');
} else {
console.error('Audiocast upload error: ' + err.message);
}
};
与原件相同。
const fileName = ('/tmp/' + data.id + '.mp3');
var wstream = fs.createWriteStream(fileName);
wstream.on('finish', function () {
console.log('file has been written');
bucket.upload(fileName), function(err, file) {
if (!err) {
console.log('Audiocast uploaded!');
} else {
console.error('Audiocast upload error: ' + err.message);
}
};
});
wstream.write(response.audioContent);
wstream.end();
与原件相同。
研究如何将二进制音频内容写入临时文件还是本地文件。