Skip to content
André Schild edited this page Jul 24, 2018 · 21 revisions

Usage

You can use maven dependencies to include the libraries in your projects. Include the following in your pom files.

For all platforms

`

<groupId>ws.schild</groupId>

<artifactId>jave-all-deps</artifactId>

<version>2.4.0</version>

`

For one platform only (Linux 64Bit in this case)

`

<groupId>ws.schild</groupId>

<artifactId>jave-core</artifactId>

<version>2.4.0</version>
<groupId>ws.schild</groupId>

<artifactId>jave-native-linux64</artifactId>

<version>2.4.0</version>

All available platforms:

The most important JAVE class is {{{ws.schild.jave.Encoder}}}. Encoder objects expose many methods for multimedia transcoding. In order to use JAVE, you always have to create an Encoder istance:

Encoder encoder = new Encoder();

Once the instance has been created, you can start transcoding calling the encode() method:

public void encode(java.io.File source,
               java.io.File target,
               ws.schild.jave.EncodingAttributes attributes)
        throws java.lang.IllegalArgumentException,
               ws.schild.jave.InputFormatException,
               ws.schild.jave.EncoderException

The first parameter, source, represents the source file to decode.

The second parameter, target, is the target file that will be created and encoded.

The attributes parameter, whose type is ws.schild.jave.EncodingAttributes, is a data structure containing any information needed by the encoder.

Please note that a call to encode() is a blocking one: the method will return only once the transcoding operation has been completed (or failed). If you are interested in monitoring the transcoding operation take a look to the "Monitoring the transcoding operation" section. Encoding attributes

To specify your preferences about the transcoding operation you have to supply an {{{ws.schild.jave.EncodingAttributes}}} instance to the encode() call. You can create your own EncodingAttributes instance, and you can populate it with the following methods:

public void setAudioAttributes(ws.schild.jave.AudioAttributes audioAttributes)

It sets the audio encoding attributes. If never called on a new EncodingAttributes instance, or if the given parameter is null, no audio stream will be included in the encoded file. See also "Audio encoding attributes".

public void setVideoAttributes(ws.schild.jave.AudioAttributes videoAttributes)

It sets the video encoding attributes. If never called on a new EncodingAttributes instance, or if the given parameter is null, no video stream will be included in the encoded file. See also "Video encoding attributes".

public void setFormat(java.lang.String format)

It sets the format of the streams container that will be used for the new encoded file. The given parameter represents the format name. An encoding format name is valid and supported only if it appears in the list returned by the {{{getSupportedEncodingFormats()}}} method of the Encoder instance in use.

public void setOffset(java.lang.Float offset)

It sets an offset for the transcoding operation. The source file will be re-encoded starting at offset seconds since its beginning. In example if you'd like to cut the first five seconds of the source file, you should call setOffset(5) on the EncodingAttributes object passed to the encoder.

public void setDuration(java.lang.Float duration)

It sets a duration for the transcoding operation. Only duration seconds of the source will be re-encoded in the target file. In example if you'd like to extract and transcode a portion of thirty seconds from the source, you should call setDuration(30) on the EncodingAttributes object passed to the encoder.

Audio encoding attributes

Audio encoding attributes are represented by the instances of the ws.schild.jave.AudioAttributes class. The available methods on this kind of objects are:

public void setCodec(java.lang.String codec)

It sets the name of the codec that will be used for the transcoding of the audio stream. You have to choose a value from the list returned by the getAudioEncoders() method of the current Encoder instance. Otherwise you can pass the AudioAttributes.DIRECT_STREAM_COPY special value, that requires the copy of the original audio stream from the source file.

public void setBitRate(java.lang.Integer bitRate)

It sets the bitrate value for the new re-encoded audio stream. If no bitrate value is set, a default one will be picked by the encoder. The value should be expressed in bits per second. In example if you want a 128 kb/s bitrate you should call setBitRate(new Integer(128000)).

public void setSamplingRate(java.lang.Integer bitRate)

It sets the sampling rate for the new re-encoded audio stream. If no sampling-rate value is set, a default one will be picked by the encoder. The value should be expressed in hertz. In example if you want a CD-like 44100 Hz sampling-rate, you should call setSamplingRate(new Integer(44100)).

public void setChannels(java.lang.Integer channels)

It sets the number of the audio channels that will be used in the re-encoded audio stream (1 = mono, 2 = stereo). If no channels value is set, a default one will be picked by the encoder.

public void setVolume(java.lang.Integer volume)

This method can be called to alter the volume of the audio stream. A value of 256 means no volume change. So a value less than 256 is a volume decrease, while a value greater than 256 will increase the volume of the audio stream.

Video encoding attributes

Video encoding attributes are represented by the instances of the it.sauronsoftware.jave.VideoAttributes class. The available methods on this kind of objects are:

public void setCodec(java.lang.String codec)

It sets the name of the codec that will be used for the transcoding of the video stream. You have to choose a value from the list returned by the getVideoEncoders() method of the current Encoder instance. Otherwise you can pass the VideoAttributes.DIRECT_STREAM_COPY special value, that requires the copy of the original video stream from the source file.

public void setTag(java.lang.String tag)

It sets the tag/fourcc value associated to the re-encoded video stream. If no value is set a default one will be picked by the encoder. The tag value is often used by multimedia players to choose which video decoder run on the stream. In example a MPEG 4 video stream with a "DIVX" tag value will be decoded with the default DivX decoder used by the player. And, by the way, this is exactly what a DivX is: a MPEG 4 video stream with an attached "DIVX" tag/fourcc value!

public void setBitRate(java.lang.Integer bitRate)

It sets the bitrate value for the new re-encoded video stream. If no bitrate value is set, a default one will be picked by the encoder. The value should be expressed in bits per second. In example if you want a 360 kb/s bitrate you should call setBitRate(new Integer(360000)).

public void setFrameRate(java.lang.Integer bitRate)

It sets the frame rate value for the new re-encoded audio stream. If no bitrate frame-rate is set, a default one will be picked by the encoder. The value should be expressed in frames per second. In example if you want a 30 f/s frame-rate you should call setFrameRate(new Integer(30)). public void setSize(it.sauronsoftware.jave.VideoSize size)

It sets the size and the proportion of the images in the video stream. If no value is set, the encoder will preserve the original size and proportion. Otherwise you can pass a it.sauronsoftware.java.VideoSize instance, with your preferred size. You can set the width and the height of the new encoded video, with pixel values, scaling the original one. In example if you want to scale the video to 512 px in width and 384px in height you should call setSize(new VideoSize(512, 384)).

Monitoring the transcoding operation

You can monitor a transcoding operation with a listener. JAVE defines the ws.schild.jave.EncoderProgressListener interface. This interface could be implemented by your application, and concrete EncoderProgressListener instances can be passed to the encoder. The encoder will call your listener methods every time a significant event occurs. To pass an EncoderProgressListener to the encoder you should use this definition of the encode() method:

public void encode(java.io.File source,
               java.io.File target,
               it.sauronsoftware.jave.EncodingAttributes attributes,
               it.sauronsoftware.jave.EncoderProgressListener listener)
        throws java.lang.IllegalArgumentException,
               it.sauronsoftware.jave.InputFormatException,
               it.sauronsoftware.jave.EncoderException

To implemen the EncoderProgressListener interface you have to define all of the following methods:

public void sourceInfo(ws.schild.jave.MultimediaInfo info)

The encoder calls this method after the source file has been analized. The info parameter is an instance of the it.sauronsoftware.jave.MultimediaInfo class and it represents informations about the source audio and video streams and their container.

public void progress(int permil)

This method is called by the encoder every time a progress in the encoding operation has been done. The permil parameter is a value representing the point reached by the current operation and its range is from 0 (operation just started) to 1000 (operation completed).

public void message(java.lang.String message)

This method is called by the encoder to notify a message regarding the transcoding operation (usually the message is a warning).

Transcoding failures

Of course, a transcoding operation could fail. Then the encode() method will propagate an exception. Depending on what is happened, the exception will be one of the following:

java.lang.IllegalArgumentException

The transcoding operation has never started since the encoding attributes passed to the encoder has been recognized as invalid. Usualy this occurs when the EncodingAttributes instance given to the encoder asks the encoding of a container with no audio and no video streams (both AudioAttributes and VideoAttribues attributes are null or not set).

ws.schild.jave.InputFormatException

The source file can't be decoded. It occurs when the source file container, the video stream format or the audio stream format are not supported by the decoder. You can check for supported containers and plugged decoders calling the encoder methods getSupportedDecodingFormats(), getAudioDecoders() and getVideoDecoders().

ws.schild.jave.EncoderExpection

The operation has failed during the trancoding due to an internal error. You should check the exception message, and you can also use an EncoderProgressListener instance to check any message issued by the encoder.

Getting informations about a multimedia file

You can get informations about an existing multimedia file before transcoding it, calling the encoder getInfo() method. The getInfo() method gives you informations about the container used by the file and about its wrapped audio and video streams:

public ws.schild.jave.MultimediaInfo getInfo(java.io.File source)
                                         throws ws.schild.jave.InputFormatException,
                                                ws.schild.jave.EncoderException

An it.sauronsoftware.jave.MultimediaInfo object encapsulates information on the whole multimedia content and its streams, using instances of ws.schild.jave.AudioInfo and ws.schild.jave.VideoInfo to describe the wrapped audio and video. These objects are similar to the EncodingAttributes, AudioAttributes and VideoAttributes ones, but they works in a read-only mode. Check the JAVE API javadoc documentation, bundled with the JAVE distribution, to gain more details about them.

Example usage:

System.out.println("encode WEBM");
File source = new File("test/videos/l4.mov");
File target = new File("test/out/l4.faststart.webm");
AudioAttributes audio = new AudioAttributes();
VideoAttributes video = new VideoAttributes();
EncodingAttributes attrs = new EncodingAttributes();
attrs.setAudioAttributes(audio);
attrs.setVideoAttributes(video);
audio.setCodec("libvorbis");
audio.setBitRate(new Integer(64000));
audio.setSamplingRate(new Integer(44100));
audio.setChannels(new Integer(2));
audio.setBitRate(new Integer(192000));
video.setCodec("libvpx");
video.setBitRate(new Integer(250000));
video.setFrameRate(new Integer(25));
attrs.setFormat("webm");
Encoder instance = new Encoder();
instance.encode(source, target, attrs, null);

See the Examples page for more samples

Clone this wiki locally