JAVE 视音频转码

官方参考文档: http://www.sauronsoftware.it/projects/jave/manual.php


一、什么是JAVE

    JAVE(Java Audio Video Encoder),是一个包涵ffmpeg项目库。开发这可以运用它去实现音频(Audio)与视频(Video)文件的转码。例如你要把AVI格式文件转为MPEG文件、WAV格式文件转为MP3格式文件,同时你还能调整文件大小与比例。JAVE兼容和支持很多格式之间的转码……


二、典型案例分析

    近期在做微信开发时,需要获取用户发给公众服务号的语音留言。而从微信服务端下载来的语音格式却是amr的格式,同样的你手机录音、Android语音等也都是生成amr格式文件。但当你想在web页面去播放此文件时,就困难了。因为无论是当前HTML5的


三、所需环境与配置

    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。 嗯,你应该看得懂~:D

    

    噢~差点忘了,你在使用时当然还必须引入它的jar包,请猛戳这里点击下载:jave-1.0.2.zip


四、具体用法与文档说明:

    1.JAVE中有个最重要的类Encoder,它暴露了很多的方法,总之你在使用JAVE时,你总是要创建Encoder的实例。

    Encoder encoder = new Encoder();

    让后转码时调用 encode()方法:

[java]  view plain  copy
 
  1. public void encode(java.io.File source,  
  2.                    java.io.File target,  
  3.                    it.sauronsoftware.jave.EncodingAttributes attributes)  
  4.             throws java.lang.IllegalArgumentException,  
  5.                    it.sauronsoftware.jave.InputFormatException,  
  6.                    it.sauronsoftware.jave.EncoderException  
    第一个参数source:需要转码的源文件

    第二个参数target:需转型成的目标文件

    第三个参数attributes:是一个包含编码所需数据的参数


    2.Encoding attributes

    如上所述的encoder()方法,第三个参数是很重要的,所以,你得实例化出一个EncodingAttributes即EncodingAttributes attrs = new EncodingAttributes();

    接下来看看attrs都包含了些什么方法:

[java]  view plain  copy
 
  1. public void setAudioAttributes(it.sauronsoftware.jave.AudioAttributes audioAttributes)  
从方法名可以看出是在转码音频时需要用到的方法,可以说是添加音频转码时所需音频属性。

[java]  view plain  copy
 
  1. public void setVideoAttributes(it.sauronsoftware.jave.AudioAttributes videoAttributes)  
从方法名可以看出是在转码视频时需要用到的方法,可以说是添加视频转码时所需视频属性。
[java]  view plain  copy
 
  1. public void setFormat(java.lang.String format)  
这个则是设置转码格式的方法。

[java]  view plain  copy
 
  1. public void setOffset(java.lang.Float offset)  
设置转码偏移位置的方法,例如你想在5秒后开始转码源文件则setOffset(5)。

[java]  view plain  copy
 
  1. public void setDuration(java.lang.Float duration)  
设置转码持续时间的方法,例如你想持续30秒的转码则setDuration(30)。


 3.Audio encoding attributes

    同样的我们也需设置Audio的属***:AudioAttributes audio = new AudioAttributes();

    看看它的方法:

[java]  view plain  copy
 
  1. public void setCodec(java.lang.String codec)//设置编码器  
  2.   
  3. public void setBitRate(java.lang.Integer bitRate)//设置比特率  
  4.   
  5. public void setSamplingRate(java.lang.Integer bitRate)//设置节录率  
  6.   
  7. public void setChannels(java.lang.Integer channels)//设置声音频道  
  8.   
  9. public void setVolume(java.lang.Integer volume)//设置音量  

4.Video encoding attributes

[java]  view plain  copy
 
  1. public void setCodec(java.lang.String codec)//设置编码器  
  2.       
  3. public void setTag(java.lang.String tag)//设置标签(通常用多媒体播放器所选择的视频解码)  
  4.       
  5. public void setBitRate(java.lang.Integer bitRate)//设置比特率  
  6.       
  7. public void setFrameRate(java.lang.Integer bitRate)//设置帧率  
  8.       
  9. public void setSize(it.sauronsoftware.jave.VideoSize size)//设置大小  

5.Monitoring the transcoding operation

    你可以用listener监测转码操作。JAVE定义了一个EncoderProgressListener的接口。

[java]  view plain  copy
 
  1. public void encode(java.io.File source,  
  2.                    java.io.File target,  
  3.                    it.sauronsoftware.jave.EncodingAttributes attributes,  
  4.                    it.sauronsoftware.jave.EncoderProgressListener listener)  
  5.             throws java.lang.IllegalArgumentException,  
  6.                    it.sauronsoftware.jave.InputFormatException,  
  7.                    it.sauronsoftware.jave.EncoderException  
