JAVE 视音频转码

来源:互联网 发布:淘宝伊舜数码靠谱吗 编辑:程序博客网 时间:2024/06/07 05:37
官方参考文档: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的<audio>标签,还是众多的播放插件都不支持amr格式文件的播放。所以,你不得不先把它转码为常见的MP3等类型文件。


三、所需环境与配置

    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

FormatoDescrizione4xm4X Technologies formatMTVMTV formatRoQId RoQ formataacADTS AACac3raw ac3aiffAudio IFFalawpcm A law formatamr3gpp amr file formatapcCRYO APC formatapeMonkey's Audioasfasf formatauSUN AU Formataviavi formatavsAVISynthbethsoftvidBethesda Softworks 'Daggerfall' VID formatc93Interplay C93daudD-Cinema audio formatdsicinDelphine Software International CIN formatdtsraw dtsdvDV video formatdxadxaeaElectronic Arts Multimedia Formatea_cdataElectronic Arts cdataffmffm formatfilm_cpkSega FILM/CPK formatflacraw flacflicFLI/FLC/FLX animation formatflvflv formatgifGIF AnimationgxfGXF formath261raw h261h263raw h263h264raw H264 video formatidcinId CIN formatimage2image2 sequenceimage2pipepiped image2 sequenceingenientIngenient MJPEGipmovieInterplay MVE formatlibnutnut formatm4vraw MPEG4 video formatmatroskaMatroska File FormatmjpegMJPEG videommAmerican Laser Games MM formatmmfmmf formatmov,mp4,m4a,3gp,3g2,mj2QuickTime/MPEG4/Motion JPEG 2000 formatmp3MPEG audio layer 3mpcmusepackmpc8musepack8mpegMPEG1 System formatmpegtsMPEG2 transport stream formatmpegtsrawMPEG2 raw transport stream formatmpegvideoMPEG videomulawpcm mu law formatmxfMXF formatnsvNullSoft Video formatnutnut formatnuvNuppelVideo formatoggOgg formatpsxstrSony Playstation STR formatrawvideoraw video formatredirRedirector formatrmrm formatrtspRTSP input formats16bepcm signed 16 bit big endian formats16lepcm signed 16 bit little endian formats8pcm signed 8 bit formatsdpSDPshnraw shortensiffBeam Software SIFFsmkSmacker VideosolSierra SOL FormatswfFlash formatthpTHPtiertexseqTiertex Limited SEQ formatttatrue-audiotxdtxd formatu16bepcm unsigned 16 bit big endian formatu16lepcm unsigned 16 bit little endian formatu8pcm unsigned 8 bit formatvc1raw vc1vmdSierra VMD formatvocCreative Voice File formatwavwav formatwc3movieWing Commander III movie formatwsaudWestwood Studios audio formatwsvqaWestwood Studios VQA formatwvWavPackyuv4mpegpipeYUV4MPEG pipe format

Encoding

