package com.face.test;
import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
public class FFmpegUtil {
/**
*
* @param ffmpegPath ffmpeg压缩工具路径
* @param faceVideo 上传的视频
* @return
*/
public static byte[] FFmpeg(String ffmpegPath, MultipartFile faceVideo){
byte[] byteVideo = null;
File oldFile = null, newFile = null;
try {
String suffix = "mp4";
String fileName = faceVideo.getOriginalFilename();
int index = fileName.lastIndexOf(".");
if(index > 0) {
suffix = fileName.substring(index);
}
oldFile = File.createTempFile("oldVideo", suffix);
faceVideo.transferTo(oldFile);
if (!oldFile.isFile()) {
return byteVideo;
}
String inputPath = oldFile.getPath();
newFile = File.createTempFile("newVideo", suffix);
String outputPath = newFile.getPath();
int type = checkContentType(inputPath);
List command = getFFmpegCommand(type, ffmpegPath, inputPath, outputPath);
if (command.size() > 0) {
boolean result = process(command);
if(result) {
byteVideo = getBytes(outputPath);
}
}
}
catch (Exception e){
e.printStackTrace();
}
finally {
if(oldFile != null) {
oldFile.deleteOnExit();
}
if(newFile != null) {
newFile.deleteOnExit();
}
}
return byteVideo;
}
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 1;
} else if (type.equals("mpg")) {
return 1;
} else if (type.equals("wmv")) {
return 1;
} else if (type.equals("3gp")) {
return 1;
} else if (type.equals("mov")) {
return 1;
} else if (type.equals("mp4")) {
return 1;
} else if (type.equals("mkv")) {
return 1;
} else if (type.equals("asf")) {
return 0;
} else if (type.equals("flv")) {
return 0;
} else if (type.equals("rm")) {
return 0;
} else if (type.equals("rmvb")) {
return 1;
}
return 9;
}
private static boolean process(List<String> command) throws Exception {
try {
Process videoProcess = new ProcessBuilder(command).redirectErrorStream(true).start();
new PrintStream(videoProcess.getErrorStream()).start();
new PrintStream(videoProcess.getInputStream()).start();
int exitCode = videoProcess.waitFor();
if (exitCode == 1) {
return false;
}
return true;
} catch (Exception e) {
throw e;
}
}
private static List getFFmpegCommand(int type, String ffmpegPath, String oldFilePath, String outputPath) throws Exception {
List command = new ArrayList();
if (type == 1) {
command.add(ffmpegPath);
command.add("-y");
command.add("-i");
command.add(oldFilePath);
command.add("-c:v");
command.add("libx264");
command.add("-x264opts");
command.add("force-cfr=1");
command.add("-c:a");
command.add("mp2");
command.add("-b:a");
command.add("256k");
command.add("-vsync");
command.add("cfr");
command.add("-f");
command.add("mpegts");
command.add("-r");
command.add("25");
command.add("-b:v");
command.add("1000k");
command.add(outputPath);
} else if (type == 0) {
command.add(ffmpegPath);
command.add("-y");
command.add("-i");
command.add(oldFilePath);
command.add("-c:v");
command.add("libx264");
command.add("-x264opts");
command.add("force-cfr=1");
command.add("-vsync");
command.add("cfr");
command.add("-vf");
command.add("idet,yadif=deint=interlaced");
command.add("-filter_complex");
command.add("aresample=async=1000");
command.add("-c:a");
command.add("libmp3lame");
command.add("-b:a");
command.add("192k");
command.add("-pix_fmt");
command.add("yuv420p");
command.add("-f");
command.add("mpegts");
command.add("-r");
command.add("25");
command.add("-b:v");
command.add("1000k");
command.add(outputPath);
} else {
throw new Exception("不支持当前上传的文件格式");
}
return command;
}
/**
* 获得指定文件的byte数组
*/
public static byte[] getBytes(String filePath) {
byte[] buffer = null;
FileInputStream fis = null;
ByteArrayOutputStream bos = null;
try {
File file = new File(filePath);
if(!file.exists()) {
throw new FileNotFoundException("文件没有找到");
}
fis = new FileInputStream(file);
bos = new ByteArrayOutputStream((int)file.length());
byte[] b = new byte[1000];
int n;
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
buffer = bos.toByteArray();
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if(fis != null) {
fis.close();
}
if(bos != null) {
bos.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return buffer;
}
}