package com.roncoo.education.util.tools; /** * @ClassName ReadVideo * @Decription 得到视频长度 * @Author dingguoxue * @Date 2019/11/2 13:53 * @Version 1.0 **/ import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.math.BigDecimal; import java.math.RoundingMode; import java.nio.channels.FileChannel; import io.swagger.models.auth.In; import org.springframework.util.StringUtils; import ws.schild.jave.MultimediaInfo; import ws.schild.jave.MultimediaObject; public class ReadVideo { public static void main(String[] args) { ReadVideo r=new ReadVideo(); String length=r.ReadVideoTime("E:\\20191029_205226.wmv"); System.out.println("总时长:"+length); System.out.println("视频大小:"+r.ReadVideoSize(new File("E:\\20191029_205226.wmv"))); } /** * 获取视频时长 * @param FileUrl * @return */ public static String ReadVideoTime(String FileUrl) { File source=new File(FileUrl); String length = ""; try { MultimediaObject instance = new MultimediaObject(source); MultimediaInfo result = instance.getInfo(); long ls = result.getDuration() / 1000; Integer hour = (int) (ls / 3600); Integer minute = (int) (ls % 3600) / 60; Integer second = (int) (ls - hour * 3600 - minute * 60); String hr=hour.toString(); String mi=minute.toString(); String se=second.toString(); if(hr.length()<2){ hr="0"+hr; } if(mi.length()<2){ mi="0"+mi; } if(se.length()<2){ se="0"+se; } length = hr + ":" + mi + ":" + se; } catch (Exception e) { e.printStackTrace(); } return length; } /** * 获取视频大小 * @param source * @return */ @SuppressWarnings({ "resource" }) public static String ReadVideoSize(File source) { FileChannel fc = null; String size = ""; try { FileInputStream fis = new FileInputStream(source); fc = fis.getChannel(); BigDecimal fileSize = new BigDecimal(fc.size()); size = fileSize.divide(new BigDecimal(1048576), 2,RoundingMode.HALF_UP) + "MB"; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (null != fc) { try { fc.close(); } catch (IOException e) { e.printStackTrace(); } } } return size; } }
所需的pom文件:
<!-- https://mvnrepository.com/artifact/ws.schild/jave-all-deps --> <dependency> <groupId>ws.schild</groupId> <artifactId>jave-all-deps</artifactId> <version>2.6.0</version> </dependency>