1

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.

mir-shakir
  • 41
  • 5
  • @mir-shakir Using both `video.setCodec("libvpx-vp9");` and `video.setCodec(VideoAttributes.DIRECT_STREAM_COPY);` one after the other, seems strange to me (but I saw it used in other code samples). Can you please post an executable code sample (with `main`, `import` etc.)? – Rotem Jun 13 '22 at 09:04
  • @Rotem Yes, I got that from a tutorial online. That is actually strange. I removed that line now and also changed the codec for audio as well. See my answer for the final working code. Thanks for the help. – mir-shakir Jun 13 '22 at 09:48

1 Answers1

1

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";
    }
mir-shakir
  • 41
  • 5
  • @kleopatra Sorry but I don't know what conventions you're talking about. I am new to java and this code is part of a larger project built by other developers so I am just following the pattern. – mir-shakir Jun 14 '22 at 12:46
  • what happened when you typed _java naming conventions_ into your favorite search engine? Or looked into the java tag wiki? Anyway, what you do privately (or project internally) obviously is your own/your team's decision - but when showing code publicly (particularly code that's meant to be useful for the broader audience) - sticking to conventions is important for readability – kleopatra Jun 14 '22 at 13:32
  • @kleopatra Thanks for the advice. I just made a change and that is all I could find after looking up for java naming conventions. Kindly point out if there is any other naming mistake in the code. – mir-shakir Jun 14 '22 at 14:58
  • thanks - that's all I've been seeing :) – kleopatra Jun 14 '22 at 16:01