zoukankan      html  css  js  c++  java
  • java 获取视频时长、大小

        项目中有这样一个需求,网页上上传了一个视频,需要获取此视频的时长、大小,把这两个数据返回给前台在页面显示。后台使用的是springboot框架,项目部署在linux上面。下面是核心代码:
        1、pom文件中需要导入的jar包依赖

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

        <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>
    
        <!-- window32位 ffmpeg -->
        <dependency>
            <groupId>ws.schild</groupId>
            <artifactId>jave-native-win32</artifactId>
            <version>2.4.5</version>
        </dependency>
    
        <!-- window64位 ffmpeg -->
        <dependency>
            <groupId>ws.schild</groupId>
            <artifactId>jave-native-win64</artifactId>
            <version>2.4.5</version>
        </dependency>
    
        <!-- linux32位 ffmpeg -->
        <dependency>
            <groupId>ws.schild</groupId>
            <artifactId>jave-native-linux32</artifactId>
            <version>2.4.6</version>
        </dependency>
    
        <!-- linux64位 ffmpeg -->
        <dependency>
            <groupId>ws.schild</groupId>
            <artifactId>jave-native-linux64</artifactId>
            <version>2.4.6</version>
        </dependency>
    

        2、controller层代码

    package com.aaa.bbb.controller;

    import io.swagger.annotations.Api;
    import lombok.extern.slf4j.Slf4j;
    import org.apache.commons.io.FileUtils;
    import org.springframework.util.ResourceUtils;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    import ws.schild.jave.MultimediaInfo;
    import ws.schild.jave.MultimediaObject;

    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;

    @Slf4j
    @RestController
    @RequestMapping("/readVideo")
    @Api(tags = "获取视频时长、大小相关信息的接口")
    public class ReadVideoController {

    /**
    
     * 获取视频时长
     *
     * @param fileUrl
     * @return
     */
    @PostMapping("/videoLengthAndSize")
    public static String getLengthAndSize(@RequestParam String fileUrl) throws FileNotFoundException {
        ReadVideoController r = new ReadVideoController();
        String path = ResourceUtils.getURL("classpath:").getPath() + "static";
        System.out.println("666666666666666666666666666666【" + path + "】666666666666666666666666666666】");
        fileUrl = path + fileUrl;
        String videoLength = r.ReadVideoTime(fileUrl);//视频时长
    
        System.out.println("===========================视频时长:" + videoLength + "===========================");
        return videoLength;
    }
    
    /**
     * 视频时长
     *
     * @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(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;
    }
    

    }

  • 相关阅读:
    【阿里云产品评测】装甲兵在云路上!
    区间交集问题
    信封嵌套问题
    twoSum问题的核心思想
    队列实现栈|栈实现队列
    设计Twitter 时间线
    如何使用单调栈解题
    二叉堆详解实现优先级队列
    git/SQL/正则表达式的在线练习网站
    我的 Redis 被入侵了
  • 原文地址:https://www.cnblogs.com/zhuchaoyang123/p/12925278.html
Copyright © 2011-2022 走看看