package time;
import java.io.File;
import org.apache.log4j.Logger;
import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.EncoderException;
import it.sauronsoftware.jave.MultimediaInfo;
public class Test2 {
/* 支持的后缀 */
private static final String[] SUFFIX_SUPPORT = { ".avi", ".mp4" };
private static Logger logger = Logger.getLogger(Test2.class);
private static long countTime = 0;
public static void main(String[] args) throws EncoderException {
/*if (args.length != 1) {
throw new RuntimeException("参数错误");
}*/
String path="D:\\BaiduYunDownload\\视频";
File file = new File(path);
long ls = traverseFileGetLongTime(file);
long hour = ls / 3600000;
long min = (ls - hour * 3600000) / 60000;
long sec = (ls - hour * 3600000 - min * 60000) / 1000;
System.out.println("视频时长:" + hour + "时:" + min + "分:" + sec + "秒");
}
private static long traverseFileGetLongTime(File rootFile) throws EncoderException {
if (rootFile.isDirectory()) {
File[] files = rootFile.listFiles();
for (File file : files) {
traverseFileGetLongTime(file);
}
} else {
if (isVideo(rootFile)) {
countTime += getLongTime(rootFile);
}
}
return countTime;
}
/**
* 是否是视频文件
*
* @param rootFile
* 文件
* @return 是否是视频文件
*/
private static boolean isVideo(File rootFile) {
String name = rootFile.getName();
for (int i = 0; i < SUFFIX_SUPPORT.length; i++) {
if (name.toLowerCase().endsWith(SUFFIX_SUPPORT[i])) {
logger.debug("视频文件:" + rootFile.getAbsolutePath());
return true;
}
}
return false;
}
/**
* 统计视频文件时长
*
* @param root
* @return
* @throws EncoderException
*/
public static long getLongTime(File root) throws EncoderException {
long t = 0;
Encoder encoder = new Encoder();
MultimediaInfo multimediaInfo = encoder.getInfo(root);
t += multimediaInfo.getDuration();
return t;
}
}
1、jave 下载地址:http://download.csdn.net/download/zengmingen/10016811
2、logger