Java使用ffmpeg将视频转为Mp4格式

布满荆棘的人生 2024-04-07 13:37 215阅读 0赞

不支持Dav格式的视频!!!

我测试过 1分钟的DAV视频 转成 MP4就十几秒 不知道问题出在哪里。

  1. /**
  2. *
  3. * @param inputPath 视频的地址
  4. * @param outputPath 视频转完格式存放地址
  5. * @param newFileName 新生成的视频名称
  6. */
  7. public boolean davToMP4Video(String inputPath,String outputPath,String newFileName){
  8. //ffmpeg软件地址
  9. String ffmpegPath = LibtraryUtil.getffmpeg();
  10. if (!checkfile(inputPath)) {
  11. logger.error(inputPath + " is not file");
  12. return false;
  13. }
  14. String video = "";
  15. String imgUrl = "";
  16. String oldVideoUrl = "";
  17. if (StringUtil.isNotBlank(inputPath)) {
  18. imgUrl = inputPath;
  19. video = imgUrl.replaceAll("\\\\","/");
  20. oldVideoUrl = video.substring(0,video.lastIndexOf("/")+1);
  21. video = video.substring(video.lastIndexOf("/")+1);
  22. }else {
  23. logger.error("inputPath is null :" + inputPath);
  24. return false;
  25. }
  26. String oldFileName = video;
  27. // int type = checkContentType(inputPath);
  28. boolean status = false;
  29. logger.info("直接转成mp4格式");
  30. status = processMp4(inputPath,inputPath,ffmpegPath,outputPath,newFileName);// 直接转成mp4格式
  31. if (status) {
  32. File folder = new File(oldVideoUrl);
  33. File[] files = folder.listFiles();
  34. for (File file : files) {
  35. if (file.getName().equals(oldFileName)){
  36. file.delete();
  37. }
  38. }
  39. logger.info("视频转MP4成功!");
  40. return true;
  41. }else {
  42. logger.error("视频转换失败,重试!");
  43. status = processMp4(inputPath,inputPath,ffmpegPath,outputPath,newFileName);// 直接转成mp4格式
  44. if (status) {
  45. File folder = new File(oldVideoUrl);
  46. File[] files = folder.listFiles();
  47. for (File file : files) {
  48. if (file.getName().equals(oldFileName)){
  49. file.delete();
  50. }
  51. }
  52. logger.info("视频转MP4成功!");
  53. return true;
  54. }
  55. return false;
  56. }
  57. }
  58. private static boolean checkfile(String path) {
  59. File file = new File(path);
  60. if (!file.isFile()) {
  61. logger.error(path+"不是文件夹!");
  62. return false;
  63. }
  64. return true;
  65. }
  66. private static boolean processMp4(String inputPath,String oldfilepath,String ffmpegPath,String outputPath,String fileName) {
  67. if (!checkfile(inputPath)) {
  68. logger.error(oldfilepath + " is not file");
  69. return false;
  70. }
  71. List<String> command = new ArrayList<String>();
  72. command.add(ffmpegPath + "ffmpeg");
  73. command.add("-i");
  74. command.add(oldfilepath);
  75. command.add("-c:v");
  76. command.add("libx264");
  77. command.add("-mbd");
  78. command.add("0");
  79. command.add("-c:a");
  80. command.add("aac");
  81. command.add("-strict");
  82. command.add("-2");
  83. command.add("-pix_fmt");
  84. command.add("yuv420p");
  85. command.add("-movflags");
  86. command.add("faststart");
  87. command.add(outputPath +fileName+ ".mp4");
  88. try {
  89. // 方案1
  90. // Process videoProcess = Runtime.getRuntime().exec(ffmpegPath + "ffmpeg -i " + oldfilepath
  91. // + " -ab 56 -ar 22050 -qscale 8 -r 15 -s 600x500 "
  92. // + outputPath + "a.flv");
  93. // 方案2
  94. Process videoProcess = new ProcessBuilder(command).redirectErrorStream(true).start();
  95. new PrintStream(videoProcess.getErrorStream()).start();
  96. new PrintStream(videoProcess.getInputStream()).start();
  97. videoProcess.waitFor();
  98. return true;
  99. } catch (Exception e) {
  100. e.printStackTrace();
  101. return false;
  102. }
  103. }
  104. }
  105. class PrintStream extends Thread {
  106. java.io.InputStream __is = null;
  107. public PrintStream(java.io.InputStream is) {
  108. __is = is;
  109. }
  110. @Override
  111. public void run() {
  112. try {
  113. while (this != null) {
  114. int _ch = __is.read();
  115. if (_ch != -1) {
  116. // System.out.print((char) _ch);
  117. } else {
  118. break;
  119. }
  120. }
  121. } catch (Exception e) {
  122. e.printStackTrace();
  123. }
  124. }

下面这个方法我没用到 担心你们也有需要我就贴出来吧

  1. private static int checkContentType(String inputPath) {
  2. String type = inputPath.substring(inputPath.lastIndexOf(".") + 1, inputPath.length())
  3. .toLowerCase();
  4. // ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
  5. if (type.equals("avi")) {
  6. return 0;
  7. } else if (type.equals("mpg")) {
  8. return 0;
  9. } else if (type.equals("wmv")) {
  10. return 0;
  11. } else if (type.equals("3gp")) {
  12. return 0;
  13. } else if (type.equals("mov")) {
  14. return 0;
  15. } else if (type.equals("mp4")) {
  16. return 0;
  17. } else if (type.equals("asf")) {
  18. return 0;
  19. } else if (type.equals("asx")) {
  20. return 0;
  21. } else if (type.equals("flv")) {
  22. return 0;
  23. }
  24. // 对ffmpeg无法解析的文件格式(wmv9,rm,rmvb等),
  25. // 可以先用别的工具(mencoder)转换为avi(ffmpeg能解析的)格式.
  26. else if (type.equals("wmv9")) {
  27. return 1;
  28. } else if (type.equals("rm")) {
  29. return 1;
  30. } else if (type.equals("rmvb")) {
  31. return 1;
  32. }
  33. return 9;
  34. }

发表评论

表情:
评论列表 (有 0 条评论,215人围观)

还没有评论,来说两句吧...

相关阅读