实现 EncoderProgressListener 接口,需定义的方法:

[java]  view plain  copy
 
  1. public void sourceInfo(it.sauronsoftware.jave.MultimediaInfo info)//源文件信息  
  2.       
  3. public void progress(int permil)//增长千分率  
  4.   
  5. public void message(java.lang.String message)//转码信息提示  

6.Getting informations about a multimedia file

    获取多媒体文件转码时的信息:

[java]  view plain  copy
 
  1. public it.sauronsoftware.jave.MultimediaInfo getInfo(java.io.File source)  
  2.                                              throws it.sauronsoftware.jave.InputFormatException,  
  3.                                                     it.sauronsoftware.jave.EncoderException  

五、例子:

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

[java]  view plain  copy
 
  1. File source = new File("source.avi");  
  2. File target = new File("target.flv");  
  3. AudioAttributes audio = new AudioAttributes();  
  4. audio.setCodec("libmp3lame");  
  5. audio.setBitRate(new Integer(64000));  
  6. audio.setChannels(new Integer(1));  
  7. audio.setSamplingRate(new Integer(22050));  
  8. VideoAttributes video = new VideoAttributes();  
  9. video.setCodec("flv");  
  10. video.setBitRate(new Integer(160000));  
  11. video.setFrameRate(new Integer(15));  
  12. video.setSize(new VideoSize(400300));  
  13. EncodingAttributes attrs = new EncodingAttributes();  
  14. attrs.setFormat("flv");  
  15. attrs.setAudioAttributes(audio);  
  16. attrs.setVideoAttributes(video);  
  17. Encoder encoder = new Encoder();  
  18. encoder.encode(source, target, attrs);  

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

[java]  view plain  copy
 
  1. File source = new File("source.avi");  
  2. File target = new File("target.wav");  
  3. AudioAttributes audio = new AudioAttributes();  
  4. audio.setCodec("pcm_s16le");  
  5. EncodingAttributes attrs = new EncodingAttributes();  
  6. attrs.setFormat("wav");  
  7. attrs.setAudioAttributes(audio);  
  8. Encoder encoder = new Encoder();  
  9. encoder.encode(source, target, attrs);  

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

[java]  view plain  copy
 
  1. File source = new File("source.wav");  
  2. File target = new File("target.mp3");  
  3. AudioAttributes audio = new AudioAttributes();  
  4. audio.setCodec("libmp3lame");  
  5. audio.setBitRate(new Integer(128000));  
  6. audio.setChannels(new Integer(2));  
  7. audio.setSamplingRate(new Integer(44100));  
  8. EncodingAttributes attrs = new EncodingAttributes();  
  9. attrs.setFormat("mp3");  
  10. attrs.setAudioAttributes(audio);  
  11. Encoder encoder = new Encoder();  
  12. 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:

[java]  view plain  copy
 
  1. File source = new File("source.avi");  
  2. File target = new File("target.avi");  
  3. AudioAttributes audio = new AudioAttributes();  
  4. audio.setCodec("libmp3lame");  
  5. audio.setBitRate(new Integer(56000));  
  6. audio.setChannels(new Integer(1));  
  7. audio.setSamplingRate(new Integer(22050));  
  8. VideoAttributes video = new VideoAttributes();  
  9. video.setCodec(VideoAttributes.DIRECT_STREAM_COPY);  
  10. EncodingAttributes attrs = new EncodingAttributes();  
  11. attrs.setFormat("avi");  
  12. attrs.setAudioAttributes(audio);  
  13. attrs.setVideoAttributes(video);  
  14. Encoder encoder = new Encoder();  
  15. encoder.encode(source, target, attrs);  

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

[java]  view plain  copy
 
  1. File source = new File("source.avi");  
  2. File target = new File("target.avi");  
  3. AudioAttributes audio = new AudioAttributes();  
  4. audio.setCodec("libvorbis");  
  5. VideoAttributes video = new VideoAttributes();  
  6. video.setCodec("mpeg4");  
  7. video.setTag("DIVX");  
  8. video.setBitRate(new Integer(160000));  
  9. video.setFrameRate(new Integer(30));  
  10. EncodingAttributes attrs = new EncodingAttributes();  
  11. attrs.setFormat("mpegvideo");  
  12. attrs.setAudioAttributes(audio);  
  13. attrs.setVideoAttributes(video);  
  14. Encoder encoder = new Encoder();  
  15. encoder.encode(source, target, attrs);  

A smartphone suitable video:

