java实现获取视频时长和大小

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>

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值