java获取视频时长、大小

项目中有这样一个需求,网页上上传了一个视频,需要获取此视频的时长、大小,把这两个数据返回给前台在页面显示。后台使用的是springboot框架,项目部署在linux上面。

pom文件中需要导入的jar包依赖:
(分为两部分:核心包、ffmpeg包两部分,ffmpeg包又分为Windows环境以及Linux环境,同时又区分32位系统以及64位系统。针对于不同的运行环境要导入不同的包,这一点对于开发、测试环境为Window而生产环境为Linux的情况,尤其要注意需要导入所有的包,使其在两种环境下都能够使用)

jar<dependency>
    <groupId>ws.schild</groupId>
    <artifactId>jave-all-deps</artifactId>
    <version>2.6.0</version>
</dependency>
<dependency>
    <groupId>ws.schild</groupId>
    <artifactId>jave-core</artifactId>
    <version>2.4.5</version>
</dependency>

<!-- window32ffmpeg -->
<dependency>
    <groupId>ws.schild</groupId>
    <artifactId>jave-native-win32</artifactId>
    <version>2.4.5</version>
</dependency>

<!-- window64ffmpeg -->
<dependency>
    <groupId>ws.schild</groupId>
    <artifactId>jave-native-win64</artifactId>
    <version>2.4.5</version>
</dependency>

<!-- linux32ffmpeg -->
<dependency>
    <groupId>ws.schild</groupId>
    <artifactId>jave-native-linux32</artifactId>
    <version>2.4.6</version>
</dependency>

<!-- linux64ffmpeg -->
<dependency>
    <groupId>ws.schild</groupId>
    <artifactId>jave-native-linux64</artifactId>
    <version>2.4.6</version>
</dependency>


代码
/**
 * 视频时长
 *
 * @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
 */
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(1024 * 1024), 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;
}
发布者:songJian   点击数:23   发布时间:2020-10-09 14:25:06   更新时间:2020-10-09 14:25:06
正在加载评论...
相关文章