[java]  view plain  copy
 
  1. File source = new File("source.avi");  
  2. File target = new File("target.3gp");  
  3. AudioAttributes audio = new AudioAttributes();  
  4. audio.setCodec("libfaac");  
  5. audio.setBitRate(new Integer(128000));  
  6. audio.setSamplingRate(new Integer(44100));  
  7. audio.setChannels(new Integer(2));  
  8. VideoAttributes video = new VideoAttributes();  
  9. video.setCodec("mpeg4");  
  10. video.setBitRate(new Integer(160000));  
  11. video.setFrameRate(new Integer(15));  
  12. video.setSize(new VideoSize(176144));  
  13. EncodingAttributes attrs = new EncodingAttributes();  
  14. attrs.setFormat("3gp");  
  15. attrs.setAudioAttributes(audio);  
  16. attrs.setVideoAttributes(video);  
  17. Encoder encoder = new Encoder();  
  18. encoder.encode(source, target, attrs);  

总结下,以上例子看上去都大同小异,步骤就那几步固定死了。

首先,源文件与目标文件。

其次,设置视音频转码钱的属***数据。

      其中setCodec()方法中的参数要对应你所转码的格式的编码encoders。

最后,设置attrs并转码。


六、支持包含在内的格式:

Supported container formats

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

Decoding

Formato Descrizione
4xm 4X Technologies format
MTV MTV format
RoQ Id RoQ format
aac ADTS AAC
ac3 raw ac3
aiff Audio IFF
alaw pcm A law format
amr 3gpp amr file format
apc CRYO APC format
ape Monkey's Audio
asf asf format
au SUN AU Format
avi avi format
avs AVISynth
bethsoftvid Bethesda Softworks 'Daggerfall' VID format
c93 Interplay C93
daud D-Cinema audio format
dsicin Delphine Software International CIN format
dts raw dts
dv DV video format
dxa dxa
ea Electronic Arts Multimedia Format
ea_cdata Electronic Arts cdata
ffm ffm format
film_cpk Sega FILM/CPK format
flac raw flac
flic FLI/FLC/FLX animation format
flv flv format
gif GIF Animation
gxf GXF format
h261 raw h261
h263 raw h263
h264 raw H264 video format
idcin Id CIN format
image2 image2 sequence
image2pipe piped image2 sequence
ingenient Ingenient MJPEG
ipmovie Interplay MVE format
libnut nut format
m4v raw MPEG4 video format
matroska Matroska File Format
mjpeg MJPEG video
mm American Laser Games MM format
mmf mmf format
mov,mp4,m4a,3gp,3g2,mj2 QuickTime/MPEG4/Motion JPEG 2000 format
mp3 MPEG audio layer 3
mpc musepack
mpc8 musepack8
mpeg MPEG1 System format
mpegts MPEG2 transport stream format
mpegtsraw MPEG2 raw transport stream format
mpegvideo MPEG video
mulaw pcm mu law format
mxf MXF format
nsv NullSoft Video format
nut nut format
nuv NuppelVideo format
ogg Ogg format
psxstr Sony Playstation STR format
rawvideo raw video format
redir Redirector format
rm rm format
rtsp RTSP input format
s16be pcm signed 16 bit big endian format
s16le pcm signed 16 bit little endian format
s8 pcm signed 8 bit format
sdp SDP
shn raw shorten
siff Beam Software SIFF
smk Smacker Video
sol Sierra SOL Format
swf Flash format
thp THP
tiertexseq Tiertex Limited SEQ format
tta true-audio
txd txd format
u16be pcm unsigned 16 bit big endian format
u16le pcm unsigned 16 bit little endian format
u8 pcm unsigned 8 bit format
vc1 raw vc1
vmd Sierra VMD format
voc Creative Voice File format
wav wav format
wc3movie Wing Commander III movie format
wsaud Westwood Studios audio format
wsvqa Westwood Studios VQA format
wv WavPack
yuv4mpegpipe YUV4MPEG pipe format

Encoding

Formato Descrizione
3g2 3gp2 format
3gp 3gp format
RoQ Id RoQ format
ac3 raw ac3
adts ADTS AAC
aiff Audio IFF
alaw pcm A law format
amr 3gpp amr file format
asf asf format
asf_stream asf format
au SUN AU Format
avi avi format
crc crc testing format
dv DV video format
dvd MPEG2 PS format (DVD VOB)
ffm ffm format
flac raw flac
flv flv format
framecrc framecrc testing format
gif GIF Animation
gxf GXF format
h261 raw h261
h263 raw h263
h264 raw H264 video format
image2 image2 sequence
image2pipe piped image2 sequence
libnut nut format
m4v raw MPEG4 video format
matroska Matroska File Format
mjpeg MJPEG video
mmf mmf format
mov mov format
mp2 MPEG audio layer 2
mp3 MPEG audio layer 3
mp4 mp4 format
mpeg MPEG1 System format
mpeg1video MPEG video
mpeg2video MPEG2 video
mpegts MPEG2 transport stream format
mpjpeg Mime multipart JPEG format
mulaw pcm mu law format
null null video format
nut nut format
ogg Ogg format
psp psp mp4 format
rawvideo raw video format
rm rm format
rtp RTP output format
s16be pcm signed 16 bit big endian format
s16le pcm signed 16 bit little endian format
s8 pcm signed 8 bit format
svcd MPEG2 PS format (VOB)
swf Flash format
u16be pcm unsigned 16 bit big endian format
u16le pcm unsigned 16 bit little endian format
u8 pcm unsigned 8 bit format
vcd MPEG1 System format (VCD)
vob MPEG2 PS format (VOB)
voc Creative Voice File format
wav wav format
yuv4mpegpipe YUV4MPEG pipe format

