我正在使用 Google Apps 脚本来调用 GCP 中的文本转语音功能。我收到回复,但它是文本格式而不是 mp3。如何将其另存为 mp3?
这是代码:
function gcp_text_to_speech(){
var payload = {
'input':{
'text':'I\'ve added the event to your calendar.'
},
'voice':{
'languageCode':'en-gb',
'name':'en-GB-Standard-A',
'ssmlGender':'FEMALE'
},
'audioConfig':{
'audioEncoding':'MP3'
}
};
var response = UrlFetchApp.fetch(
"https://texttospeech.googleapis.com/v1/text:synthesize", {
method: "POST",
headers: {
"Authorization" : "Bearer " + getService().getAccessToken()
},
contentType: "application/json",
payload: JSON.stringify(payload),
muteHttpExceptions: true
});
var mp3 = response.getBlob()
var res = Drive.Files.insert({title: ".mp3"}, mp3)
}
我在 getAs(contentType)
中没有看到任何音频选项。可能吗?
这些都不起作用。我尝试过:
response.getBlob('audioContent')
response.getContent('audioContent')
JSON.parse(response).audioContent.getBlob()
response.getBlob(JSON.parse(response).audioContent)
response.getAs('audio/mpeg')
response.getAs('mp3')
最佳答案
解码您的 Base64 字符串响应,然后从中创建一个 blob。然后将文件插入驱动器。
var decoded = Utilities.base64Decode(JSON.parse(response).audioContent, Utilities.Charset.UTF_8);
var blob = Utilities.newBlob(decoded, 'audio/mpeg', "output.mp3");
var res = Drive.Files.insert({'title': 'test.mp3', 'mimeType': 'audio/mpeg'}, mp3)
关于google-apps-script - 谷歌应用程序脚本: response "audioContent" from GCP text-to-speech coming back as text file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69194442/