import java.io.BufferedReader; import java.io.File; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.util.ArrayList; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory;检查视频后缀
public 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) {
if (!checkfile(resourcePath)) {
logger.info(resourcePath + "isnotfile");
return null;
}
List commend = new ArrayList();
String saveName = "";
String transformPath = "";
if (resourcePath.contains("//")) {
resourcePath = resourcePath.replaceAll("//", "/");
}
if (resourcePath.contains("/")) {
saveName = resourcePath.substring(resourcePath.lastIndexOf("/") + 1)
.substring(
0,
resourcePath.substring(
resourcePath.lastIndexOf("/") + 1).indexOf(
"."));
transformPath = resourcePath.substring(0,
resourcePath.lastIndexOf("/") + 1);
} else {
saveName = resourcePath
.substring(resourcePath.lastIndexOf("\") + 1).substring(
0,
resourcePath.substring(
resourcePath.lastIndexOf("\") + 1)
.indexOf("."));
transformPath = resourcePath.substring(0,
resourcePath.lastIndexOf("\") + 1);
}
logger.info("读取转换工具路径");
String path = "";
try {
path = new PropertiesUtil("system.properties").readProperty("ffmpegPath");
} catch (Exception e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
logger.info("读取转换工具路径失败");
}
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("-vcodec");
commend.add("libx264");
commend.add("-profile:v");
commend.add("baseline");
commend.add(transformPath + 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);
}
logger.info("视频转码完成...");
// long end = System.currentTimeMillis();// 取结束时间
// logger.info("转码一共运行" + (end - start) + "毫秒");
logger.info("转换后视频存放路径transformPath:" + transformPath + saveName
+ ".mp4");
return saveName + ".mp4";
} catch (Exception e) {
e.printStackTrace();
logger.info("视频转码失败!");
return null;
}
}
删除源文件
public static boolean deleteFile(String fileName) {
File file = new File(fileName);
// 如果文件路径所对应的文件存在,并且是一个文件,则直接删除
if (file.exists() && file.isFile()) {
if (file.delete()) {
logger.info("删除单个文件" + fileName + "成功!");
return true;
} else {
logger.info("删除单个文件" + fileName + "失败!");
return false;
}
} else {
logger.info("删除单个文件失败:" + fileName + "不存在!");
return false;
}
}
附带ffmpeg 和测试需要使用的各种后缀的视频
ffmpeg程序+测试视频



