使用Microsoft Speech to text REST API时,获取400- OGG文件格式的错误请求

问题描述 投票:1回答:1

我正在使用Microsoft Azure语音文本发送REST API。根据文档,REST API支持Ogg和Wav格式。但是,当我发送对OGG音频文件的请求时,出现400-错误的请求错误。

我正在使用以下代码来准备请求,并且这适用于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();
    }}
speech-to-text microsoft-cognitive bad-request
1个回答
2
投票

API适用于WAV和OGG格式。该请求也很好。问题可能出在请求中发送的音频文件。如果要使用OGG音频文件格式,则音频文件必须具有以下属性:OGG(编解码器:作品,比特率:16位,采样率:16 kHz,香奈儿:单声道)

否则,您将收到400错误的请求错误。确保编解码器为Opus,因为大多数OGG文件都具有API不支持的Vorbis编解码器。

您可以使用this网站转换所需格式的音频文件。

© www.soinside.com 2019 - 2024. All rights reserved.