Java Audio Video Encoder

http://www.sauronsoftware.it/projects/jave/


JAVE

The JAVE (Java Audio Video Encoder) library is Java wrapper on the ffmpeg project. Developers can take take advantage of JAVE to transcode audio and video files from a format to another. In example you can transcode an AVI file to a MPEG one, you can change a DivX video stream into a (youtube like) Flash FLV one, you can convert a WAV audio file to a MP3 or a Ogg Vorbis one, you can separate and transcode audio and video tracks, you can resize videos, changing their sizes and proportions and so on. Many other formats, containers and operations are supported by JAVE.

Requirements

JAVE requires a J2SE environment 1.4 or later and a Windows or Linux OS on a i386 / 32 bit hardware architecture. JAVE can also be easily ported to other OS and hardware configurations, see the JAVE manual for details.

License

JAVE is Free Software and it is licensed under GPL (you will find a copy of the license bundled into the downloadable software distribution).

Feedback

You can send comments and requests to Carlo Pelliccia.

Make a donation

JAVE is free, but if you find it useful please make a donation via PayPal.


http://www.sauronsoftware.it/projects/jave/manual.php?PHPSESSID=ee88pohar3vmfmqnjsprouqbn7

JAVE manual

Installation and requirements

In order to use JAVE in your Java application, you have to add the file jave-1.0.jar in your application CLASSPATH.

JAVE runs on a Java Runtime Environment J2SE v.1.4 or later.

JAVE includes and uses a ffmpeg executable built for Windows and Linux operating systems on i386/32 bit hardware platforms. In order to run JAVE on other platforms you have to replace the built-in ffmpeg executable with another one suitable for your needs. This is very simple, once you have built your own ffmpeg binaries. The operation is described in the "Using an alternative ffmpeg executable" section.

Audio/video encoding

