Java使用ffmpeg.exe获取视频文件时长

/**
	 * 获取视频总时间
	 * @param viedo_path    视频路径
	 * @param ffmpeg_path	ffmpeg路径
	 * @return
	 */
	public static int getVideoTime(String video_path, String ffmpeg_path) {
		List<String> commands = new java.util.ArrayList<String>();
		commands.add(ffmpeg_path);
		commands.add("-i");
		commands.add(video_path);
		try {
			ProcessBuilder builder = new ProcessBuilder();
			builder.command(commands);
			final Process p = builder.start();
			
			//从输入流中读取视频信息
	        BufferedReader br = new BufferedReader(new InputStreamReader(p.getErrorStream()));
	        StringBuffer sb = new StringBuffer();
	        String line = "";
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
	        br.close();
	        
	        //从视频信息中解析时长
	        String regexDuration = "Duration: (.*?), start: (.*?), bitrate: (\\d*) kb\\/s";
	        Pattern pattern = Pattern.compile(regexDuration);
	        Matcher m = pattern.matcher(sb.toString());
	        if (m.find()) {
	        	int time = getTimelen(m.group(1));
	        	log.info(video_path+",视频时长:"+time+", 开始时间:"+m.group(2)+",比特率:"+m.group(3)+"kb/s");
	        	return time;
	        }
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return 0;
	}
	
	//格式:"00:00:10.68"
    private static int getTimelen(String timelen){
        int min=0;
        String strs[] = timelen.split(":");
        if (strs[0].compareTo("0") > 0) {
            min+=Integer.valueOf(strs[0])*60*60;//秒
        }
        if(strs[1].compareTo("0")>0){
            min+=Integer.valueOf(strs[1])*60;
        }
        if(strs[2].compareTo("0")>0){
            min+=Math.round(Float.valueOf(strs[2]));
        }
        return min;
    }

转自:https://blog.csdn.net/cyh1111/article/details/50232363

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值