JAVE 视音频转码

/ Java / 没有评论 / 2367浏览

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的<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()方法:

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 {
}
  1. Encoding attributes 如上所述的encoder()方法,第三个参数是很重要的,所以,你得实例化出一个EncodingAttributes.
EncodingAttributes attrs = new EncodingAttributes();

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

// 在转码音频时需要用到的方法,可以说是添加音频转码时所需音频属性。
public void setAudioAttributes(it.sauronsoftware.jave.AudioAttributes audioAttributes) ; 
// 在转码视频时需要用到的方法,可以说是添加视频转码时所需视频属性。
public void setVideoAttributes(it.sauronsoftware.jave.AudioAttributes videoAttributes) ;
// 设置转码格式的方法。
// 它设置将用于新编码文件的流容器的格式。给定的参数表示格式名。
// 只有当编码格式名出现在使用中的编码器实例的getSupportedEncodingFormats()方法返回的列表中时,该编码格式名才有效且受支持。
public void setFormat(java.lang.String format)  
// 设置转码偏移位置的方法,例如你想在5秒后开始转码源文件则setOffset(5)。
public void setOffset(java.lang.Float offset)  
// 设置转码持续时间的方法,例如你想持续30秒的转码则setDuration(30)。
public void setDuration(java.lang.Float duration)  
  1. Audio encoding attributes 同样的我们也需设置Audio的属性:
AudioAttributes audio = new AudioAttributes();

看看它的方法:

// 设置编码器  
// 它设置将用于音频流转码的编解码器的名称。
// 必须从当前编码器实例的getAudioEncoders()方法返回的列表中选择一个值。
// 否则你可以通过AudioAttributes.DIRECT_STREAM_COPY特殊值,要求从源文件复制原始音频流。
public void setCodec(java.lang.String codec)
// 设置比特率  
// 它为新的重新编码音频流设置比特率值。
// 如果未设置比特率值,编码器将选择默认值。该值应以位/秒表示。
// 在示例中,如果您想要128 kb/s的比特率,您应该调用setBitRate(new Integer(128000))。
public void setBitRate(java.lang.Integer bitRate)
// 设置节录率  
// 它设置新的重新编码音频流的采样率。
// 如果未设置采样率值,编码器将选择默认值。该值应以赫兹表示。
// 在示例中,如果您想要像CD一样的44100hz采样率,您应该调用setSamplingRate(new Integer(44100))。
public void setSamplingRate(java.lang.Integer bitRate)
// 设置声音频道  
// 它设置将在重新编码的音频流中使用的音频通道数(1=单声道,2=立体声)。
// 如果未设置通道值,编码器将选择默认值。
public void setChannels(java.lang.Integer channels)
// 设置音量  
// 可以调用此方法来更改音频流的音量。值256表示没有音量变化。
// 因此,小于256的值是音量减小,而大于256的值将增大音频流的音量。
public void setVolume(java.lang.Integer volume)
  1. Video encoding attributes
// 设置编码器  
// 它设置将用于视频流转码的编解码器的名称。
// 必须从当前编码器实例的getVideoEncoders()方法返回的列表中选择一个值。
// 否则你可以通过VideoAttributes.DIRECT_STREAM_拷贝特殊值,要求从源文件复制原始视频流。
public void setCodec(java.lang.String codec)
// 设置标签(通常用多媒体播放器所选择的视频解码)  
// 它设置与重新编码的视频流相关联的tag/fourcc值。如果未设置任何值,编码器将选取默认值。
// 多媒体播放器通常使用标记值来选择流上运行的视频解码器。
// 在示例中,具有“DIVX”标记值的MPEG 4视频流将使用播放器使用的默认DIVX解码器进行解码。
// 而且,顺便说一下,这正是DivX是什么:一个MPEG 4视频流附加了一个“DivX”标签/fourcc值!
public void setTag(java.lang.String tag)
// 设置比特率  
// 它为新的重新编码的视频流设置比特率值。如果未设置比特率值,编码器将选择默认值。该值应以位/秒表示。
// 在这个例子中,如果你想要一个360kb/s的比特率,你应该调用setBitRate(新整数(360000))。
public void setBitRate(java.lang.Integer bitRate)
// 设置帧率  
// 它设置新的重新编码音频流的帧速率值。如果未设置比特率帧速率,编码器将选择默认的比特率。
// 该值应以每秒帧数表示。在示例中,如果您想要30 f/s的帧速率,您应该调用setFrameRate(new Integer(30))。
public void setFrameRate(java.lang.Integer bitRate)
// 设置大小  
// 它设置视频流中图像的大小和比例。如果未设置值,编码器将保留原始大小和比例。
// 否则你可以通过it.sauronsoftware.java.VideoSize实例,使用您喜欢的大小。
// 可以使用像素值设置新编码视频的宽度和高度,缩放原始视频。
// 在示例中,如果要将视频的宽度缩放到512 px,高度缩放到384px,则应调用setSize(new VideoSize(512,384))。
public void setSize(it.sauronsoftware.jave.VideoSize size)
  1. Monitoring the transcoding operation 你可以用listener监测转码操作。JAVE定义了一个EncoderProgressListener的接口。
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 {

}

实现EncoderProgressListener接口,需定义的方法:

// 源文件信息  
public void sourceInfo(it.sauronsoftware.jave.MultimediaInfo info)
// 增长千分率  
public void progress(int permil)
// 转码信息提示  
public void message(java.lang.String message)

  1. Getting informations about a multimedia file 获取多媒体文件转码时的信息:
public it.sauronsoftware.jave.MultimediaInfo getInfo(java.io.File source)  
    throws it.sauronsoftware.jave.InputFormatException,  
it.sauronsoftware.jave.EncoderException  

五、例子:

  1. From a generic AVI to a youtube-like FLV movie, with an embedded MP3 audio stream: 从普通的AVI到类似youtube的FLV电影,带有嵌入式MP3音频流:
# 转换文件
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);  
  1. Next lines extracts audio informations from an AVI and store them in a plain WAV file: 下一行从AVI中提取音频信息并将其存储在纯WAV文件中:
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);  
  1. Next example takes an audio WAV file and generates a 128 kbit/s, stereo, 44100 Hz MP3 file: 下一个示例采用音频WAV文件并生成128 kbit/s、立体声、44100 Hz MP3文件:
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);  
  1. 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: 下一个解码通用AVI文件,并创建另一个具有相同源视频流和重新编码的低质量MP3音频流的AVI文件:
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);  
  1. Next one generates an AVI with MPEG 4/DivX video and OGG Vorbis audio: 下一个将生成一个包含MPEG 4/DivX视频和OGG Vorbis音频的AVI:
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);  
  1. 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);  

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

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

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

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