Built-in decoders and encoders

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

Audio decoders

adpcm_4xm adpcm_adx adpcm_ct adpcm_ea adpcm_ea_r1
adpcm_ea_r2 adpcm_ea_r3 adpcm_ea_xas adpcm_ima_amv adpcm_ima_dk3
adpcm_ima_dk4 adpcm_ima_ea_eacs adpcm_ima_ea_sead adpcm_ima_qt adpcm_ima_smjpeg
adpcm_ima_wav adpcm_ima_ws adpcm_ms adpcm_sbpro_2 adpcm_sbpro_3
adpcm_sbpro_4 adpcm_swf adpcm_thp adpcm_xa adpcm_yamaha
alac ape atrac 3 cook dca
dsicinaudio flac g726 imc interplay_dpcm
liba52 libamr_nb libamr_wb libfaad libgsm
libgsm_ms mace3 mace6 mp2 mp3
mp3adu mp3on4 mpc sv7 mpc sv8 mpeg4aac
nellymoser pcm_alaw pcm_mulaw pcm_s16be pcm_s16le
pcm_s16le_planar pcm_s24be pcm_s24daud pcm_s24le pcm_s32be
pcm_s32le pcm_s8 pcm_u16be pcm_u16le pcm_u24be
pcm_u24le pcm_u32be pcm_u32le pcm_u8 pcm_zork
qdm2 real_144 real_288 roq_dpcm shorten
smackaud sol_dpcm sonic truespeech tta
vmdaudio vorbis wavpack wmav1 wmav2
ws_snd1 xan_dpcm      

Audio encoders

ac3 adpcm_adx adpcm_ima_wav adpcm_ms adpcm_swf
adpcm_yamaha flac g726 libamr_nb libamr_wb
libfaac libgsm libgsm_ms libmp3lame libvorbis
mp2 pcm_alaw pcm_mulaw pcm_s16be pcm_s16le
pcm_s24be pcm_s24daud pcm_s24le pcm_s32be pcm_s32le
pcm_s8 pcm_u16be pcm_u16le pcm_u24be pcm_u24le
pcm_u32be pcm_u32le pcm_u8 pcm_zork roq_dpcm
sonic sonicls vorbis wmav1 wmav2

Video decoders

4xm 8bps VMware video aasc amv
asv1 asv2 avs bethsoftvid bmp
c93 camstudio camtasia cavs cinepak
cljr cyuv dnxhd dsicinvideo dvvideo
dxa ffv1 ffvhuff flashsv flic
flv fraps gif h261 h263
h263i h264 huffyuv idcinvideo indeo2
indeo3 interplayvideo jpegls kmvc loco
mdec mjpeg mjpegb mmvideo mpeg1video
mpeg2video mpeg4 mpegvideo msmpeg4 msmpeg4v1
msmpeg4v2 msrle msvideo1 mszh nuv
pam pbm pgm pgmyuv png
ppm ptx qdraw qpeg qtrle
rawvideo roqvideo rpza rv10 rv20
sgi smackvid smc snow sp5x
svq1 svq3 targa theora thp
tiertexseqvideo tiff truemotion1 truemotion2 txd
ultimotion vb vc1 vcr1 vmdvideo
vp3 vp5 vp6 vp6a vp6f
vqavideo wmv1 wmv2 wmv3 wnv1
xan_wc3 xl zlib zmbv  

Video encoders

asv1 asv2 bmp dnxhd dvvideo
ffv1 ffvhuff flashsv flv gif
h261 h263 h263p huffyuv jpegls
libtheora libx264 libxvid ljpeg mjpeg
mpeg1video mpeg2video mpeg4 msmpeg4 msmpeg4v1
msmpeg4v2 pam pbm pgm pgmyuv
png ppm qtrle rawvideo roqvideo
rv10 rv20 sgi snow svq1
targa tiff wmv1 wmv2 zlib
zmbv        


七、执行ffmpeg的二选一

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.

你可能感兴趣的:(android_多媒体)