FormatoDescrizione3g23gp2 format3gp3gp formatRoQId RoQ formatac3raw ac3adtsADTS AACaiffAudio IFFalawpcm A law formatamr3gpp amr file formatasfasf formatasf_streamasf formatauSUN AU Formataviavi formatcrccrc testing formatdvDV video formatdvdMPEG2 PS format (DVD VOB)ffmffm formatflacraw flacflvflv formatframecrcframecrc testing formatgifGIF AnimationgxfGXF formath261raw h261h263raw h263h264raw H264 video formatimage2image2 sequenceimage2pipepiped image2 sequencelibnutnut formatm4vraw MPEG4 video formatmatroskaMatroska File FormatmjpegMJPEG videommfmmf formatmovmov formatmp2MPEG audio layer 2mp3MPEG audio layer 3mp4mp4 formatmpegMPEG1 System formatmpeg1videoMPEG videompeg2videoMPEG2 videompegtsMPEG2 transport stream formatmpjpegMime multipart JPEG formatmulawpcm mu law formatnullnull video formatnutnut formatoggOgg formatpsppsp mp4 formatrawvideoraw video formatrmrm formatrtpRTP output formats16bepcm signed 16 bit big endian formats16lepcm signed 16 bit little endian formats8pcm signed 8 bit formatsvcdMPEG2 PS format (VOB)swfFlash formatu16bepcm unsigned 16 bit big endian formatu16lepcm unsigned 16 bit little endian formatu8pcm unsigned 8 bit formatvcdMPEG1 System format (VCD)vobMPEG2 PS format (VOB)vocCreative Voice File formatwavwav formatyuv4mpegpipeYUV4MPEG 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_r1adpcm_ea_r2adpcm_ea_r3adpcm_ea_xasadpcm_ima_amvadpcm_ima_dk3adpcm_ima_dk4adpcm_ima_ea_eacsadpcm_ima_ea_seadadpcm_ima_qtadpcm_ima_smjpegadpcm_ima_wavadpcm_ima_wsadpcm_msadpcm_sbpro_2adpcm_sbpro_3adpcm_sbpro_4adpcm_swfadpcm_thpadpcm_xaadpcm_yamahaalacapeatrac 3cookdcadsicinaudioflacg726imcinterplay_dpcmliba52libamr_nblibamr_wblibfaadlibgsmlibgsm_msmace3mace6mp2mp3mp3adump3on4mpc sv7mpc sv8mpeg4aacnellymoserpcm_alawpcm_mulawpcm_s16bepcm_s16lepcm_s16le_planarpcm_s24bepcm_s24daudpcm_s24lepcm_s32bepcm_s32lepcm_s8pcm_u16bepcm_u16lepcm_u24bepcm_u24lepcm_u32bepcm_u32lepcm_u8pcm_zorkqdm2real_144real_288roq_dpcmshortensmackaudsol_dpcmsonictruespeechttavmdaudiovorbiswavpackwmav1wmav2ws_snd1xan_dpcm   

Audio encoders

ac3adpcm_adxadpcm_ima_wavadpcm_msadpcm_swfadpcm_yamahaflacg726libamr_nblibamr_wblibfaaclibgsmlibgsm_mslibmp3lamelibvorbismp2pcm_alawpcm_mulawpcm_s16bepcm_s16lepcm_s24bepcm_s24daudpcm_s24lepcm_s32bepcm_s32lepcm_s8pcm_u16bepcm_u16lepcm_u24bepcm_u24lepcm_u32bepcm_u32lepcm_u8pcm_zorkroq_dpcmsonicsoniclsvorbiswmav1wmav2

Video decoders

4xm8bpsVMware videoaascamvasv1asv2avsbethsoftvidbmpc93camstudiocamtasiacavscinepakcljrcyuvdnxhddsicinvideodvvideodxaffv1ffvhuffflashsvflicflvfrapsgifh261h263h263ih264huffyuvidcinvideoindeo2indeo3interplayvideojpeglskmvclocomdecmjpegmjpegbmmvideompeg1videompeg2videompeg4mpegvideomsmpeg4msmpeg4v1msmpeg4v2msrlemsvideo1mszhnuvpampbmpgmpgmyuvpngppmptxqdrawqpegqtrlerawvideoroqvideorpzarv10rv20sgismackvidsmcsnowsp5xsvq1svq3targatheorathptiertexseqvideotifftruemotion1truemotion2txdultimotionvbvc1vcr1vmdvideovp3vp5vp6vp6avp6fvqavideowmv1wmv2wmv3wnv1xan_wc3xlzlibzmbv 

Video encoders

asv1asv2bmpdnxhddvvideoffv1ffvhuffflashsvflvgifh261h263h263phuffyuvjpeglslibtheoralibx264libxvidljpegmjpegmpeg1videompeg2videompeg4msmpeg4msmpeg4v1msmpeg4v2pampbmpgmpgmyuvpngppmqtrlerawvideoroqvideorv10rv20sgisnowsvq1targatiffwmv1wmv2zlibzmbv    


七、执行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.

    额~天气炎热心情烦躁,上面的就大家自己去翻译吧~


    结语:关于JAVE的资料网络上也比较少,只好去翻阅官方文档,但自己英语水平一般,理解的也许不够透彻,文章中存在着很多漏洞,还望包涵并指正。有些东西不一定别人说的就完全正确,还是看官方的比较靠谱。