VideoTool.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package com.iden.common.videotool;
  2. import com.iden.common.util.DateUtils;
  3. import com.iden.common.util.FileUtil;
  4. import lombok.extern.slf4j.Slf4j;
  5. import java.io.BufferedReader;
  6. import java.io.File;
  7. import java.io.InputStreamReader;
  8. import java.util.Date;
  9. import java.util.List;
  10. import java.util.UUID;
  11. import java.util.regex.Matcher;
  12. import java.util.regex.Pattern;
  13. @Slf4j
  14. public class VideoTool {
  15. /**
  16. * 视频分解为图片
  17. * @param ffmpegPath
  18. * @param videoFilePath
  19. * @param saveTmpDir
  20. * @param shootEndTime
  21. * @return
  22. * @throws Exception
  23. */
  24. public static Long fetchAllPic(String ffmpegPath,String videoFilePath, String saveTmpDir,String targetDir, Date shootEndTime) throws Exception {
  25. Long endTime = shootEndTime.getTime() ;
  26. //ms
  27. long duration = getVideoTime(ffmpegPath,videoFilePath) * 1000;
  28. Long beginTime = endTime - duration;
  29. //每秒多少帧
  30. int framRate = 1;
  31. String cmd = ffmpegPath + " -y -i " + videoFilePath + " -r " + framRate + " " + saveTmpDir + "%08d.jpg";
  32. log.info("start fetchAllPic:" + cmd);
  33. Runtime.getRuntime().exec(cmd);
  34. log.info("fetchAllPic end");
  35. moveFile(beginTime,saveTmpDir,targetDir,framRate);
  36. return duration;
  37. }
  38. private static void moveFile(Long beginTime,String saveTmpDir,String targetDir, int framRate){
  39. File saveTmpDirFile = new File(saveTmpDir);
  40. File[] tmpFiles = FileUtil.sortByName(saveTmpDirFile.listFiles());
  41. if (tmpFiles != null && tmpFiles.length > 0) {
  42. for (int i = 0; i< tmpFiles.length; i++){
  43. File tmpFile = tmpFiles[i];
  44. String tmpFileName = tmpFile.getName();
  45. String tmpFileExt = tmpFileName.substring(tmpFileName.lastIndexOf(".") + 1);
  46. long time = beginTime + (i * (1000/framRate));
  47. String tmpFileNameNew = DateUtils.formatToDateStr(new Date(time),"yyyyMMddHHmmss") + "_" + UUID.randomUUID().toString() + "." + tmpFileExt;
  48. File tmpNewFile = new File(targetDir,tmpFileNameNew);
  49. tmpFile.renameTo(tmpNewFile);
  50. }
  51. }
  52. }
  53. /**
  54. * 视频剪辑
  55. * @param ffmpegPath
  56. * @param beginTime
  57. * @param endTime
  58. * @param videoSrcPath
  59. * @param targetPath
  60. */
  61. public static void cutVideo(String ffmpegPath, String beginTime,String endTime,String videoSrcPath,String targetPath) throws Exception {
  62. Long duration = DateUtils.betweenTimeSS(beginTime, endTime); //s
  63. String cmd = ffmpegPath + " -ss " + beginTime + " -i " + videoSrcPath
  64. + " -t " + duration + " -c:v copy -c:a copy " + targetPath;
  65. log.info("start cutVideo :" + cmd);
  66. Runtime.getRuntime().exec(cmd);
  67. log.info("cutVideo end");
  68. }
  69. /**
  70. * 获取视频总时间
  71. * @param ffmpeg_path ffmpeg路径
  72. * @param video_path 视频路径
  73. * @return
  74. */
  75. public static int getVideoTime( String ffmpeg_path,String video_path) {
  76. List<String> commands = new java.util.ArrayList<String>();
  77. commands.add(ffmpeg_path);
  78. commands.add("-i");
  79. commands.add(video_path);
  80. try {
  81. ProcessBuilder builder = new ProcessBuilder();
  82. builder.command(commands);
  83. final Process p = builder.start();
  84. //从输入流中读取视频信息
  85. BufferedReader br = new BufferedReader(new InputStreamReader(p.getErrorStream()));
  86. StringBuffer sb = new StringBuffer();
  87. String line = "";
  88. while ((line = br.readLine()) != null) {
  89. sb.append(line);
  90. }
  91. br.close();
  92. //从视频信息中解析时长
  93. String regexDuration = "Duration: (.*?), start: (.*?), bitrate: (\\d*) kb\\/s";
  94. Pattern pattern = Pattern.compile(regexDuration);
  95. Matcher m = pattern.matcher(sb.toString());
  96. if (m.find()) {
  97. int time = getTimelen(m.group(1));
  98. System.out.println(video_path+",视频时长:"+time+", 开始时间:"+m.group(2)+",比特率:"+m.group(3)+"kb/s");
  99. return time;
  100. }
  101. } catch (Exception e) {
  102. e.printStackTrace();
  103. }
  104. return 0;
  105. }
  106. //格式:"00:00:10.68"
  107. private static int getTimelen(String timelen){
  108. int min=0;
  109. String strs[] = timelen.split(":");
  110. if (strs[0].compareTo("0") > 0) {
  111. min+=Integer.valueOf(strs[0])*60*60;//秒
  112. }
  113. if(strs[1].compareTo("0")>0){
  114. min+=Integer.valueOf(strs[1])*60;
  115. }
  116. if(strs[2].compareTo("0")>0){
  117. min+=Math.round(Float.valueOf(strs[2]));
  118. }
  119. return min;
  120. }
  121. public static void main(String[] args) {
  122. System.out.println(getVideoTime("D:\\program\\ffmpeg\\ffmpeg-4.3.1-2021-01-01-essentials_build\\bin\\ffmpeg.exe","e:/output99.mp4"));
  123. }
  124. }