当前位置: 首页 > news >正文

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

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

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

package com.xkj.utils;import lombok.extern.slf4j.Slf4j;import java.io.*;
import java.util.ArrayList;
import java.util.List;@Slf4j
public class ConvertVideoUtils {//需要转码的视频的全地址private String inputPath;//转码后文件的目录private String outputPath;//ffmpeg文件的路径,linux下无需配置private String ffmpegPath;//转码后文件的名称private String fileName;public ConvertVideoUtils(String inputPath, String outputPath, String ffmpegPath, String fileName) {log.info("开始转码.....");log.info("inputPath={}", inputPath);log.info("outputPath={}", outputPath);log.info("fileName={}", fileName);log.info("ffmpegPath={}", ffmpegPath);this.inputPath = inputPath;this.outputPath = outputPath;this.ffmpegPath = ffmpegPath;this.fileName = fileName;}public static boolean process(String inputPath, String ffmpegPath, String outputPath, String fileName) {int type = checkContentType(inputPath);boolean status = false;log.info("直接转成mp4格式");status = processMp4(inputPath, ffmpegPath, outputPath, fileName);// 直接转成mp4格式return status;}private static int checkContentType(String inputPath) {String type = inputPath.substring(inputPath.lastIndexOf(".") + 1, inputPath.length()).toLowerCase();// ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)if (type.equals("avi")) {return 0;} else if (type.equals("mpg")) {return 0;} else if (type.equals("wmv")) {return 0;} else if (type.equals("3gp")) {return 0;} else if (type.equals("mov")) {return 0;} else if (type.equals("mp4")) {return 0;} else if (type.equals("asf")) {return 0;} else if (type.equals("asx")) {return 0;} else if (type.equals("flv")) {return 0;}// 对ffmpeg无法解析的文件格式(wmv9,rm,rmvb等),// 可以先用别的工具(mencoder)转换为avi(ffmpeg能解析的)格式.else if (type.equals("wmv9")) {return 1;} else if (type.equals("rm")) {return 1;} else if (type.equals("rmvb")) {return 1;}return 9;}private static boolean checkfile(String inputPath) {File file = new File(inputPath);if (!file.isFile()) {return false;}return true;}private static boolean processMp4(String oldfilepath, String ffmpegPath, String outputPath, String fileName) {if (!checkfile(oldfilepath)) {log.info(oldfilepath + " is not file");return false;}String systemName = getSystemName();if (systemName.contains("windows")) { //windows系统ffmpegPath = ffmpegPath + "ffmpeg.exe";} else {ffmpegPath = ffmpegPath + "ffmpeg";}List<String> command = new ArrayList<>();command.add(ffmpegPath);command.add("-i");command.add(oldfilepath);command.add("-c:v");command.add("libx264");command.add("-mbd");command.add("0");command.add("-c:a");command.add("aac");command.add("-strict");command.add("-2");command.add("-pix_fmt");command.add("yuv420p");command.add("-movflags");command.add("faststart");command.add(outputPath + fileName + ".mp4");log.info("生成的command={}", command);try {if (systemName.contains("windows")) { //windows系统Process videoProcess = new ProcessBuilder(command).redirectErrorStream(true).start();threadExec(videoProcess.getErrorStream());threadExec(videoProcess.getInputStream());videoProcess.waitFor();} else {log.info("linux开始");StringBuilder test = new StringBuilder();for (String s : command) test.append(s).append(" ");log.info(test.toString());// 执行命令Process p = Runtime.getRuntime().exec(test.toString());// 取得命令结果的输出流InputStream fis = p.getInputStream();// 用一个读输出流类去读InputStreamReader isr = new InputStreamReader(fis);// 用缓冲器读行BufferedReader br = new BufferedReader(isr);String line = null;// 直到读完为止while ((line = br.readLine()) != null) {log.info("视频转换:{}", line);}}return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** 获取系统平台名称:windows、linux...*/private static String getSystemName() {return System.getProperty("os.name").toLowerCase();}/*** 分线程执行** @param input 输入流*/private static void threadExec(final InputStream input) {new Thread(() -> {String line;try (InputStreamReader inputStreamReader = new InputStreamReader(input);BufferedReader bf = new BufferedReader(inputStreamReader)) {while (null != (line = bf.readLine())) {log.info(line);}} catch (IOException e) {e.printStackTrace();}}).start();}public Boolean setVoidInfos() {if (!checkfile(inputPath)) {log.info(inputPath + " is not file");return false;}if (process(inputPath, ffmpegPath, outputPath, fileName)) {log.info("ok");return true;}return false;}
}