The most important JAVE class is it.sauronsoftware.jave.EncoderEncoder 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,
                   it.sauronsoftware.jave.EncodingAttributes attributes)
            throws java.lang.IllegalArgumentException,
                   it.sauronsoftware.jave.InputFormatException,
                   it.sauronsoftware.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 it.sauronsoftware.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 it.sauronsoftware.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(it.sauronsoftware.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(it.sauronsoftware.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 it.sauronsoftware.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 it.sauronsoftware.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(it.sauronsoftware.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 VideoAttribuesattributes are null or not set).
  • it.sauronsoftware.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().
  • it.sauronsoftware.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 EncoderProgressListenerinstance 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 it.sauronsoftware.jave.MultimediaInfo getInfo(java.io.File source)
                                             throws it.sauronsoftware.jave.InputFormatException,
                                                    it.sauronsoftware.jave.EncoderException

An it.sauronsoftware.jave.MultimediaInfo object encapsulates information on the whole multimedia content and its streams, using instances ofit.sauronsoftware.jave.AudioInfo and it.sauronsoftware.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.

Using an alternative ffmpeg executable

JAVE is not pure Java: it acts as a wrapper around an ffmpeg (http://ffmpeg.mplayerhq.hu/) executable. ffmpeg is an open source and free software project entirely written in C, so its executables cannot be easily ported from a machine to another. You need a pre-compiled version of ffmpeg in order to run JAVE on your target machine. The JAVE distribution includes two pre-compiled executables of ffmpeg: a Windows one and a Linux one, both compiled for i386/32 bit hardware achitectures. This should be enough in most cases. If it is not enough for your specific situation, you can still run JAVE, but you need to obtain a platform specific ffmpeg executable. Check the Internet for it. You can even build it by yourself getting the code (and the documentation to build it) on the official ffmpeg site. Once you have obtained a ffmpeg executable suitable for your needs, you have to hook it in the JAVE library. That's a plain operation. JAVE gives you an abstract class called it.sauronsoftware.jave.FFMPEGLocator. Extend it. All you have to do is to define the following method:

public java.lang.String getFFMPEGExecutablePath()

This method should return a file system based path to your custom ffmpeg executable.

Once your class is ready, suppose you have called it MyFFMPEGExecutableLocator, you have to create an alternate encoder that uses it instead of the default locator:

Encoder encoder = new Encoder(new MyFFMPEGExecutableLocator())

You can use the same procedure also to switch to other versions of ffmpeg, even if you are on a platform covered by the executables bundled in the JAVE distribution.

Anyway be careful and test ever your application: JAVE it's not guaranteed to work properly with custom ffmpeg executables different from the bundled ones.

Supported container formats

The JAVE built-in ffmpeg executable gives support for the following multimedia container formats:

Decoding

FormatoDescrizione
4xm4X Technologies format
MTVMTV format
RoQId RoQ format
aacADTS AAC
ac3raw ac3
aiffAudio IFF
alawpcm A law format
amr3gpp amr file format
apcCRYO APC format
apeMonkey's Audio
asfasf format
auSUN AU Format
aviavi format
avsAVISynth
bethsoftvidBethesda Softworks 'Daggerfall' VID format
c93Interplay C93
daudD-Cinema audio format
dsicinDelphine Software International CIN format
dtsraw dts
dvDV video format
dxadxa
eaElectronic Arts Multimedia Format
ea_cdataElectronic Arts cdata
ffmffm format
film_cpkSega FILM/CPK format
flacraw flac
flicFLI/FLC/FLX animation format
flvflv format
gifGIF Animation
gxfGXF format
h261raw h261
h263raw h263
h264raw H264 video format
idcinId CIN format
image2image2 sequence
image2pipepiped image2 sequence
ingenientIngenient MJPEG
ipmovieInterplay MVE format
libnutnut format
m4vraw MPEG4 video format
matroskaMatroska File Format
mjpegMJPEG video
mmAmerican Laser Games MM format
mmfmmf format
mov,mp4,m4a,3gp,3g2,mj2QuickTime/MPEG4/Motion JPEG 2000 format
mp3MPEG audio layer 3
mpcmusepack
mpc8musepack8
mpegMPEG1 System format
mpegtsMPEG2 transport stream format
mpegtsrawMPEG2 raw transport stream format
mpegvideoMPEG video
mulawpcm mu law format
mxfMXF format
nsvNullSoft Video format
nutnut format
nuvNuppelVideo format
oggOgg format
psxstrSony Playstation STR format
rawvideoraw video format
redirRedirector format
rmrm format
rtspRTSP input format
s16bepcm signed 16 bit big endian format
s16lepcm signed 16 bit little endian format
s8pcm signed 8 bit format
sdpSDP
shnraw shorten
siffBeam Software SIFF
smkSmacker Video
solSierra SOL Format
swfFlash format
thpTHP
tiertexseqTiertex Limited SEQ format
ttatrue-audio
txdtxd format
u16bepcm unsigned 16 bit big endian format
u16lepcm unsigned 16 bit little endian format
u8pcm unsigned 8 bit format
vc1raw vc1
vmdSierra VMD format
vocCreative Voice File format
wavwav format
wc3movieWing Commander III movie format
wsaudWestwood Studios audio format
wsvqaWestwood Studios VQA format
wvWavPack
yuv4mpegpipeYUV4MPEG pipe format

Encoding

FormatoDescrizione
3g23gp2 format
3gp3gp format
RoQId RoQ format
ac3raw ac3
adtsADTS AAC
aiffAudio IFF
alawpcm A law format
amr3gpp amr file format
asfasf format
asf_streamasf format
auSUN AU Format
aviavi format
crccrc testing format
dvDV video format
dvdMPEG2 PS format (DVD VOB)
ffmffm format
flacraw flac
flvflv format
framecrcframecrc testing format
gifGIF Animation
gxfGXF format
h261raw h261
h263raw h263
h264raw H264 video format
image2image2 sequence
image2pipepiped image2 sequence
libnutnut format
m4vraw MPEG4 video format
matroskaMatroska File Format
mjpegMJPEG video
mmfmmf format
movmov format
mp2MPEG audio layer 2
mp3MPEG audio layer 3
mp4mp4 format
mpegMPEG1 System format
mpeg1videoMPEG video
mpeg2videoMPEG2 video
mpegtsMPEG2 transport stream format
mpjpegMime multipart JPEG format
mulawpcm mu law format
nullnull video format
nutnut format
oggOgg format
psppsp mp4 format
rawvideoraw video format
rmrm format
rtpRTP output format
s16bepcm signed 16 bit big endian format
s16lepcm signed 16 bit little endian format
s8pcm signed 8 bit format
svcdMPEG2 PS format (VOB)
swfFlash format
u16bepcm unsigned 16 bit big endian format
u16lepcm unsigned 16 bit little endian format
u8pcm unsigned 8 bit format
vcdMPEG1 System format (VCD)
vobMPEG2 PS format (VOB)
vocCreative Voice File format
wavwav format
yuv4mpegpipeYUV4MPEG pipe format

Built-in decoders and encoders

The JAVE built-in ffmpeg executable contains the following decoders and encoders:

Audio decoders

adpcm_4xmadpcm_adxadpcm_ctadpcm_eaadpcm_ea_r1
adpcm_ea_r2adpcm_ea_r3adpcm_ea_xasadpcm_ima_amvadpcm_ima_dk3
adpcm_ima_dk4adpcm_ima_ea_eacsadpcm_ima_ea_seadadpcm_ima_qtadpcm_ima_smjpeg
adpcm_ima_wavadpcm_ima_wsadpcm_msadpcm_sbpro_2adpcm_sbpro_3
adpcm_sbpro_4adpcm_swfadpcm_thpadpcm_xaadpcm_yamaha
alacapeatrac 3cookdca
dsicinaudioflacg726imcinterplay_dpcm
liba52libamr_nblibamr_wblibfaadlibgsm
libgsm_msmace3mace6mp2mp3
mp3adump3on4mpc sv7mpc sv8mpeg4aac
nellymoserpcm_alawpcm_mulawpcm_s16bepcm_s16le
pcm_s16le_planarpcm_s24bepcm_s24daudpcm_s24lepcm_s32be
pcm_s32lepcm_s8pcm_u16bepcm_u16lepcm_u24be
pcm_u24lepcm_u32bepcm_u32lepcm_u8pcm_zork
qdm2real_144real_288roq_dpcmshorten
smackaudsol_dpcmsonictruespeechtta
vmdaudiovorbiswavpackwmav1wmav2
ws_snd1xan_dpcm   

Audio encoders

ac3adpcm_adxadpcm_ima_wavadpcm_msadpcm_swf
adpcm_yamahaflacg726libamr_nblibamr_wb
libfaaclibgsmlibgsm_mslibmp3lamelibvorbis
mp2pcm_alawpcm_mulawpcm_s16bepcm_s16le
pcm_s24bepcm_s24daudpcm_s24lepcm_s32bepcm_s32le
pcm_s8pcm_u16bepcm_u16lepcm_u24bepcm_u24le
pcm_u32bepcm_u32lepcm_u8pcm_zorkroq_dpcm
sonicsoniclsvorbiswmav1wmav2

Video decoders

4xm8bpsVMware videoaascamv
asv1asv2avsbethsoftvidbmp
c93camstudiocamtasiacavscinepak
cljrcyuvdnxhddsicinvideodvvideo
dxaffv1ffvhuffflashsvflic
flvfrapsgifh261h263
h263ih264huffyuvidcinvideoindeo2
indeo3interplayvideojpeglskmvcloco
mdecmjpegmjpegbmmvideompeg1video
mpeg2videompeg4mpegvideomsmpeg4msmpeg4v1
msmpeg4v2msrlemsvideo1mszhnuv
pampbmpgmpgmyuvpng
ppmptxqdrawqpegqtrle
rawvideoroqvideorpzarv10rv20
sgismackvidsmcsnowsp5x
svq1svq3targatheorathp
tiertexseqvideotifftruemotion1truemotion2txd
ultimotionvbvc1vcr1vmdvideo
vp3vp5vp6vp6avp6f
vqavideowmv1wmv2wmv3wnv1
xan_wc3xlzlibzmbv 

Video encoders

asv1asv2bmpdnxhddvvideo
ffv1ffvhuffflashsvflvgif
h261h263h263phuffyuvjpegls
libtheoralibx264libxvidljpegmjpeg
mpeg1videompeg2videompeg4msmpeg4msmpeg4v1
msmpeg4v2pampbmpgmpgmyuv
pngppmqtrlerawvideoroqvideo
rv10rv20sgisnowsvq1
targatiffwmv1wmv2zlib
zmbv    

Examples

From a generic AVI to a youtube-like FLV movie, with an embedded MP3 audio stream:

File source = new File("source.avi");
File target = new File("target.flv");
AudioAttributes audio = new AudioAttributes();
audio.setCodec("libmp3lame");
audio.setBitRate(new Integer(64000));
audio.setChannels(new Integer(1));
audio.setSamplingRate(new Integer(22050));
VideoAttributes video = new VideoAttributes();
video.setCodec("flv");
video.setBitRate(new Integer(160000));
video.setFrameRate(new Integer(15));
video.setSize(new VideoSize(400, 300));
EncodingAttributes attrs = new EncodingAttributes();
attrs.setFormat("flv");
attrs.setAudioAttributes(audio);
attrs.setVideoAttributes(video);
Encoder encoder = new Encoder();
encoder.encode(source, target, attrs);

Next lines extracts audio informations from an AVI and store them in a plain WAV file:

File source = new File("source.avi");
File target = new File("target.wav");
AudioAttributes audio = new AudioAttributes();
audio.setCodec("pcm_s16le");
EncodingAttributes attrs = new EncodingAttributes();
attrs.setFormat("wav");
attrs.setAudioAttributes(audio);
Encoder encoder = new Encoder();
encoder.encode(source, target, attrs);

Next example takes an audio WAV file and generates a 128 kbit/s, stereo, 44100 Hz MP3 file:

File source = new File("source.wav");
File target = new File("target.mp3");
AudioAttributes audio = new AudioAttributes();
audio.setCodec("libmp3lame");
audio.setBitRate(new Integer(128000));
audio.setChannels(new Integer(2));
audio.setSamplingRate(new Integer(44100));
EncodingAttributes attrs = new EncodingAttributes();
attrs.setFormat("mp3");
attrs.setAudioAttributes(audio);
Encoder encoder = new Encoder();
encoder.encode(source, target, attrs);

Next one decodes a generic AVI file and creates another one with the same video stream of the source and a re-encoded low quality MP3 audio stream:

File source = new File("source.avi");
File target = new File("target.avi");
AudioAttributes audio = new AudioAttributes();
audio.setCodec("libmp3lame");
audio.setBitRate(new Integer(56000));
audio.setChannels(new Integer(1));
audio.setSamplingRate(new Integer(22050));
VideoAttributes video = new VideoAttributes();
video.setCodec(VideoAttributes.DIRECT_STREAM_COPY);
EncodingAttributes attrs = new EncodingAttributes();
attrs.setFormat("avi");
attrs.setAudioAttributes(audio);
attrs.setVideoAttributes(video);
Encoder encoder = new Encoder();
encoder.encode(source, target, attrs);

Next one generates an AVI with MPEG 4/DivX video and OGG Vorbis audio:

File source = new File("source.avi");
File target = new File("target.avi");
AudioAttributes audio = new AudioAttributes();
audio.setCodec("libvorbis");
VideoAttributes video = new VideoAttributes();
video.setCodec("mpeg4");
video.setTag("DIVX");
video.setBitRate(new Integer(160000));
video.setFrameRate(new Integer(30));
EncodingAttributes attrs = new EncodingAttributes();
attrs.setFormat("mpegvideo");
attrs.setAudioAttributes(audio);
attrs.setVideoAttributes(video);
Encoder encoder = new Encoder();
encoder.encode(source, target, attrs);

A smartphone suitable video:

File source = new File("source.avi");
File target = new File("target.3gp");
AudioAttributes audio = new AudioAttributes();
audio.setCodec("libfaac");
audio.setBitRate(new Integer(128000));
audio.setSamplingRate(new Integer(44100));
audio.setChannels(new Integer(2));
VideoAttributes video = new VideoAttributes();
video.setCodec("mpeg4");
video.setBitRate(new Integer(160000));
video.setFrameRate(new Integer(15));
video.setSize(new VideoSize(176, 144));
EncodingAttributes attrs = new EncodingAttributes();
attrs.setFormat("3gp");
attrs.setAudioAttributes(audio);
attrs.setVideoAttributes(video);
Encoder encoder = new Encoder();
encoder.encode(source, target, attrs);

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值