读取视频时长

1.下载jar

http://www.sauronsoftware.it/projects/jave/download.php

2.将下载的jar加入到项目中

 3.代码

   @RequestMapping(value = "uploadBatchFile",method = RequestMethod.POST)
    @ResponseBody
    public Object uploadBatchFile(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request) throws IOException, InterruptedException {
        HttpSession session = request.getSession();
        // "/upload"是你自己定义的上传目录
        // linux 下的路径
//        String temp = session.getServletContext().getRealPath("/upload");
        // window 下的路径
        String temp = session.getServletContext().getRealPath("\upload");
        File tempDir = new File(temp);
        if (!tempDir.exists()){
            tempDir.mkdir();
        }
        File copyFilePath = new File(tempDir,file.getOriginalFilename());
        file.transferTo(copyFilePath);
        //文件存储路径
        // linux -- /data/wwwroot/video.xiaoxiao.cool/upload/f20cd0d0a6154f2dce2c7059a8a62167.mp4
        // window -- E:\IdeaProjects\video_film\target\demo\upload\f20cd0d0a6154f2dce2c7059a8a62167.mp4
        // linux
//        String tempFilePath = temp + "/" + file.getOriginalFilename();
        // window
        String tempFilePath = temp + "\" + file.getOriginalFilename();
        long size = getSize(tempFilePath);
        System.err.println("时长 : " + size);
        //获取完时长,如果使用的是oss存储,则需要将服务器上的文件删除,减少存储空间
        copyFilePath.delete();
        Map map = Maps.newHashMap();
        map.put("tempDir",temp);
        map.put("tempFilePath",tempFilePath);
        map.put("size",size);
        return JsonData.success(map) ;
    }

  

  
  //获取时长的方法 单位秒
  //url - 视频的绝对路径
  long
getSize(String url){ File source = new File(url); Encoder encoder = new Encoder(); long sum =0; try { MultimediaInfo m = encoder.getInfo(source); sum = m.getDuration()/1000; } catch (Exception e) { e.printStackTrace(); } System.out.println(sum); return sum; }
原文地址:https://www.cnblogs.com/SeaWxx/p/12985638.html