• ffmpeg把视频文件转码为MP4格式


    windows系统需要下载ffmpeg软件,并在代码中指定路径

    centos系统需要安装ffmepg是可执行的命令

    1. package com.xkj.utils;
    2. import lombok.extern.slf4j.Slf4j;
    3. import java.io.*;
    4. import java.util.ArrayList;
    5. import java.util.List;
    6. @Slf4j
    7. public class ConvertVideoUtils {
    8. //需要转码的视频的全地址
    9. private String inputPath;
    10. //转码后文件的目录
    11. private String outputPath;
    12. //ffmpeg文件的路径,linux下无需配置
    13. private String ffmpegPath;
    14. //转码后文件的名称
    15. private String fileName;
    16. public ConvertVideoUtils(String inputPath, String outputPath, String ffmpegPath, String fileName) {
    17. log.info("开始转码.....");
    18. log.info("inputPath={}", inputPath);
    19. log.info("outputPath={}", outputPath);
    20. log.info("fileName={}", fileName);
    21. log.info("ffmpegPath={}", ffmpegPath);
    22. this.inputPath = inputPath;
    23. this.outputPath = outputPath;
    24. this.ffmpegPath = ffmpegPath;
    25. this.fileName = fileName;
    26. }
    27. public static boolean process(String inputPath, String ffmpegPath, String outputPath, String fileName) {
    28. int type = checkContentType(inputPath);
    29. boolean status = false;
    30. log.info("直接转成mp4格式");
    31. status = processMp4(inputPath, ffmpegPath, outputPath, fileName);// 直接转成mp4格式
    32. return status;
    33. }
    34. private static int checkContentType(String inputPath) {
    35. String type = inputPath.substring(inputPath.lastIndexOf(".") + 1, inputPath.length())
    36. .toLowerCase();
    37. // ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
    38. if (type.equals("avi")) {
    39. return 0;
    40. } else if (type.equals("mpg")) {
    41. return 0;
    42. } else if (type.equals("wmv")) {
    43. return 0;
    44. } else if (type.equals("3gp")) {
    45. return 0;
    46. } else if (type.equals("mov")) {
    47. return 0;
    48. } else if (type.equals("mp4")) {
    49. return 0;
    50. } else if (type.equals("asf")) {
    51. return 0;
    52. } else if (type.equals("asx")) {
    53. return 0;
    54. } else if (type.equals("flv")) {
    55. return 0;
    56. }
    57. // 对ffmpeg无法解析的文件格式(wmv9,rm,rmvb等),
    58. // 可以先用别的工具(mencoder)转换为avi(ffmpeg能解析的)格式.
    59. else if (type.equals("wmv9")) {
    60. return 1;
    61. } else if (type.equals("rm")) {
    62. return 1;
    63. } else if (type.equals("rmvb")) {
    64. return 1;
    65. }
    66. return 9;
    67. }
    68. private static boolean checkfile(String inputPath) {
    69. File file = new File(inputPath);
    70. if (!file.isFile()) {
    71. return false;
    72. }
    73. return true;
    74. }
    75. private static boolean processMp4(String oldfilepath, String ffmpegPath, String outputPath, String fileName) {
    76. if (!checkfile(oldfilepath)) {
    77. log.info(oldfilepath + " is not file");
    78. return false;
    79. }
    80. String systemName = getSystemName();
    81. if (systemName.contains("windows")) { //windows系统
    82. ffmpegPath = ffmpegPath + "ffmpeg.exe";
    83. } else {
    84. ffmpegPath = ffmpegPath + "ffmpeg";
    85. }
    86. List command = new ArrayList<>();
    87. command.add(ffmpegPath);
    88. command.add("-i");
    89. command.add(oldfilepath);
    90. command.add("-c:v");
    91. command.add("libx264");
    92. command.add("-mbd");
    93. command.add("0");
    94. command.add("-c:a");
    95. command.add("aac");
    96. command.add("-strict");
    97. command.add("-2");
    98. command.add("-pix_fmt");
    99. command.add("yuv420p");
    100. command.add("-movflags");
    101. command.add("faststart");
    102. command.add(outputPath + fileName + ".mp4");
    103. log.info("生成的command={}", command);
    104. try {
    105. if (systemName.contains("windows")) { //windows系统
    106. Process videoProcess = new ProcessBuilder(command).redirectErrorStream(true).start();
    107. threadExec(videoProcess.getErrorStream());
    108. threadExec(videoProcess.getInputStream());
    109. videoProcess.waitFor();
    110. } else {
    111. log.info("linux开始");
    112. StringBuilder test = new StringBuilder();
    113. for (String s : command) test.append(s).append(" ");
    114. log.info(test.toString());
    115. // 执行命令
    116. Process p = Runtime.getRuntime().exec(test.toString());
    117. // 取得命令结果的输出流
    118. InputStream fis = p.getInputStream();
    119. // 用一个读输出流类去读
    120. InputStreamReader isr = new InputStreamReader(fis);
    121. // 用缓冲器读行
    122. BufferedReader br = new BufferedReader(isr);
    123. String line = null;
    124. // 直到读完为止
    125. while ((line = br.readLine()) != null) {
    126. log.info("视频转换:{}", line);
    127. }
    128. }
    129. return true;
    130. } catch (Exception e) {
    131. e.printStackTrace();
    132. return false;
    133. }
    134. }
    135. /**
    136. * 获取系统平台名称:windows、linux...
    137. */
    138. private static String getSystemName() {
    139. return System.getProperty("os.name").toLowerCase();
    140. }
    141. /**
    142. * 分线程执行
    143. *
    144. * @param input 输入流
    145. */
    146. private static void threadExec(final InputStream input) {
    147. new Thread(() -> {
    148. String line;
    149. try (InputStreamReader inputStreamReader = new InputStreamReader(input);
    150. BufferedReader bf = new BufferedReader(inputStreamReader)) {
    151. while (null != (line = bf.readLine())) {
    152. log.info(line);
    153. }
    154. } catch (IOException e) {
    155. e.printStackTrace();
    156. }
    157. }).start();
    158. }
    159. public Boolean setVoidInfos() {
    160. if (!checkfile(inputPath)) {
    161. log.info(inputPath + " is not file");
    162. return false;
    163. }
    164. if (process(inputPath, ffmpegPath, outputPath, fileName)) {
    165. log.info("ok");
    166. return true;
    167. }
    168. return false;
    169. }
    170. }
  • 相关阅读:
    自动驾驶:2022 apollo day 观后感(一)
    lio-sam框架:点云匹配之手写高斯牛顿下降优化求状态量更新
    由投影仪到智能会议平板,经历怎样的发展过程?
    Qt5 Python-docx库的使用,Qt python混合编程,qt 读写word,不依赖office
    HashMap底层数据结构,扩容机制
    【python】屈小原求三峡大学面积(CTGU百年校庆)
    婚纱租赁系统毕业设计,婚纱租赁管理系统设计与实现,论文毕设作品参考
    基于vue+node+MySQL的导航可视化系统webapp设计
    刷题记录----字符串
    css:过渡transition 、转换transform、动画animation
  • 原文地址:https://blog.csdn.net/qq_36352889/article/details/139664753