使用 Microsoft Speech 发送文本 REST 时收到 400- 对 OGG 文件格式的错误请求 API
Getting 400- Bad request for OGG file format while using Microsoft Speech to text REST API
我正在使用 Microsoft Azure 语音文本 REST API。
根据文档,REST API 支持 Ogg 和 Wav 格式。但是,当我发送 OGG 音频文件请求时出现 400- Bad request 错误。
我正在使用以下代码准备请求,这适用于 WAV 音频格式:
String url= "https://westus.stt.speech.microsoft.com/speech/recognition/conversation/cognitiveservices/v1?language=en-US&format=simple";
private void connect(String extension) throws IOException {
connection = (HttpURLConnection) new URL(url).openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
if (extension.equalsIgnoreCase(Constants.WAV))
connection.setRequestProperty("Content-type", "audio/wav; codecs=\"audio/pcm\"; samplerate=16000");
else if (extension.equalsIgnoreCase(Constants.OGG))
connection.setRequestProperty("Content-type", "audio/ogg; codecs=\"audio/opus\"");
connection.setRequestProperty("Accept", "application/json;text/xml");
connection.setRequestProperty("Ocp-Apim-Subscription-Key", subscriptionKey);
connection.setRequestProperty("Transfer-Encoding", "chunked");
connection.setRequestProperty("Expect", "100-continue");
connection.setChunkedStreamingMode(0); // 0 == default chunk size
connection.connect();
}
使用它来上传文件:
private void upload(InputStream inputStream) throws IOException {
try (OutputStream output = connection.getOutputStream()) {
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) != -1) {
output.write(buffer, 0, length);
}
output.flush();
}}
API 适用于 WAV 和 OGG 格式。该请求看起来也很好。问题可能出在请求中发送的音频文件上。
如果要使用 OGG 音频文件格式,则音频文件必须具有以下属性:
OGG(编解码器:Opus,比特率:16 位,采样率:16 kHz,Chanel:单声道)
否则,您将收到 400 bad request 错误。
确保编解码器是 Opus,因为大多数 OGG 文件都有 API.
不支持的 Vorbis 编解码器
您可以使用 this 网站将音频文件转换为所需格式。
我正在使用 Microsoft Azure 语音文本 REST API。 根据文档,REST API 支持 Ogg 和 Wav 格式。但是,当我发送 OGG 音频文件请求时出现 400- Bad request 错误。
我正在使用以下代码准备请求,这适用于 WAV 音频格式:
String url= "https://westus.stt.speech.microsoft.com/speech/recognition/conversation/cognitiveservices/v1?language=en-US&format=simple";
private void connect(String extension) throws IOException {
connection = (HttpURLConnection) new URL(url).openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
if (extension.equalsIgnoreCase(Constants.WAV))
connection.setRequestProperty("Content-type", "audio/wav; codecs=\"audio/pcm\"; samplerate=16000");
else if (extension.equalsIgnoreCase(Constants.OGG))
connection.setRequestProperty("Content-type", "audio/ogg; codecs=\"audio/opus\"");
connection.setRequestProperty("Accept", "application/json;text/xml");
connection.setRequestProperty("Ocp-Apim-Subscription-Key", subscriptionKey);
connection.setRequestProperty("Transfer-Encoding", "chunked");
connection.setRequestProperty("Expect", "100-continue");
connection.setChunkedStreamingMode(0); // 0 == default chunk size
connection.connect();
}
使用它来上传文件:
private void upload(InputStream inputStream) throws IOException {
try (OutputStream output = connection.getOutputStream()) {
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) != -1) {
output.write(buffer, 0, length);
}
output.flush();
}}
API 适用于 WAV 和 OGG 格式。该请求看起来也很好。问题可能出在请求中发送的音频文件上。 如果要使用 OGG 音频文件格式,则音频文件必须具有以下属性: OGG(编解码器:Opus,比特率:16 位,采样率:16 kHz,Chanel:单声道)
否则,您将收到 400 bad request 错误。 确保编解码器是 Opus,因为大多数 OGG 文件都有 API.
不支持的 Vorbis 编解码器您可以使用 this 网站将音频文件转换为所需格式。