使用 JAVE 对音视频转码

jopen 9年前

JAVE (Java Audio Video Encoder) 类库是一个 ffmpeg 项目的 Java 语言封装。开发人员可以使用JAVE 在不同的格式间转换视频和音频。例如将 AVI 转成 MPEG 动画,等等 ffmpeg 中可以完成的在 JAVE 都有对应的方法。 摘要 JAVE (Java Audio Video Encoder) 类库是一个 ffmpeg 项目的 Java 语言封装。开发人员可以使用JAVE 在不同的格式间转换视频和音频。例如将 AVI 转成 MPEG 动画,等等 ffmpeg 中可以完成的在 JAVE 都有对应的方法。

JAVE 官方的jar包带了ffmpeg的exe程序,所以在windows上可以直接使用,但是如果要在Linux上用就得先把ffmpeg的环境装好。

官方的jar包在这里下载:http://www.sauronsoftware.it/projects/jave/download.php

在这里列几个官方的demo:

1、From a generic AVI to a 油Tube-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);



2、 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);



3、 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);



4、 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);



5、 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);



6、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);



简单来说,其实JAVE就是个工具类,封装了ffmpeg的转换命令,不用自己拼接命令参数,简化了使用ffmpeg的操作步骤,大家有兴趣的话,可以看下源码,自己扩展优化。

这里发两个其它的博客地址,大家可以参考下:

http://mfan.iteye.com/blog/2032454

http://blog.csdn.net/qllinhongyu/article/details/29817297

</div>