我是 Azure 新手,也是一般编程新手。
背景
我正在构建一个 Google Sheet 应用程序脚本,它将 Sheet2!B1 的内容发送到 Azure Speech API,并将生成的语音输出到 Sheet2!B2。但是,尽管使用
X-Microsoft-OutputFormat
作为 riff-48khz-16bit-mono-pcm
,输出音频文件仍无法播放。我尝试根据我在这里看到的答案将输出更改为
audio-24khz-160kbitrate-mono-mp3
,但它也不起作用。我也尝试过将文件类型更改为 .wav,但这也不起作用。
这是代码。你能建议我应该做什么吗?此外,任何关于创建批量 api 请求的提示都值得赞赏。谢谢你。
function sendTextToAzureTTS() {
// Azure Text to Speech endpoint and API key
var endpoint = "https://eastus.tts.speech.microsoft.com/cognitiveservices/v1";
var apiKey = "hidden";
// Get the plain text from Sheet2 B1
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet2");
var text = sheet.getRange("B1").getValue();
// Create the request payload
var requestData = {
method: "POST",
contentType: "application/ssml+xml",
headers: {
"Ocp-Apim-Subscription-Key": apiKey,
"X-Microsoft-OutputFormat": "audio-24khz-160kbitrate-mono-mp3", // Use the supported format
},
payload: '<speak version="1.0" xml:lang="en-US"><voice xml:lang="en-US" xml:gender="Female" name="en-US-EmmaNeural">' + text + '</voice></speak>',
};
// Make the POST request to Azure Text to Speech
var response = UrlFetchApp.fetch(endpoint, requestData);
// Check if the response was successful
if (response.getResponseCode() == 200) {
// Get the audio data from the response
var audioData = response.getBlob().getBytes();
// Write the audio data to a file in Google Drive with the correct MIME type
var audioFile = DriveApp.createFile('otuput_audio.mp3', audioData, "audio/mp3");
// Get the URL of the created audio file
var audioUrl = audioFile.getUrl();
// Write the audio URL to Sheet2 B2
sheet.getRange("B2").setValue(audioUrl);
} else {
Logger.log("Error: " + response.getResponseCode() + " - " + response.getContentText());
}
}
我尝试创建天蓝色语音发布请求并通过 Google Sheet 应用程序脚本取回音频文件。但是,音频文件未播放。我期望一个可以工作的音频文件,但该音频文件没有播放。
我创建了一个 Google 电子表格并将其命名为
TextToSpeech
。将触发器添加到以下电子表格中。
代码:
function sendTextToAzureTTS() {
// Azure Text to Speech region and access key
var region = "eastus"; // Replace with your Azure region
var apiKey = "your-api-key"; // Replace with your Azure access key
var endpoint = "your speech servive endpoint";
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("TextToSpeech");
var text = sheet.getRange("B1").getValue();
var requestData = {
method: "POST",
contentType: "application/ssml+xml",
headers: {
"Ocp-Apim-Subscription-Key": apiKey,
"X-Microsoft-OutputFormat": "audio-24khz-160kbitrate-mono-mp3",
},
payload: '<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis" xmlns:mstts="https://www.w3.org/2001/mstts" xml:lang="en-US"><voice name="en-US-AriaNeural">' + text + '</voice></speak>',
};
var response = UrlFetchApp.fetch(endpoint, requestData);
if (response.getResponseCode() == 200) {
var audioData = response.getBlob().getBytes();
var audioFile = DriveApp.createFile('output_audio.mp3', audioData, "audio/mpeg");
var audioUrl = audioFile.getUrl();
// Write the audio URL to the same sheet in cell B2
sheet.getRange("B2").setValue(audioUrl);
} else {
Logger.log("Error: " + response.getResponseCode() + " - " + response.getContentText());
}
}
输出: