关于工具类中用到的一些自定义对象和方法就不贴出来了,相信大家看方法名或者对象名就能明白。
public class FFmpegUtils {
private static String ffmpegEXE = "ffmpeg";
static {
String expression = ConfigUtils.getType("ffmpeg.expression");
ffmpegEXE = StringUtils.isBlank(expression) ? "ffmpeg" : expression;
}
public static boolean screenCut(FFmpegParameter record, String outputPath) throws Exception {
Process process = null;
try {
List<String> commands = new java.util.ArrayList<String>();
commands.add(ffmpegEXE);
commands.add("-y");
commands.add("-ss");
commands.add(record.getCutTime());
commands.add("-i");
commands.add(record.getFilePath());
commands.add("-f");
commands.add("image2");
commands.add(outputPath);
process = new ProcessBuilder(commands).redirectErrorStream(true).start();
new PrintStreamUtils(process.getErrorStream()).start();
new PrintStreamUtils(process.getInputStream()).start();
process.waitFor();
return validateInvokingFFmpeg(outputPath);
} finally {
if (null != process) {
process.destroy();
}
}
}
public static boolean processMedia(FFmpegParameter record, String targetPath) throws Exception {
Process process = null;
try {
if (checkVideo(record.getTargetType(), record.getTargetEncoding(), record.getBitrate(), record.getFramerate(), record.getFilePath())) {
// 视频本身符合要求
FileUtils.copyFile(new File(record.getFilePath()), new File(targetPath));
return true;
}
List<String> commands = new ArrayList<String>();
commands.add(ffmpegEXE);
commands.add("-y");
commands.add("-i");
commands.add(record.getFilePath());
if (StringUtils.isNotBlank(record.getBitrate())) {
commands.add("-b");
commands.add(record.getBitrate() + "k");
}
if (StringUtils.isNotBlank(record.getTargetEncoding())) {
commands.add("-c:v");
commands.add(record.getTargetEncoding());
}
if (true == record.isAudio()) {
commands.add("-c:a");
commands.add("aac");
} else {
commands.add("-an");
}
if (EncodingConstant.GIF.equals(record.getSourceType())) {
String[] resolution = correctResolution(record.getFilePath());
commands.add("-s");
commands.add(resolution[0] + "x" + resolution[1]);
}
if (StringUtils.isNotBlank(record.getFramerate())) {
commands.add("-r");
commands.add(record.getFramerate());
}
commands.add(targetPath);
process = new ProcessBuilder(commands).redirectErrorStream(true).start();
new PrintStreamUtils(process.getErrorStream()).start();
new PrintStreamUtils(process.getInputStream()).start();
process.waitFor();
return validateInvokingFFmpeg(targetPath);
} finally {
if (null != process) {
process.destroy();
}
}
}
public static String getDuration(String filePath) throws Exception {
Long time = null;
String info = getMediaInfo(filePath);
//从视频信息中解析时长
String regexDuration = "Duration: (.*?), start";
Pattern pattern = Pattern.compile(regexDuration);
Matcher m = pattern.matcher(info);
if (m.find()) {
time = getTimelen(m.group(1));
}
return String.valueOf(time * 1000);
}
public static String getEncoding(String filePath) throws Exception {
String info = getMediaInfo(filePath);
String regexVideo = "Video: (.*?), (.*?), (.*?), (.*?), (.*?), (.*?)[,\\s]";
Pattern pattern = Pattern.compile(regexVideo);
Matcher m = pattern.matcher(info);
String encoding = null;
if (m.find()) {
//编码格式:m.group(1),视频格式:m.group(2),分辨率:m.group(3),(码率:m.group(4),fps:m.group(5)||码率:m.group(5),fps:m.group(6))
encoding = m.group(1).toLowerCase().replace(" ", "");
}
return encoding;
}
public static String getResolution(String filePath) throws Exception {
String resolution = null;
String info = getMediaInfo(filePath);
String regexVideo = "Video: (.*?), (.*?), (.*?), (.*?), (.*?), (.*?)[,\\s]";
Pattern pattern = Pattern.compile(regexVideo);
Matcher m = pattern.matcher(info);
if (m.find()) {
//编码格式:m.group(1),视频格式:m.group(2),分辨率:m.group(3),(码率:m.group(4),fps:m.group(5)||码率:m.group(5),fps:m.group(6))
if (m.group(3).toLowerCase().contains("x")) {
resolution = m.group(3);