How to convert MultipartFile properly to match for Microsoft AudioConfig?

172 Views Asked by At

In my Spring Boot application, I accept an audiofile as MultipartFile with @RequestParam. I know, I can convert the file into some InputStream. I am also able to convert it into some byte array:

@PostMapping("/microsoft")
void transcribe(@RequestParam("file") MultipartFile file) {
  InputStream inputStream =  new BufferedInputStream(file.getInputStream());
  byte[] byteArr = file.getBytes();

  AudioConfig audioConfig = AudioConfig.???;    //here the correct method with my file needs to be called
}

For transcription using Microsoft API, I need to create some Audioconfig object. This is the link to the class. I used in the past fromWavFileInput(...) when I loaded a local audio file. But now, I need to be able to use the MultipartFile. I have no idea which method of the AudioConfig class I can use and how to convert the file correctly.

1

There are 1 best solutions below

0
On BEST ANSWER

The idea is, to create a temp file and transfer the MultipartFile into it:

File tempFile = File.createTempFile("prefix-", "-suffix");
file.transferTo(tempFile);

The use fromWavFileOutput with the path of the temporary file:

AudioConfig audioConfig = AudioConfig.fromWavFileOutput(tempFile.getPath());

For me, the temp file exceeds its maximum permitted size. To get this solved, make sure to add spring.servlet.multipart.max-file-size=-1 in your application.properties file to set limit to infinity.