【问题标题】:Recorded audio/webm as input to Google Speech-To-Text API录制的音频/webm 作为 Google Speech-To-Text API 的输入
【发布时间】:2023-03-14 21:14:01
【问题描述】:

我目前使用 React 作为我的前端和 Java Spring Boot 作为我的服务器。我正在使用 React-Mic 录制音频,将音频传递给 FormData 并将该 FormData 作为正文的 HTTP 发布请求发送到我的 Java 服务器。但是,由于录制的音频在 webm 中,因此 Google Speech-To-Text API 没有适当的编码。知道如何将音频转换为 flac 或 Google Speech-To-Text API 支持的任何其他格式类型吗?

【问题讨论】:

    标签: java speech-to-text google-speech-api


    【解决方案1】:

    可能使用 JAVE2 将 webm 转换为 mp3(或其他)。

    https://github.com/a-schild/jave2

    自述文件中的示例应该为您指明正确的方向:

    try {                                                         
     File source = new File("file path"); // Path to your webm                   
     File target = new File("file path");  // Output path   
    
     //Audio Attributes                                       
     AudioAttributes audio = new AudioAttributes();              
     audio.setCodec("libmp3lame"); // Change this to flac if you prefer flac                               
     audio.setBitRate(128000);                                   
     audio.setChannels(2);                                       
     audio.setSamplingRate(44100);                               
    
     //Encoding attributes                                       
     EncodingAttributes attrs = new EncodingAttributes();        
     attrs.setFormat("mp3"); // Change to flac if you prefer flac                                     
     attrs.setAudioAttributes(audio);                            
    
     //Encode                                                    
     Encoder encoder = new Encoder();                            
     encoder.encode(new MultimediaObject(source), target, attrs); 
     // The target file should now be present at the path specified above
    
    
    } catch (Exception ex) {                                      
     ex.printStackTrace();                                        
    }                     
    

    转换后,您将拥有一个文件对象,您可以将其转换为 byte[] 以发送到语音到文本 api,如下例所示:

    https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/speech/cloud-client/src/main/java/com/example/speech/QuickstartSample.java

    【讨论】:

    猜你喜欢
    • 2016-03-12
    • 1970-01-01
    • 1970-01-01
    • 2018-06-15
    • 2020-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多