jave2 工具類的使用,使用信息的獲取以及AVI轉換爲MP4
使用手冊:http://www.sauronsoftware.it/projects/jave/manual.php
github地址: https://github.com/a-schild/jave2https://github.com/a-schild/jave2
maven倉庫座標:
<dependency>
<groupId>ws.schild</groupId>
<artifactId>jave-all-deps</artifactId>
<version>2.7.3</version>
</dependency>
package com.XXX;
import ws.schild.jave.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.nio.channels.FileChannel;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;
import static org.hibernate.validator.internal.util.Contracts.assertTrue;
/**
* @Description: 視頻文件的工具類
*/
public class VideoUtils {
/**
* 測試功能
*/
public static void main(String[] args){
Map<String, Object> voideMsg = getVideoMsg("C:\\Users\\權\\Desktop\\test.avi");
System.out.println(voideMsg);
videoConvertMP4("C:\\Users\\權\\Desktop\\test.avi", "C:\\Users\\權\\Desktop\\testMP4.mp4");
Map<String, Object> voideMsg2 = getVideoMsg("C:\\Users\\權\\Desktop\\testMP4.mp4");
System.out.println(voideMsg2);
}
/**
* @Description: 將視頻轉換爲MP4格式 未開發完成
*
* @param path 要轉換的文件路徑
* @param targetPath 要保存的文件路徑 ,如: C:\Users\權\Desktop\testMP4.mp4
* @return 返回值 轉化後的視頻的路徑
*/
public static void videoConvertMP4(String path, String targetPath) {
ConvertProgressListener listener = new ConvertProgressListener();
try {
File source = new File(path);
File target = new File(targetPath);
if (target.exists())
{
target.delete();
}
//設置音頻屬性
AudioAttributes audio = new AudioAttributes();
audio.setCodec("aac");
audio.setBitRate(128000);
audio.setSamplingRate(44100);
audio.setChannels(2);
// 設置視頻屬性
VideoAttributes video = new VideoAttributes();
video.setCodec("libx264");
video.setBitRate(4000000);
video.setFrameRate(15);
// 設置編碼格式
EncodingAttributes attrs = new EncodingAttributes();
attrs.setFormat("mp4");
attrs.setAudioAttributes(audio);
attrs.setVideoAttributes(video);
// 進行編碼,輸出到本地
Encoder encoder = new Encoder();
encoder.encode(new MultimediaObject(source), target, attrs,listener);
assertTrue(target.exists(), "輸出文件丟失");
} catch (Exception ex) {
ex.printStackTrace();
System.out.println("視頻轉換失敗");
}
}
/**
* @Description: 獲取視頻文件的信息
*
* @param path 文件路徑
* @return map 保存視頻文件的信息,時間,寬,高,大小,格式,
*/
public static Map<String, Object> getVideoMsg(String path){
Map<String, Object> map = new HashMap<String, Object>();
File file = new File(path);
FileChannel fc= null;
String size = "";
if(file != null){
try {
MultimediaObject object = new MultimediaObject(file);
MultimediaInfo m = object.getInfo();
long ls = m.getDuration(); // 毫秒
map.put("millisecond", ls);
map.put("timestamp", ls);
// 格式化的時間 如 01:30:37
DecimalFormat format=new DecimalFormat("00");
String time = format.format(ls/3600000)+ ":" +format.format(ls/60000)+":"+format.format(((ls)/1000 - ls/60000 * 60));
map.put("time", time);
map.put("localtime", ls/60000+"分"+((ls)/1000 - ls/60000 * 60)+"秒"); // 本地時間:2分37秒
FileInputStream fis = new FileInputStream(file);
fc= fis.getChannel();
BigDecimal fileSize = new BigDecimal(fc.size());
size = fileSize.divide(new BigDecimal(1048576), 2, RoundingMode.HALF_UP) + "MB";
map.put("height", m.getVideo().getSize().getHeight());
map.put("width", m.getVideo().getSize().getWidth());
map.put("size", size);
map.put("format", m.getFormat());
}catch (Exception e) {
e.printStackTrace();
}finally {
if (null!=fc){
try {
fc.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
return map;
}
}
/**
* @Description: 視頻轉換時的監聽器
*
*/
class ConvertProgressListener implements EncoderProgressListener {
public ConvertProgressListener() {
//code
}
public void message(String m) {
//code
}
public void progress(int p) {
//Find %100 progress
double progress = p / 1000.00;
System.out.println("轉換進度:"+progress*100+"%");
}
public void sourceInfo(MultimediaInfo m) {
//code
}
}