zoukankan      html  css  js  c++  java
  • java读取视频文件时长

    1.下载jar包:http://www.sauronsoftware.it/projects/jave/index.php

    2.上代码  

     1     @RequestMapping(value = "amendFile.htm", produces = "application/json;charset=UTF-8")
     2     @ResponseBody
     3     public String amendFile(MultipartFile file, HttpServletRequest request,
     4             HttpServletResponse response, String equid,String begintime,String endtime) {
     5         log.info(this.getClass().getSimpleName() + "."
     6                 + Thread.currentThread().getStackTrace()[1].getMethodName()
     7                 + "()----start");
     8         
     9         String path =  request.getSession().getServletContext()
    10                 .getRealPath("/")+"resources/"+equid+"/"+begintime.substring(0,begintime.length()-6)+"/";
    11         // 通过文件ID 获取文件存储路径
    12         if (file.isEmpty()) {
    13             return "empty";
    14         }
    15         Map<String, Object> map = new HashMap<String, Object>();
    16         try {
    17             /* 写入文件 */
    18             File targetFile = new File(path, file.getOriginalFilename());
    19             if (!targetFile.exists()) {
    20                 targetFile.mkdirs();
    21             }
    22             file.transferTo(targetFile); // 转储 
    23             Encoder encoder = new Encoder(); 
    24             // zp add 读取视频时长 2017-07-31
    25             try {
    26                  MultimediaInfo m = encoder.getInfo(targetFile);
    27                  long ls = m.getDuration(); 
    28                  ls = Math.round((double)ls/1000); 
    29                  map.put("timeLength", Math.round(ls));
    30             } catch (Exception e) {
    31                 e.printStackTrace();
    32             } 
    33             // 读取视频时长结束  2017-07-31
    34             response.addHeader("Access-Control-Allow-Origin", "*");
    35             if(targetFile.exists()){
    36                 
    37                 map.put("success", "success");
    38                 ObjectMapper mapper = new ObjectMapper();
    39                 String content = mapper.writeValueAsString(map);
    40                 return content;
    41             }else{
    42                 map.put("error", "error");
    43                 ObjectMapper mapper = new ObjectMapper();
    44                 String content = mapper.writeValueAsString(map);
    45                 return content;
    46             }
    47             
    48         } catch (Exception e) {
    49             e.printStackTrace();
    50         }
    51         log.info(this.getClass().getSimpleName() + "."
    52                 + Thread.currentThread().getStackTrace()[1].getMethodName()
    53                 + "()----end");
    54         return "success";
    55     } 
    View Code

    其中最重要的代码就是:

     1  // zp add 读取视频时长 2017-07-31
     2             try {
     3                  MultimediaInfo m = encoder.getInfo(targetFile);
     4                  long ls = m.getDuration(); 
     5                  ls = Math.round((double)ls/1000); 
     6                  map.put("timeLength", Math.round(ls));
     7             } catch (Exception e) {
     8                 e.printStackTrace();
     9             } 
    10             // 读取视频时长结束  2017-07-31
    View Code
  • 相关阅读:
    要回家了,想到以后..
    面试应注意的问题by JohnPhilips(转自matrix论坛)
    在痛苦中坚持,翻译啊
    数据抓取和分析~~
    不考研,就很闲吗?
    在Swing中使用高级的MVC和POJOs
    测试驱动
    开始找工作了,先记个流水帐
    又是开学的时候了
    开发者想要什么算是我的翻译作品的处女作吧,呵呵,致力于英语学习..
  • 原文地址:https://www.cnblogs.com/pengpengzhang/p/7262652.html
Copyright © 2011-2022 走看看