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 }
其中最重要的代码就是:
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