google-apps-script - 谷歌应用程序脚本: response "audioContent" from GCP text-to-speech coming back as text file

标签 google-apps-script google-cloud-platform text-to-speech

我正在使用 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/

相关文章:

security - 谷歌CP : Allowing Public Ingress Web Traffic from the Load Balancer ONLY

python - 如何使用 Python Azure 文本转语音 api 生成 mp3 文件

xml - 在 Google 表格中使用 ImportXML 的 <address> 标记的 XPath

google-apps-script - Google小工具/电子表格:抓取小工具的电子表格 key

python - 在 Python 中使用 Apps Script Execution API 时出现 404 错误

javascript - 如何分解长文本文件的内容以适应谷歌应用程序脚本中的多个变量?

amazon-web-services - 如何使用 aws-cli 访问 Google Cloud Storage 存储桶

python - 使用 apache beam/google 云数据流读取多行 JSON

unity-game-engine - (Unity 5.6) (Watson SDK) 我的 'text to speech' 小部件不会解析/播放关联的字符串

ibm-cloud - 我如何集成Bluemix对​​话和“语音转文本”以及“文本转语音”