Skip to content

Instantly share code, notes, and snippets.

@balvinder294
Last active July 25, 2022 10:14
Show Gist options
  • Save balvinder294/e6bfd52d137143c4d9ebd7dbb92aa515 to your computer and use it in GitHub Desktop.
Save balvinder294/e6bfd52d137143c4d9ebd7dbb92aa515 to your computer and use it in GitHub Desktop.
Code for video conversion with JAVA
Convert a video to Mp4(MPEG-4 AVC) and AAC audio
/* Step 1. Declaring source file and Target file */
File source = new File("source.avi");
File target = new File("target.mp4");
/* Step 2. Set Audio Attrributes for conversion*/
AudioAttributes audio = new AudioAttributes();
audio.setCodec("aac");
// here 64kbit/s is 64000
audio.setBitRate(64000);
audio.setChannels(2);
audio.setSamplingRate(44100);
/* Step 3. Set Video Attributes for conversion*/
VideoAttributes video = new VideoAttributes();
video.setCodec("h264");
video.setX264Profile(X264_PROFILE.BASELINE);
// Here 160 kbps video is 160000
video.setBitRate(160000);
// More the frames more quality and size, but keep it low based on devices like mobile
video.setFrameRate(15);
video.setSize(new VideoSize(400, 300));
/* Step 4. Set Encoding Attributes*/
EncodingAttributes attrs = new EncodingAttributes();
attrs.setFormat("mp4");
attrs.setAudioAttributes(audio);
attrs.setVideoAttributes(video);
/* Step 5. Do the Encoding*/
try {
Encoder encoder = new Encoder();
encoder.encode(source, target, attrs);
} catch (Exception e) {
/*Handle here the video failure*/
e.printStackTrace();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment