Tamil veera Cholan
Tamil veera Cholan

Reputation: 512

How to convert ogv video file into Mp4 video format using Java

I need to convert OGV VIDEO Format into MP4 VIDEO Format.

public class VideoConvert {
    public static void main(String[] args) throws IOException {
    File source = new File("D:\\readxml\\videoConvertorProject\\MP4toOGV\\mp4\\Sample1.ogv");
    File target = new File("D:\\readxml\\videoConvertorProject\\MP4toOGV\\ogv\\Sample2.mp4");
    AudioAttributes audio = new AudioAttributes();
    //audio.setCodec("libvorbis");
    audio.setCodec("libfaac");
    //audio.setCodec("libmp3lame");
    //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.setSize(new VideoSize(176, 144));
    //video.setCodec("libtheora");
    video.setCodec("mpeg4");
    //video.setCodec(VideoAttributes.DIRECT_STREAM_COPY);
    EncodingAttributes attrs = new EncodingAttributes();
    attrs.setFormat("mp4");
    attrs.setAudioAttributes(audio);
    attrs.setVideoAttributes(video);
    Encoder encoder = new Encoder();
    try {
        encoder.encode(source, target, attrs);
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (InputFormatException e) {
        e.printStackTrace();
    } catch (EncoderException e) {
        e.printStackTrace();
    }
}

Upvotes: 4

Views: 8039

Answers (2)

user5883062
user5883062

Reputation:

I think, you the JAVE (Java Audio Video Encoder) library, so I changed the setCodec AudioAttributes.DIRECT_STREAM_COPY

public class VideoConvert {
public static void main(String[] args) throws IOException {
File source = new File("D:\\video\\mp4\\Sample.ogv");
File target = new File("D:\\video\\ogv\\Sample.mp4");
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("mpeg4");
video.setCodec(VideoAttributes.DIRECT_STREAM_COPY);
EncodingAttributes attrs = new EncodingAttributes();
attrs.setFormat("mp4");
attrs.setAudioAttributes(audio);
attrs.setVideoAttributes(video);
Encoder encoder = new Encoder();
try {
    encoder.encode(source, target, attrs);
} catch (IllegalArgumentException e) {
    e.printStackTrace();
} catch (InputFormatException e) {
    e.printStackTrace();
} catch (EncoderException e) {
    e.printStackTrace();
}

}

Upvotes: 5

MithrilTuxedo
MithrilTuxedo

Reputation: 548

I'm not entirely sure what library you're using, but the MPEG-4 container format supports a number of different video and audio codecs.

I see libfaac for audio, which lets me assume you're using something ffmpeg-based.

For video you probably want H.264/MPEG-4 AVC, in which case you'll use libx264 as the video encoder. I've never seen a mpeg4 video coding format otherwise.

Upvotes: 1

Related Questions