I am having problem while converting audio file to text using google speech to text. I am able to download the file from Twilio but when I supply that audio file to google speech then it gives me 0 length response. But if I convert this downloaded file using vlc media player and then supply it to google speech then it gives me right output. Please help me on this I am stuck for about a week now.
After getting response from Twilio I save it in a file with .wav extension
InputStream in = new URL(jsonObject.get("redirect_to").toString()).openStream();
Files.copy(in, Paths.get("src/main/resources/mp.wav"), StandardCopyOption.REPLACE_EXISTING);
Below is the google speech to text code.
Path path = Paths.get("src/main/resources/mp.wav");
byte[] content = Files.readAllBytes(path);
ByteString audioBytes = ByteString.copyFrom(content);
try (SpeechClient speech = SpeechClient.create()) {
RecognitionConfig recConfig =
RecognitionConfig.newBuilder()
.setEncoding(RecognitionConfig.AudioEncoding.LINEAR16)
.setLanguageCode("en-US")
.setSampleRateHertz(44100)
.setModel("default")
.setAudioChannelCount(2)
.build();
RecognitionAudio recognitionAudio = RecognitionAudio.newBuilder().setContent(audioBytes).build();
OperationFuture<LongRunningRecognizeResponse, LongRunningRecognizeMetadata> response =
speech.longRunningRecognizeAsync(recConfig, recognitionAudio);
while (!response.isDone()) {
System.out.println("Waiting for response...");
Thread.sleep(10000);
}
List<SpeechRecognitionResult> results = response.get().getResultsList();
for (SpeechRecognitionResult result : results) {
// There can be several alternative transcripts for a given chunk of speech. Just use the
// first (most likely) one here.
SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
System.out.printf("Transcription: %s%n", alternative.getTranscript());
}
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}