您的位置:首页 > 其它

资源文件信息工具类--ResourceInfoCalculator

2015-11-16 15:22 309 查看
包含获取amr播放时长,视频播放时长,以及图片的长宽

视频播放这个方法,要引入一个jar包(jave-1.0.2.jar)

具体下载地址:
http://www.sauronsoftware.it/projects/jave/download.php
上传不了,超过2M了,这个包挺大的,5M多

工具类源代码:

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.RandomAccessFile;

import javax.imageio.ImageIO;

import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.MultimediaInfo;
/**
* 获取amr播放时长,视频播放时长,以及图片的长宽
* @author dhh
* 2015-11-16
*
*/
public class ResourceInfoCalculator {
/**
* 得到amr文件的语音长度 单位:s(秒)
*
* @param file
* @return
* @throws Exception
*/
public static long getAmrDuration(File file) throws Exception {
long duration = -1;
int[] packedSize = { 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0,
0, 0 };
RandomAccessFile randomAccessFile = null;
try {
randomAccessFile = new RandomAccessFile(file, "rw");
long length = file.length();// 文件的长度
int pos = 6;// 设置初始位置
int frameCount = 0;// 初始帧数
int packedPos = -1;
// ///////////////////////////////////////////////////
byte[] datas = new byte[1];// 初始数据值
while (pos <= length) {
randomAccessFile.seek(pos);
if (randomAccessFile.read(datas, 0, 1) != 1) {
duration = length > 0 ? ((length - 6) / 650) : 0;
break;
}
packedPos = (datas[0] >> 3) & 0x0F;
pos += packedSize[packedPos] + 1;
frameCount++;
}
// ///////////////////////////////////////////////////
duration += frameCount * 20;// 帧数*20
} finally {
if (randomAccessFile != null) {
randomAccessFile.close();
}
}
return duration;
}

/**
* 得到视频文件的播放时长(单位:s)
*
* @param file
* @return
*/
public static long getVedioduration(File file) {
long duration = -1;
Encoder encoder = new Encoder();
try {
MultimediaInfo m = encoder.getInfo(file);
long ls = m.getDuration();
duration = ls/1000;
} catch (Exception e) {
return duration;
}
return duration;
}

/**
* 得到图片的宽高
*
* @param file
* @return
* @throws Exception
*/
public static Integer[] getImageWH(File file) throws Exception {
InputStream is = null;
try {
is = new FileInputStream(file);// 通过文件名称读取
BufferedImage buff = ImageIO.read(is);
Integer width = buff.getWidth(); // 得到图片的宽度
Integer height = buff.getHeight(); // 得到图片的高度
return new Integer[] { width, height };
} catch (Exception e) {
e.printStackTrace();
} finally {
is.close(); // 关闭Stream
}
return null;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息