统计指定目录下的视频时长


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

}


需要依赖的jar包:

1、jave  下载地址:http://download.csdn.net/download/zengmingen/10016811

2、logger





  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

松门一枝花

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值