引入包
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();
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;
}
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;
}
转换
@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) {
e2.printStackTrace();
logger.info("读取转换工具路径失败");
}
try {
path = URLDecoder.decode(path, "UTF-8");
transformPath = URLDecoder.decode(transformPath, "UTF-8");
} catch (UnsupportedEncodingException e1) {
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);
logger.info("视频转码开始...");
Process process = builder.start();
BufferedReader br = new BufferedReader(new InputStreamReader(
process.getInputStream()));
String line = null;
while ((line = br.readLine()) != null) {
}
logger.info("视频转码完成...");
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程序+测试视频