VideoHelp.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package com.qxgmat.help;
  2. import com.nuliji.tools.Tools;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.beans.factory.annotation.Value;
  6. import org.springframework.stereotype.Service;
  7. import java.io.BufferedReader;
  8. import java.io.File;
  9. import java.io.InputStreamReader;
  10. import java.util.List;
  11. import java.util.regex.Matcher;
  12. import java.util.regex.Pattern;
  13. @Service
  14. public class VideoHelp {
  15. private static final Logger logger = LoggerFactory.getLogger(VideoHelp.class);
  16. @Value("${video.ffmpeg}")
  17. private String ffmpegPath;
  18. @Value("${upload.local_path}")
  19. private String localPath;
  20. @Value("${upload.web_url}")
  21. private String webUrl;
  22. @Value("${upload.hls_path}")
  23. private String hlsPath;
  24. @Value("${upload.hls_url}")
  25. private String hlsUrl;
  26. public String getHLS(String videoUrl){
  27. return videoUrl.replace(webUrl, hlsUrl+"/"+"video.m3u8");
  28. }
  29. public void generateHLS(String videoPath){
  30. File file = new File(videoPath);
  31. String fileName = file.getName();
  32. File dir = new File(hlsPath);
  33. if (!dir.exists()) {
  34. dir.mkdirs();
  35. }
  36. File dest = new File( dir.getAbsolutePath() + File.separator+fileName+File.separator+"video.m3u8");
  37. File dirs = new File(dest.getAbsolutePath());
  38. if (!dirs.exists()){
  39. dirs.mkdirs();
  40. }
  41. List<String> commands = new java.util.ArrayList<String>();
  42. commands.add(ffmpegPath);
  43. commands.add("-i");
  44. commands.add(videoPath);
  45. commands.add("-c:v libx264 -c:a aac -strict -2 -f hls "+dest);
  46. try {
  47. ProcessBuilder builder = new ProcessBuilder();
  48. builder.command(commands);
  49. final Process p = builder.start();
  50. //从输入流中读取视频信息
  51. BufferedReader br = new BufferedReader(new InputStreamReader(p.getErrorStream()));
  52. StringBuffer sb = new StringBuffer();
  53. String line = "";
  54. while ((line = br.readLine()) != null) {
  55. sb.append(line);
  56. }
  57. br.close();
  58. logger.debug("videoPath: {}", videoPath);
  59. logger.debug("ffmpeg: {}", sb.toString());
  60. } catch (Exception e) {
  61. e.printStackTrace();
  62. }
  63. }
  64. public int getVideoTime(String videoPath) {
  65. List<String> commands = new java.util.ArrayList<String>();
  66. commands.add(ffmpegPath);
  67. commands.add("-i");
  68. commands.add(videoPath);
  69. try {
  70. ProcessBuilder builder = new ProcessBuilder();
  71. builder.command(commands);
  72. final Process p = builder.start();
  73. //从输入流中读取视频信息
  74. BufferedReader br = new BufferedReader(new InputStreamReader(p.getErrorStream()));
  75. StringBuffer sb = new StringBuffer();
  76. String line = "";
  77. while ((line = br.readLine()) != null) {
  78. sb.append(line);
  79. }
  80. br.close();
  81. logger.debug("videoPath: {}", videoPath);
  82. logger.debug("ffmpeg: {}", sb.toString());
  83. //从视频信息中解析时长
  84. String regexDuration = "Duration: (.*?), start: (.*?), bitrate: (\\d*) kb\\/s";
  85. Pattern pattern = Pattern.compile(regexDuration);
  86. Matcher m = pattern.matcher(sb.toString());
  87. if (m.find()) {
  88. int time = getTimelen(m.group(1));
  89. return time;
  90. }
  91. } catch (Exception e) {
  92. e.printStackTrace();
  93. }
  94. return 0;
  95. }
  96. private static int getTimelen(String timelen){
  97. int min=0;
  98. String strs[] = timelen.split(":");
  99. if (strs[0].compareTo("0") > 0) {
  100. min+=Integer.valueOf(strs[0])*60*60;//秒
  101. }
  102. if(strs[1].compareTo("0")>0){
  103. min+=Integer.valueOf(strs[1])*60;
  104. }
  105. if(strs[2].compareTo("0")>0){
  106. min+=Math.round(Float.valueOf(strs[2]));
  107. }
  108. return min;
  109. }
  110. }