java使用ffmpeg将视频转换为另外一个格式
此文是基于网上众多ffmpeg资料写的,其中有稍微的改动,比如MP4格式一定要是使用libx264与baseline的,获取的路径处理,(linux与Windows不同)package com.xincheng.utils;import java.io.BufferedReader;import java.io.File;import java.io.IOException
此文是基于网上众多ffmpeg资料写的,其中有稍微的改动,比如MP4格式一定要是使用libx264与baseline的,获取的路径处理,(linux与Windows不同)
package com.xincheng.utils;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.xincheng.config.SystemConfig;
import com.xincheng.log.LoggerUlog;
public class videoSwitchUtils {
private static Logger logger = LoggerFactory.getLogger(videoSwitchUtils.class);
public static String process(String resourcePath,String uploadList) {
int type = checkContentType(resourcePath);
String status = null;
if (type == 0) {
status = processFLV(resourcePath,uploadList);// 直接将文件转为mp4文件
} else if (type == 1) {
status = processFLV(resourcePath,uploadList);// 将rmvb转为mp4
}
return status;
}
private static int checkContentType(String resourcePath) {
String type = resourcePath.substring(resourcePath.lastIndexOf(".") + 1, resourcePath.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;
} else if (type.equals("mpeg")) {
return 0;
} else if (type.equals("mpe")) {
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 path) {
File file = new File(path);
if (!file.isFile()) {
return false;
}
return true;
}
// ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
@SuppressWarnings("unchecked")
private static String processFLV(String resourcePath,String uploadList) {
if (!checkfile(resourcePath)) {
System.out.println(resourcePath + " is not file");
LoggerUlog.error(resourcePath+"无法解析", "1I4999");
return null;
}
// 文件命名
Calendar c = Calendar.getInstance();
String savename = DateUtils.transitionDate();//String.valueOf(c.getTimeInMillis()) + Math.round(Math.random() * 100000);
List commend = new ArrayList();
String oldName = "";
String transformPath = "";
// resourcePath = resourcePath.substring(1);
if(resourcePath.contains("//")){
resourcePath = resourcePath.replaceAll("//", "/");
}
if(resourcePath.contains("/")){
oldName = resourcePath.substring(resourcePath.lastIndexOf("/")+1).substring(0,resourcePath.substring(resourcePath.lastIndexOf("/")+1).indexOf("."));
transformPath = resourcePath.substring(0,resourcePath.lastIndexOf("/")+1);
}else{
oldName = resourcePath.substring(resourcePath.lastIndexOf("\\")+1).substring(0,resourcePath.substring(resourcePath.lastIndexOf("\\")+1).indexOf("."));
transformPath = resourcePath.substring(0,resourcePath.lastIndexOf("\\")+1);
}
// String path = SystemConfig.class.getClassLoader().getResource("").getPath() + "ffmpeg/ffmpeg.exe";
logger.info("读取转换工具路径");
String path = "";
try {
path = SystemConfig.getPara("TransformTool");
} catch (Exception e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
logger.info("读取转换工具路径失败");
}
// String path = "/mnt/ffmpeg/ffmpeg_3_3_4/ffmpeg";
try {
// 路径解码,主要解决空格%20
path = URLDecoder.decode(path, "UTF-8");
transformPath = URLDecoder.decode(transformPath, "UTF-8");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
logger.info("当前路径为:----------"+path);
commend.add(path);
commend.add("-i");
commend.add(resourcePath);
commend.add("-f");
commend.add("mp4");
commend.add("-acodec");
commend.add("aac");
/* commend.add("-b");
commend.add("512k");
commend.add("-ab");
commend.add("512k");*/
commend.add("-vcodec");
commend.add("libx264");
commend.add("-profile:v");
commend.add("baseline");
commend.add(transformPath + oldName+"-"+savename + ".mp4");
try {
ProcessBuilder builder = new ProcessBuilder();
builder.command(commend);
builder.redirectErrorStream(true);
long start = System.currentTimeMillis(); // 记录起始时间
logger.info("视频转码开始...");
// builder.start();
Process process = builder.start();
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while((line = br.readLine()) != null){
logger.info("视频转换中:"+line);
}
/* InputStream in = process.getInputStream();
byte[] re = new byte[1024];
logger.info("正在进行转码,请稍候");
while (in.read(re) != -1) {
System.out.print(".");
}
System.out.println("");
in.close();*/
logger.info("视频转码完成...");
long end = System.currentTimeMillis();//取结束时间
logger.info("转码一共运行" + (end - start) + "毫秒");
logger.info("转换后视频存放路径transformPath:"+transformPath + oldName+"-"+savename + ".mp4");
return transformPath + oldName+"-"+savename + ".mp4";
} catch (Exception e) {
e.printStackTrace();
logger.info("视频转码失败!");
LoggerUlog.error("视频转码失败!"+resourcePath, "1I4999");
return null;
}
}
// @SuppressWarnings("static-access")
public static void main(String[] args) {
// videoSwitchUtils vs = new videoSwitchUtils();
// String resourcePath = "/D:/zhuoqin/double/apache-tomcat-7.0.52/webapps/videoQuartz/WEB-INF/classes/temp/20171116/TransferList/18/21_20171108181122.3gp";
// String uploadList = "D:/zhuoqin/double/test/";
// String path = vs.process(resourcePath,uploadList);
// System.out.println(path);
// String transformPath = resourcePath.substring(0,resourcePath.lastIndexOf("/")+1);
// System.out.println(transformPath);
// String n = "/D:/zhuoqin/double/apache-to".substring(1);
// System.out.println(n);
}
}
更多推荐
所有评论(0)