码迷,mamicode.com
首页 > 编程语言 > 详细

java 代码获取视频时长

时间:2017-12-20 15:05:18      阅读:291      评论:0      收藏:0      [点我收藏+]

标签:www.   port   字符   tle   return   col   project   idea   public   

java 代码获取视频时长

 1 package test;
 2 
 3 import java.io.File;
 4 import java.math.BigDecimal;
 5 
 6 import it.sauronsoftware.jave.Encoder;
 7 import it.sauronsoftware.jave.MultimediaInfo;
 8 
 9 
10 public class ReadVideo {
11     /**
12      * 读取视频时长
13      * @param videoPath 视频路径
14      * @return
15      */    
16     public static String readVideo(String videoPath) {
17         String durationStr = "";
18         File file = new File(videoPath); //videoPath传入值("D:\\666.mp4")
19         Encoder encoder = new Encoder();
20         long sum = 0;
21         try {
22             MultimediaInfo m = encoder.getInfo(file);
23             sum = m.getDuration()/1000; //时长sum单位:秒
24             
25         } catch (Exception e) {
26             e.printStackTrace();
27         }
28         double sum1 = (double) sum;
29         System.out.println("视频时长总秒数:"+sum1);
30         
31         BigDecimal duration =BigDecimal.valueOf(sum);
32         durationStr = durationFormatToString(duration);
33         System.out.println("视频时长:"+durationStr);
34         
35         return durationStr;
36     }
37     
38     /**
39      * 将视频时长转换成"00:00:00.000"的字符串格式            
40      * @param duration 视频时长(单位:秒)
41      * @return
42      */
43     public static String durationFormatToString(BigDecimal duration)
44     {
45         BigDecimal nine = BigDecimal.valueOf(9);
46         BigDecimal sixty = BigDecimal.valueOf(60);    
47         
48         BigDecimal second = duration.divideAndRemainder(sixty)[1];
49         BigDecimal minute = duration.subtract(second).divide(sixty).divideAndRemainder(sixty)[1];
50         BigDecimal hour = duration.subtract(second).divideToIntegralValue(BigDecimal.valueOf(3600));
51         
52         String str = "";
53         if (hour.compareTo(nine)>0)
54         {
55             str += hour.intValue() + ":";
56         }
57         else
58         {
59             str += "0" + hour.intValue() + ":";
60         }
61         if (minute.compareTo(nine)>0)
62         {
63             str += minute.intValue() + ":";
64         }
65         else
66         {
67             str += "0" + minute.intValue() + ":";
68         }
69         if (second.compareTo(nine)>0)
70         {
71             str += second.intValue() + ".000";
72         }
73         else
74         {
75             str +="0"+ second.intValue() + ".000";
76         }
77         return str;
78     }
79 
80 }

需要引入jar包 下载地址

http://www.sauronsoftware.it/projects/jave/download.php

https://pan.baidu.com/s/1slTz7it

 

参考网址:http://www.cnblogs.com/mysgk/p/4117134.html

java 代码获取视频时长

标签:www.   port   字符   tle   return   col   project   idea   public   

原文地址:http://www.cnblogs.com/jronny/p/8072695.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!