0

I am using the ws.schild.jave library in my Java project to encode audio files, and I am having trouble setting the FFMPEG executable path. By default, the library searches for the FFMPEG executable in the temporary folder of the system, but I need to specify a different location for the executable.

    public byte[] convertWavToMp3(byte[] wavBytes) throws IOException, EncoderException {

    ByteArrayInputStream inputStream = new ByteArrayInputStream(wavBytes);


    File tempWavFile = File.createTempFile("input", ".wav");
    tempWavFile.deleteOnExit();

    // Copia l'array di byte del file WAV nel file temporaneo
    try (FileOutputStream fos = new FileOutputStream(tempWavFile)) {
        fos.write(wavBytes);
    }


    File tempFile = File.createTempFile("output", ".mp3");


    AudioAttributes audioAttrs = new AudioAttributes();
    audioAttrs.setCodec("libmp3lame");
    audioAttrs.setBitRate(new Integer(128000));
    audioAttrs.setChannels(new Integer(2));
    audioAttrs.setSamplingRate(new Integer(44100));


    EncodingAttributes encodingAttrs = new EncodingAttributes();
    encodingAttrs.setOutputFormat("mp3");
    encodingAttrs.setAudioAttributes(audioAttrs);



    Encoder encoder = new Encoder(locator);
    MultimediaObject mediaInput = new MultimediaObject(tempWavFile);
    encoder.encode(mediaInput, tempFile, encodingAttrs);

    // Leggi l'array di byte del file MP3
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    byte[] buffer = new byte[4096];
    int bytesRead;
    try (FileInputStream fis = new FileInputStream(tempFile)) {
        while ((bytesRead = fis.read(buffer)) != -1) {
            outputStream.write(buffer, 0, bytesRead);
        }
    }

    // Elimina il file temporaneo
    tempFile.delete();

    // Restituisce l'array di byte del file MP3
    return outputStream.toByteArray();
}

I have tried creating a CustomFFMPEGLocator class that extends the DefaultFFMPEGLocator, as suggested in some posts, but it did not work for me. I have also tried to set the path to the FFMPEG executable directly in the Encoder object like this:

    public class CustomFFMPEGLocator extends DefaultFFMPEGLocator {

    private String ffmpegExecutablePath;

    public CustomFFMPEGLocator(String ffmpegExecutablePath) {
        this.ffmpegExecutablePath = ffmpegExecutablePath;
    }

    @Override
    public String getExecutablePath() {
        return ffmpegExecutablePath;
    }
}



        CustomFFMPEGLocator locator = new CustomFFMPEGLocator("./src/main/resources/ffmpeg.exe");
    Encoder encoder = new Encoder(locator);
    MultimediaObject mediaInput = new MultimediaObject(tempWavFile);
    encoder.encode(mediaInput, tempFile, encodingAttrs);

But this did not work either.

The only solution that worked for me was to manually copy the FFMPEG executable to the temporary folder of the system. However, this is not a viable solution for a production environment where I don't have access to the temporary folder.

Does anyone know how to set the FFMPEG executable path for the ws.schild.jave library in a way that works in a production environment? Thank you in advance.

This is the stacktrace:

ws.schild.jave.EncoderException: java.io.IOException: Cannot run program "C:\Users\L7E6D~1.FON\AppData\Local\Temp\jave\ffmpeg-amd64-3.0.1.exe": CreateProcess error=2, Impossibile trovare il file specificato
at ws.schild.jave.Encoder.encode(Encoder.java:508)
at ws.schild.jave.Encoder.encode(Encoder.java:351)
at ws.schild.jave.Encoder.encode(Encoder.java:318)
at com.intesasanpaolo.bear.ptr.core.service.impl.DownloadEcommsServiceImpl.convertWavToMp3(DownloadEcommsServiceImpl.java:358)
at com.intesasanpaolo.bear.ptr.core.controller.impl.DownloadControllerImpl.downloadAudio(DownloadControllerImpl.java:52)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:197)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:141)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:894)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1060)
at

I'm using Java 8 and JAVE 3.0.1

Luigi105
  • 141
  • 1
  • 11

0 Answers0