`

JAVE

    博客分类:
  • Java
阅读更多

转自 http://ajava.org/opens/dmtzj/10094.html

 

 

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

 

下面例子将 AVI 动画转成 FLV 格式:

 

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

 

 

项目主页: http://www.sauronsoftware.it/projects/jave/

文档地址: http://www.sauronsoftware.it/projects/jave/manual.php

下载地址: http://www.sauronsoftware.it/projects/jave/download.php

 

 

 

 

实例:

import it.sauronsoftware.jave.AudioAttributes;
import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.EncoderProgressListener;
import it.sauronsoftware.jave.EncodingAttributes;
import it.sauronsoftware.jave.MultimediaInfo;

import java.io.File;

public class JAVE {

	public static void main(String args[]) {
		wav2mp3("source.wav", "target.mp3", 320000);
		mp32mp3("target.mp3", "target1.mp3", 192000);
		mp32wav("target1.mp3", "source1.wav");
	}

	public static void wav2mp3(String src, String dst, int bitrate) {
		try {
			File source = new File(src);
			File target = new File(dst);
			AudioAttributes audio = new AudioAttributes();
			audio.setCodec("libmp3lame");
			audio.setBitRate(bitrate);
			// audio.setChannels(1);
			// audio.setSamplingRate(48000);
			EncodingAttributes attrs = new EncodingAttributes();
			attrs.setFormat("mp3");
			attrs.setAudioAttributes(audio);
			Encoder encoder = new Encoder();
			encoder.encode(source, target, attrs, new MyProgressListener());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void mp32wav(String src, String dst) {
		try {
			File source = new File(src);
			File target = new File(dst);
			AudioAttributes audio = new AudioAttributes();
			// audio.setChannels(1);
			// audio.setSamplingRate(48000);
			EncodingAttributes attrs = new EncodingAttributes();
			attrs.setFormat("wav");
			attrs.setAudioAttributes(audio);
			Encoder encoder = new Encoder();
			encoder.encode(source, target, attrs, new MyProgressListener());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void mp32mp3(String src, String dst, int bitrate) {
		try {
			File source = new File(src);
			File target = new File(dst);
			AudioAttributes audio = new AudioAttributes();
			audio.setCodec("libmp3lame");
			audio.setBitRate(bitrate);
			// audio.setChannels(1);
			// audio.setSamplingRate(48000);
			EncodingAttributes attrs = new EncodingAttributes();
			attrs.setFormat("mp3");
			attrs.setAudioAttributes(audio);
			Encoder encoder = new Encoder();
			encoder.encode(source, target, attrs, new MyProgressListener());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

class MyProgressListener implements EncoderProgressListener {

	/**
	 * This method is called before the encoding process starts, reporting
	 * information about the source stream that will be decoded and re-encoded.
	 * 
	 * @param info
	 *            Informations about the source multimedia stream.
	 */
	public void sourceInfo(MultimediaInfo info) {
		System.out.println(info);
	}

	/**
	 * This method is called to notify a progress in the encoding process.
	 * 
	 * @param permil
	 *            A permil value representing the encoding process progress.
	 */
	public void progress(int permil) {
		System.out.printf("\r%d%%", permil / 10);
	}

	/**
	 * This method is called every time the encoder need to send a message
	 * (usually, a warning).
	 * 
	 * @param message
	 *            The message sent by the encoder.
	 */
	public void message(String message) {
		System.out.println("\n\n" + message);
	}
}
 

 

 

分享到:
评论
1 楼 zhaoleiJE 2012-11-30  
public static String toMPTree(String sourcePath,String targetPath) throws IllegalArgumentException, InputFormatException, EncoderException{
		File source = new File(sourcePath);  
		File target = new File(targetPath);
		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);
		return targetPath;
	}

linux转的mp3都是0字节

相关推荐

    完美解决Jave在linux下转为MP3时为0字节或其他异常

    jave-1.0.2.2.jar完全解决如下问题: 1、报错:it.sauronsoftware.jave.EncoderException: Metadata: 2、可以转换,可以播放,但是有个异常:it.sauronsoftware.jave.EncoderException: video:0kB audio:1301kB ...

    最新修复版Jave转换mp3、wav等语音,测试完美运行64位linux和windows

    jave-1.0.3.jar解决已知如下问题: 1、it.sauronsoftware.jave.EncoderException: Metadata 2、it.sauronsoftware.jave.EncoderException: video:0kB audio:1301kB subtitle:0kB other streams:0kB global headers:0...

    jave1.2.jar

    jave1.0.2.jar包 。

    解决Jave在linux下转为MP3时为0字节或其他异常或转码后只有1分钟时长

    jave-1.0.2.3.jar完全解决如下问题: 1、报错:it.sauronsoftware.jave.EncoderException: Metadata: 2、可以转换,可以播放,但是有个异常:it.sauronsoftware.jave.EncoderException: video:0kB audio:1301kB ...

    jar包it.sauronsoftware.jave

    it.sauronsoftware.jave包下载

    jave-2.0.jar支持Linux和Windows,讯飞音频转码

    jave-2.0.jar 因开发项目需要用到科大讯飞语音转文字和语义接口,微信小程序录音文件是aac格式,微信公众号录音文件是amr格式,而讯飞只支持这些格式的音频文件“raw(未压缩的pcm或wav格式)、speex(speex格式,即...

    JAVE-1.0.2 jar

    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 ...

    Android代码-jave

    因为是基于 JAVE 项目的修改,而 JAVE 是依赖 ffmpeg 所以可以适用于所有 ffmpeg 所支持的文件格式的转换。具体可以查看 JAVE 官方文档 使用示例 引入 maven 依赖 com.github.dadiyang jave 1.0.3 调用 ...

    jar包jave-1.0.2.jar

    可计算获取视频时长,jave-1.0.2.jar 。简单使用的jar包

    jave-1.0.2.rar

    jave-1.0.2.jar用于获取音频文件长度。下载解压导入即可!

    jave.jar(包含jave-1.0.2.jar和jave-2.0.jar)

    jave.jar 包含两个版本,分别是jave-1.0.2.jar和jave-2.0.jar,另外赠送commons-logging-1.1.1.jar

    视频计算时长jar包jave-1.0.2.jar

    it\sauronsoftware\jave\1.0.2的jave包,主要用于获取视频时长

    jave-1.0.2-src.jar

    jave amr转mp3 64位 liunx&windows 有一个问题,win10上会失败,报错如下: it.sauronsoftware.jave.EncoderException: java.io.IOException: Cannot run program "C:\Users\moxiao\AppData\Local\Temp\jave-1\...

    jave-1.0.jar

    由于网上没法下载jave视频处理的jar包 在此提供一下jave包 maven 本地引用的方式 <groupId>it.sauronsoftware <artifactId>jave <scope>system ${project.basedir}/libs/jave-1.0.jar </dependency>

    Jave script 语法电子手册

    Jave script 语法电子手册Jave script 语法电子手册

    jave-1.0.2.jar

    jave的jar (jave-1.0.2.jar)jave的jar (jave-1.0.2.jar)

    jave的触屏通用游戏

    这个游戏是触屏虚拟键盘两用的游戏,值得我们去玩一下,不过是jave的游戏哦

    jave 继承、封装、多态

    jave面向对象特性继承、封装、多态的实验报告,jave重要实验。

    jave-1.0.2(jar包).zip

    jave-1.0.2(jar包).zip,解决音视频处理,注意版本是1.0.2,亲测可用,里面带文档注释

    jave-1.0.2

    jave-1.0.2.jar 视频工具类 Java 好不容易找到的 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...

Global site tag (gtag.js) - Google Analytics