domenica 25 agosto 2013

Converting video with JAVE

JAVE is a java library developed by sauronsoftware, it's a wrapper of FFmpeg and it's an easy way to converting video from a format to another. The main class of jave is it.sauronsoftware.jave.Encoder, Encoder objects have methods that transcode multimedia files.
First thing to do is adding JAVE to your CLASSPATH, if you are using eclipse simply import jave-1.0.jar in your project. Now in your code create an object of Encoder:
Encoder encoder = new Encoder();
Now you just call encode() to do the work, let's see it's signature:
public void encode(java.io.File source,
                   java.io.File target,
                   it.sauronsoftware.jave.EncodingAttributes attributes)
            throws java.lang.IllegalArgumentException,
                   it.sauronsoftware.jave.InputFormatException,
                   it.sauronsoftware.jave.EncoderException
the first argument (source) is the file you want to transcode, the second one (target) is the file you want to create on the machine. The argument attributes of type it.sauronsoftware.jave.EncodingAttributes is a structure that has all the propriety we want to give to our video. Attention: the method is syn, it returns after the video transcoding. Let's see how it's possible to convert a video by setting VideoAttributes and AudioAttributes:
 public static void convertVideo(String videoInput, String videoName) throws IllegalArgumentException, InputFormatException, EncoderException
 {
  String videoOutput = videoName + ".flv";
  Encoder encoder = new Encoder();
  
  File source = new File(videoInput);
  File target = new File(videoOutput);
  AudioAttributes audio = new AudioAttributes();
  audio.setCodec("libmp3lame");
  audio.setBitRate(new Integer(Config.AUDIO_BITRATE * 1000));
  audio.setChannels(new Integer(Config.AUDIO_CHANNELS));
  audio.setSamplingRate(new Integer(Config.AUDIO_SAMPLINGRATE));
  VideoAttributes video = new VideoAttributes();
  video.setCodec("flv");
  video.setBitRate(new Integer(Config.VIDEO_BITRATE*10000));
  video.setFrameRate(new Integer(Config.VIDEO_FRAMERATE));
  video.setSize(new VideoSize(Config.VIDEO_WIDTH, Config.VIDEO_HEIGHT));
  EncodingAttributes attrs = new EncodingAttributes();
  attrs.setFormat("flv");
  
  attrs.setAudioAttributes(audio);
  attrs.setVideoAttributes(video);
  
  encoder.encode(source, target, attrs);
 }
The video is converted in .flv format with given framerate, width, height and bitrate. The audio is transcoded in libmp3lame with given samplingrate. The values I've used to make a not so heavy .flv file are the following:
public class Config {
 public static int VIDEO_WIDTH = 640;
 public static int VIDEO_HEIGHT = 360;
 public static int VIDEO_FRAMERATE = 15;
 public static int VIDEO_BITRATE = 128;
 
 public static int AUDIO_SAMPLINGRATE = 22050;
 public static int AUDIO_CHANNELS = 1;
 public static int AUDIO_BITRATE = 64;
}
That's all, for media manipulation I suggest you to see FFmpeg and my previous post to give you the idea of how to do it in java. --------------------------------------------------------------------
ITA
La documentazione di JAVE è disponibile in italiano sul sito, il mio esempio si occupa di codificare un video da un formato input compatibile a .flv con le configurazioni date nella classe Config.

Nessun commento:

Posta un commento