Java 录制音频/webm作为Google语音到文本API的输入

Java 录制音频/webm作为Google语音到文本API的输入,java,speech-to-text,google-speech-api,Java,Speech To Text,Google Speech Api,我目前使用React作为前端,使用javaspringboot作为服务器。我正在使用React-Mic录制音频,将音频传递给FormData,并将该FormData作为主体发送一个HTTP post请求到我的Java服务器。但是,由于录制的音频在webm中,因此没有适合Google Speech To Text API的编码。你知道如何将音频转换为flac或谷歌语音文本API支持的任何其他格式类型吗?可能会使用JAVE2将webm转换为mp3(或其他格式) 自述文件中的示例应为您指明正确的方向

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

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

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

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();                                        
}                     
转换后,您将拥有一个文件对象,可以将其转换为字节[],以发送到语音到文本api,如本示例所示:


我收到的音频文件是MultipartFile而不是file。如何转换成文件?@WeiTang这可能有用