有时间可以写成递归的
1 package org.zln.video.demo1; 2 3 import it.sauronsoftware.jave.Encoder; 4 import it.sauronsoftware.jave.EncoderException; 5 import it.sauronsoftware.jave.InputFormatException; 6 import it.sauronsoftware.jave.MultimediaInfo; 7 8 import java.io.File; 9 10 /** 11 * 统计目录下视频时长 12 * Created by coolkid on 2015/6/13 0013. 13 */ 14 public class CountTime { 15 /*支持的后缀*/ 16 private static final String[] SUFFIX_SUPPORT = {".avi",".mp4"}; 17 18 public static void main(String[] args) throws EncoderException { 19 if (args.length!=1){ 20 throw new RuntimeException("参数错误"); 21 } 22 File file = new File(args[0]); 23 long ls = getLongTime(file); 24 long hour = ls/3600000; 25 long min = (ls - hour*3600000)/60000; 26 long sec = (ls - hour*3600000 - min*60000)/1000; 27 System.out.println("视频时长:"+hour+"时:"+min+"分:"+sec+"秒"); 28 29 } 30 31 public static long getLongTime(File root) throws EncoderException { 32 long t = 0; 33 File[] files = root.listFiles(); 34 for (File file:files){ 35 if (file.getName().endsWith(".mp4")){ 36 Encoder encoder = new Encoder(); 37 MultimediaInfo multimediaInfo = encoder.getInfo(file); 38 t += multimediaInfo.getDuration(); 39 } 40 } 41 return t; 42 } 43 }
这里用到了jave.jar
将上述代码进行优化,实现了统计指定目录下,及其子目录下的 avi,mp4格式文件的视频总时长的统计
1 package org.zln.video.demo1; 2 3 import it.sauronsoftware.jave.Encoder; 4 import it.sauronsoftware.jave.EncoderException; 5 import it.sauronsoftware.jave.MultimediaInfo; 6 import org.apache.log4j.Logger; 7 8 import java.io.File; 9 10 /** 11 * 统计目录下视频时长 12 * Created by coolkid on 2015/6/13 0013. 13 */ 14 public class CountTime { 15 /*支持的后缀*/ 16 private static final String[] SUFFIX_SUPPORT = {".avi",".mp4"}; 17 18 private static Logger logger = Logger.getLogger(CountTime.class); 19 20 private static long countTime = 0; 21 22 public static void main(String[] args) throws EncoderException { 23 if (args.length!=1){ 24 throw new RuntimeException("参数错误"); 25 } 26 File file = new File(args[0]); 27 long ls = traverseFileGetLongTime(file); 28 long hour = ls/3600000; 29 long min = (ls - hour*3600000)/60000; 30 long sec = (ls - hour*3600000 - min*60000)/1000; 31 System.out.println("视频时长:"+hour+"时:"+min+"分:"+sec+"秒"); 32 33 } 34 35 private static long traverseFileGetLongTime(File rootFile) throws EncoderException { 36 if (rootFile.isDirectory()){ 37 File[] files = rootFile.listFiles(); 38 for (File file:files){ 39 traverseFileGetLongTime(file); 40 } 41 }else { 42 if (isVideo(rootFile)){ 43 countTime += getLongTime(rootFile); 44 } 45 46 } 47 return countTime; 48 } 49 50 /** 51 * 是否是视频文件 52 * @param rootFile 文件 53 * @return 是否是视频文件 54 */ 55 private static boolean isVideo(File rootFile) { 56 String name = rootFile.getName(); 57 for (int i = 0; i < SUFFIX_SUPPORT.length; i++) { 58 if (SUFFIX_SUPPORT[i].equalsIgnoreCase(name.substring(name.lastIndexOf(".")))){ 59 logger.debug("视频文件:"+rootFile.getAbsolutePath()); 60 return true; 61 } 62 } 63 return false; 64 } 65 66 /** 67 * 统计视频文件时长 68 * @param root 69 * @return 70 * @throws EncoderException 71 */ 72 public static long getLongTime(File root) throws EncoderException { 73 long t = 0; 74 Encoder encoder = new Encoder(); 75 MultimediaInfo multimediaInfo = encoder.getInfo(root); 76 t += multimediaInfo.getDuration(); 77 return t; 78 } 79 }
继续修改:上述代码存在一个bug,就是当遍历到无后缀名的文件时,会抛出异常
1 package org.zln.video.demo1; 2 3 import it.sauronsoftware.jave.Encoder; 4 import it.sauronsoftware.jave.EncoderException; 5 import it.sauronsoftware.jave.MultimediaInfo; 6 import org.apache.log4j.Logger; 7 8 import java.io.File; 9 10 /** 11 * 统计目录下视频时长 12 * Created by coolkid on 2015/6/13 0013. 13 */ 14 public class CountTime { 15 /*支持的后缀*/ 16 private static final String[] SUFFIX_SUPPORT = {".avi",".mp4"}; 17 18 private static Logger logger = Logger.getLogger(CountTime.class); 19 20 private static long countTime = 0; 21 22 public static void main(String[] args) throws EncoderException { 23 if (args.length!=1){ 24 throw new RuntimeException("参数错误"); 25 } 26 File file = new File(args[0]); 27 long ls = traverseFileGetLongTime(file); 28 long hour = ls/3600000; 29 long min = (ls - hour*3600000)/60000; 30 long sec = (ls - hour*3600000 - min*60000)/1000; 31 System.out.println("视频时长:"+hour+"时:"+min+"分:"+sec+"秒"); 32 33 } 34 35 private static long traverseFileGetLongTime(File rootFile) throws EncoderException { 36 if (rootFile.isDirectory()){ 37 File[] files = rootFile.listFiles(); 38 for (File file:files){ 39 traverseFileGetLongTime(file); 40 } 41 }else { 42 if (isVideo(rootFile)){ 43 countTime += getLongTime(rootFile); 44 } 45 46 } 47 return countTime; 48 } 49 50 /** 51 * 是否是视频文件 52 * @param rootFile 文件 53 * @return 是否是视频文件 54 */ 55 private static boolean isVideo(File rootFile) { 56 String name = rootFile.getName(); 57 for (int i = 0; i < SUFFIX_SUPPORT.length; i++) { 58 if (name.toLowerCase().endsWith(SUFFIX_SUPPORT[i])){ 59 logger.debug("视频文件:"+rootFile.getAbsolutePath()); 60 return true; 61 } 62 } 63 return false; 64 } 65 66 /** 67 * 统计视频文件时长 68 * @param root 69 * @return 70 * @throws EncoderException 71 */ 72 public static long getLongTime(File root) throws EncoderException { 73 long t = 0; 74 Encoder encoder = new Encoder(); 75 MultimediaInfo multimediaInfo = encoder.getInfo(root); 76 t += multimediaInfo.getDuration(); 77 return t; 78 } 79 }
另一种用法
[完整代码](https://gitee.com/sherryBy/test-java/tree/master/demo/java-video)