相关文章:

  • mac安装高版本git(更新git)
  • 后端常见问题解答-位运算实际场景讲解
  • 【odoo | SQL】odoo使用sql语句操作数据库
  • 工具:安装R语言的R包的各种方法
  • 大腾智能正式入驻华为云
  • 未来已来:低代码平台如何重塑企业数字化策略?
  • 你知道花洒其实起源于中国古代吗?
  • 【我是产品经理_注册安全分析报告】
  • 制作翻页电子版画册攻略:轻松掌握数字创作技巧
  • Mysql开启查询日志(General Log)
  • docker环境中配置phpstorm php xdebug调试工具
  • 计算子网掩码
  • 多种传感器在钢铁工业安全风险监测预警中的应用
  • 硕士毕业论文《基于磁纹理的磁化动力学研究》
  • “探索机器学习的多面世界:从理论到应用与未来展望“
  • [deviceone开发]-do_Webview的基本示例
  • 〔开发系列〕一次关于小程序开发的深度总结
  • Angular 2 DI - IoC DI - 1
  • co.js - 让异步代码同步化
  • ES6 学习笔记(一)let,const和解构赋值
  • ES学习笔记(12)--Symbol
  • gops —— Go 程序诊断分析工具
  • php中curl和soap方式请求服务超时问题
  • Python_OOP
  • ReactNativeweexDeviceOne对比
  • 不发不行!Netty集成文字图片聊天室外加TCP/IP软硬件通信
  • 程序员最讨厌的9句话,你可有补充?
  • 订阅Forge Viewer所有的事件
  • 解析带emoji和链接的聊天系统消息
  • 使用前端开发工具包WijmoJS - 创建自定义DropDownTree控件(包含源代码)
  • 微信如何实现自动跳转到用其他浏览器打开指定页面下载APP
  • 深度学习之轻量级神经网络在TWS蓝牙音频处理器上的部署
  • #{} 和 ${}区别
  • #includecmath
  • #数据结构 笔记一
  • (10)STL算法之搜索(二) 二分查找
  • (12)Hive调优——count distinct去重优化
  • (二)Kafka离线安装 - Zookeeper下载及安装
  • (力扣)1314.矩阵区域和
  • (免费分享)基于springboot,vue疗养中心管理系统
  • (算法)求1到1亿间的质数或素数
  • (一)pytest自动化测试框架之生成测试报告(mac系统)
  • (转)人的集合论——移山之道
  • .NET 8 中引入新的 IHostedLifecycleService 接口 实现定时任务
  • .net core Redis 使用有序集合实现延迟队列
  • .NET Core中的时区转换问题
  • .NET 中各种混淆(Obfuscation)的含义、原理、实际效果和不同级别的差异(使用 SmartAssembly)
  • .Net+SQL Server企业应用性能优化笔记4——精确查找瓶颈
  • .NET和.COM和.CN域名区别
  • .NET中使用Redis (二)
  • .php结尾的域名,【php】php正则截取url中域名后的内容
  • .vimrc 配置项
  • .vue文件怎么使用_vue调试工具vue-devtools的安装
  • //解决validator验证插件多个name相同只验证第一的问题
  • /usr/bin/env: node: No such file or directory