欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JAVE

程序员文章站 2024-03-01 13:12:52
...

转自 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);
	}
}
 

 

 

相关标签: HTML PHP