java视频格式转换
发布时间:2020-12-15 06:42:39 所属栏目:Java 来源:网络整理
导读:项目中需要对各种视频格式转码为mp4格式,试了好多办法,最后使用 ffmpeg 工具完美转码,ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等) 链接:https://pan.baidu.com/s/1qDcHqCLa3eKgJX2gGtsoNg 提取码:4bdc? package com.dragon
项目中需要对各种视频格式转码为mp4格式,试了好多办法,最后使用ffmpeg 工具完美转码,ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等) 链接:https://pan.baidu.com/s/1qDcHqCLa3eKgJX2gGtsoNg package com.dragonsoft.slxf.utils; import cn.hutool.system.OsInfo; org.slf4j.Logger; org.slf4j.LoggerFactory; java.io.BufferedReader; java.io.File; java.io.InputStream; java.io.InputStreamReader; java.util.ArrayList; java.util.List; public class ConvertVideoUtils { private static Logger logger = LoggerFactory.getLogger(ConvertVideoUtils.); private String inputPath; String outputPath; String ffmpegPath; String fileName; public ConvertVideoUtils(String inputPath,String outputPath,String ffmpegPath,String fileName) { this.inputPath = inputPath; this.outputPath = outputPath; this.ffmpegPath = ffmpegPath; this.fileName = fileName; } Boolean setVoidInfos() { if (!checkfile(inputPath)) { logger.info(inputPath + " is not file"); return false; } if (process(inputPath,ffmpegPath,outputPath,fileName)) { logger.info("ok"true; } static boolean process(String inputPath,1)">int type = checkContentType(inputPath); boolean status = ; logger.info("直接转成mp4格式"); status = processMp4(inputPath,fileName);// 直接转成mp4格式 return status; } 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"if (type.equals("wmv"if (type.equals("3gp"if (type.equals("mov"if (type.equals("mp4"if (type.equals("asf"if (type.equals("asx"if (type.equals("flv" 对ffmpeg无法解析的文件格式(wmv9,rm,rmvb等), 可以先用别的工具(mencoder)转换为avi(ffmpeg能解析的)格式. if (type.equals("wmv9"return 1if (type.equals("rm"if (type.equals("rmvb"return 9; } checkfile(String inputPath) { File file = new File(inputPath); file.isFile()) { processMp4(String oldfilepath,1)">checkfile(oldfilepath)) { logger.info(oldfilepath + " is not file"; } 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"); try { (OSinfo.isWindows()) { Process videoProcess = new ProcessBuilder(command).redirectErrorStream().start(); PrintStream(videoProcess.getErrorStream()).start(); PrintStream(videoProcess.getInputStream()).start(); videoProcess.waitFor(); } else { logger.info("linux开始"); StringBuilder test = StringBuilder(); for (String s : command) test.append(s).append(" "); logger.info(test.toString()); 执行命令 Process p = Runtime.getRuntime().exec(test.toString()); 取得命令结果的输出流 InputStream fis = p.getInputStream(); 用一个读输出流类去读 InputStreamReader isr = InputStreamReader(fis); 用缓冲器读行 BufferedReader br = BufferedReader(isr); String line = null; 直到读完为止 while ((line = br.readLine()) != ) { logger.info("视频转换:{}"catch (Exception e) { e.printStackTrace(); ; } } void main(String[] args) { String ffmpegPath = "D:/upload/"; String outputPath = "D:/upload/ffmpeg/ffmpeg.exe"; String inputPath = "D:/upload/8ab0c280f0f149c78c3b205d905898c7@222221.avi"; String fileName = "22222"; ConvertVideoUtils convertVideoUtils = ConvertVideoUtils(inputPath,fileName); convertVideoUtils.setVoidInfos(); } } ? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |