【java实用小程序】用java实现统计文件夹中所有视频时长

实现项目三步走:

  • 1、利用递归遍历所有文件
  • 2、判断文件是否是视频文件
  • 3、计算视频文件时长(需要引入jar包)
    需要用到的jar包

点击免费领取

package com.time;

import java.io.File;
import java.util.ArrayList;

import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.EncoderException;
import it.sauronsoftware.jave.InputFormatException;
import it.sauronsoftware.jave.MultimediaInfo;
/**
 * 该项目一共分为三步
 * 1、利用递归遍历所有文件
 * 2、判断文件是否是视频文件
 * 3、计算视频文件时长(需要引入jar包)
 * @author yky
 *
 */
public class Time {
	private static ArrayList<String> filelist=new ArrayList<>();
	private static long totalTime; 
	public static void main(String[] args) {
		String path="D:\\迅雷下载";//放入需要统计文件夹绝对路径
		getFile(path);
		System.out.println("文件夹中视屏总时长为"+Int2String(totalTime)+"min");
	}
	public static void getFile(String path) {
		File root=new File(path);
		File[] files=root.listFiles();
		for(File file:files) {
			if(file.isDirectory()) {
				getFile(file.getAbsolutePath());
				filelist.add(file.getAbsolutePath());
				System.out.println(path+"下的目录"+file.getAbsolutePath());
			}else {
				String fileName=file.getName().trim();
				String type=fileName.substring(fileName.lastIndexOf("."));
				if(type.equals(".mp4")) {
					System.out.print(fileName+"是视屏文件,文件的大小为"+file.length()+"   ");
					MultimediaInfo m = null;
					Encoder encoder = new Encoder();
					try {
						m = encoder.getInfo(file);
					} catch (InputFormatException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (EncoderException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					long ls = m.getDuration();
		            String vtime=Int2String(ls);
		            System.out.println(vtime);
		            totalTime+=ls;
				}else {
					System.out.println(fileName+"不是视屏文件");
				}
			}
		}
	}
	//将文件时长单位转换为min
	public static String Int2String(long ls) {
		String vtime;
		if ((ls / 60000) < 10) {
            vtime = "0" + ls / 60000 + "." + ((ls / 1000) - ((ls / 60000) * 60));

        } else if ((ls / 60000) < 10 && ((ls / 1000) - ((ls / 60000) * 60)) < 10) {
            vtime = "0" + ls / 60000 + "." + "0" + ((ls / 1000) - ((ls / 60000) * 60));

        } else if ((ls / 60000) > 9 && ((ls / 1000) - ((ls / 60000) * 60)) < 10) {
            vtime = ls / 60000 + "." + "0" + ((ls / 1000) - ((ls / 60000) * 60));

        } else {
            vtime = +ls / 60000 + "." + ((ls / 1000) - ((ls / 60000) * 60));
        }
		return vtime;
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值