• 导入jar包
<!-- 获取视频长度jar包 -->
        <dependency>
            <groupId>ws.schild</groupId>
            <artifactId>jave-all-deps</artifactId>
            <version>2.5.0</version>
        </dependency>
  • 编写工具类
package xyz.anix.test.common;

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 ws.schild.jave.MultimediaInfo;
import ws.schild.jave.MultimediaObject;

/**
 * @Auther: zaa
 * @Date: 2019-06-24 10:38
 * @Description:音频视频工具类
 */
public class MediaUtil {

    public static void main(String[] args) {
        String time = ReadVideoTime("/Users/anix/Movies/webboot/第39课jvm/31.2课件地址-.mp4");
        System.out.println("视频时长为:"+time);
        String size = ReadVideoSize("/Users/anix/Movies/webboot/第39课jvm/31.2课件地址-.mp4");
        System.out.println("视频大小为:"+size);
    }

    /**
     * @描述:获取视频时长 
     * @param source
     * @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;
            int hour = (int) (ls / 3600);
            int minute = (int) (ls % 3600) / 60;
            int second = (int) (ls - hour * 3600 - minute * 60);
            length = hour + "'" + minute + "''" + second + "'''";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return length;
    }


    /**
     * @描述:获取视频大小 
     * @param source
     * @return
     */
    @SuppressWarnings({"resource"})
    public static String ReadVideoSize(String sourcepath) {
        File source = new File(sourcepath);
        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;
    }

    public static String ReadVideoTime(File source) {
        String length = "";
        try {
            MultimediaObject instance = new MultimediaObject(source);
            MultimediaInfo result = instance.getInfo();
            long ls = result.getDuration() / 1000;
            int hour = (int) (ls / 3600);
            int minute = (int) (ls % 3600) / 60;
            int second = (int) (ls - hour * 3600 - minute * 60);
            length = hour + "时" + minute + "分" + second + "秒";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return length;
    }

    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;
    }

}

  • 使用
//uploadFile为文件路径
String size = MediaUtil.ReadVideoSize(uploadFile);
String time = MediaUtil.ReadVideoTime(uploadFile);
System.out.println("视频时长为:"+time);
System.out.println("视频大小为:"+size);
最后修改于 2019-06-25 11:40:36
如果觉得我的文章对你有用,请随意赞赏
扫一扫支付
上一篇