How can I convert a mp4 file to webm using java only?

1k Views Asked by At

I am trying to convert a file from mp4 to webm. I am trying to use the JAVE wrapper of FFmpeg. I am getting the error. Here is my code:

private String ConvertVideo(URL url) {
    File target =null;
    MultimediaObject multimediaObject = new MultimediaObject(url);
    try {
        target = File.createTempFile("target", ".webm");

    } catch (IOException e1) {
        e1.printStackTrace();
    }
    AudioAttributes audio = new AudioAttributes();
    audio.setCodec(AudioAttributes.DIRECT_STREAM_COPY);
    audio.setBitRate(new Integer(128000));
    audio.setSamplingRate(new Integer(44100));
    audio.setChannels(new Integer(2));
    VideoAttributes video = new VideoAttributes();
    video.setBitRate(new Integer(160000));
    video.setFrameRate(new Integer(15));
    video.setCodec("libvpx-vp9");
    video.setCodec(VideoAttributes.DIRECT_STREAM_COPY);
    EncodingAttributes attrs = new EncodingAttributes();
    attrs.setFormat("webm");
    attrs.setAudioAttributes(audio);
    attrs.setVideoAttributes(video);
    
    try {
          Encoder encoder = new Encoder();  
          encoder.encode(multimediaObject, target, attrs);
        } catch (Exception e) {  
           e.printStackTrace();
        }
    
    return "success";
}

I am getting the below error:

2022-Jun-13 11:12:55 AM [qtp1914526580-175] ERROR ws.schild.jave.Encoder - Process exit code: 1  to target2636257785060285182.webm
ws.schild.jave.EncoderException: Exit code of ffmpeg encoding run is 1

What am I doing wrong here. Is there any other way around it? I want to do it only using java.

1

There are 1 best solutions below

4
mir-shakir On

I have sorted it out. Here is my final working code. Just removed the line video.setCodec(VideoAttributes.DIRECT_STREAM_COPY); and also changed the line audio.setCodec(AudioAttributes.DIRECT_STREAM_COPY); to audio.setCodec("libvorbis");

private String convertVideo(URL url) {
        File target =null;
        MultimediaObject multimediaObject = new MultimediaObject(url);

        try {
            target = File.createTempFile("target", ".webm");
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        AudioAttributes audio = new AudioAttributes();
        audio.setCodec("libvorbis");
        audio.setBitRate(new Integer(128000));
        audio.setSamplingRate(new Integer(44100));
        audio.setChannels(new Integer(2));
        VideoAttributes video = new VideoAttributes();
        video.setBitRate(new Integer(160000));
        video.setFrameRate(new Integer(20));
        video.setCodec("libvpx-vp9");
        EncodingAttributes attrs = new EncodingAttributes();
        attrs.setFormat("webm");
        attrs.setAudioAttributes(audio);
        attrs.setVideoAttributes(video);
        
        try {
              Encoder encoder = new Encoder();  
              encoder.encode(multimediaObject, target, attrs);
            } catch (Exception e) {   
               e.printStackTrace();
            }       
        return "success";
    }