Java使用ffmpeg将视频转为Mp4格式
不支持Dav格式的视频!!!
我测试过 1分钟的DAV视频 转成 MP4就十几秒 不知道问题出在哪里。
/**
*
* @param inputPath 视频的地址
* @param outputPath 视频转完格式存放地址
* @param newFileName 新生成的视频名称
*/
public boolean davToMP4Video(String inputPath,String outputPath,String newFileName){
//ffmpeg软件地址
String ffmpegPath = LibtraryUtil.getffmpeg();
if (!checkfile(inputPath)) {
logger.error(inputPath + " is not file");
return false;
}
String video = "";
String imgUrl = "";
String oldVideoUrl = "";
if (StringUtil.isNotBlank(inputPath)) {
imgUrl = inputPath;
video = imgUrl.replaceAll("\\\\","/");
oldVideoUrl = video.substring(0,video.lastIndexOf("/")+1);
video = video.substring(video.lastIndexOf("/")+1);
}else {
logger.error("inputPath is null :" + inputPath);
return false;
}
String oldFileName = video;
// int type = checkContentType(inputPath);
boolean status = false;
logger.info("直接转成mp4格式");
status = processMp4(inputPath,inputPath,ffmpegPath,outputPath,newFileName);// 直接转成mp4格式
if (status) {
File folder = new File(oldVideoUrl);
File[] files = folder.listFiles();
for (File file : files) {
if (file.getName().equals(oldFileName)){
file.delete();
}
}
logger.info("视频转MP4成功!");
return true;
}else {
logger.error("视频转换失败,重试!");
status = processMp4(inputPath,inputPath,ffmpegPath,outputPath,newFileName);// 直接转成mp4格式
if (status) {
File folder = new File(oldVideoUrl);
File[] files = folder.listFiles();
for (File file : files) {
if (file.getName().equals(oldFileName)){
file.delete();
}
}
logger.info("视频转MP4成功!");
return true;
}
return false;
}
}
private static boolean checkfile(String path) {
File file = new File(path);
if (!file.isFile()) {
logger.error(path+"不是文件夹!");
return false;
}
return true;
}
private static boolean processMp4(String inputPath,String oldfilepath,String ffmpegPath,String outputPath,String fileName) {
if (!checkfile(inputPath)) {
logger.error(oldfilepath + " is not file");
return false;
}
List<String> command = new ArrayList<String>();
command.add(ffmpegPath + "ffmpeg");
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 {
// 方案1
// Process videoProcess = Runtime.getRuntime().exec(ffmpegPath + "ffmpeg -i " + oldfilepath
// + " -ab 56 -ar 22050 -qscale 8 -r 15 -s 600x500 "
// + outputPath + "a.flv");
// 方案2
Process videoProcess = new ProcessBuilder(command).redirectErrorStream(true).start();
new PrintStream(videoProcess.getErrorStream()).start();
new PrintStream(videoProcess.getInputStream()).start();
videoProcess.waitFor();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
class PrintStream extends Thread {
java.io.InputStream __is = null;
public PrintStream(java.io.InputStream is) {
__is = is;
}
@Override
public void run() {
try {
while (this != null) {
int _ch = __is.read();
if (_ch != -1) {
// System.out.print((char) _ch);
} else {
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
下面这个方法我没用到 担心你们也有需要我就贴出来吧
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;
}
还没有评论,来说两句吧...