/**
* 截取视频某些帧转换为图片
*
* @author 周化益
* @param entity 实体Class
* @param entityId 记录id
* @param fileName 文件名
* @param fileSuffix 文件后缀
*/
public synchronized static <T> void capturePhotoFromVideo(Class<T> entity, long entityId, String fileName) {
Properties props = initConfigFile();
if (null != props){
String inFilePath = null;//本地文件
String outFilePath = null;//视频截取的图片
inFilePath = CommonUtil.uploadPath() + "upload/" + entity.getSimpleName() + "/" + entityId + "/" + fileName;
File file = new File(inFilePath);
if (file.exists()) {
Process process = null;
int duration = CommonUtil.getMediaDuration(file.getPath());
duration = duration/120000;//视频总时间的三分之一处截取
StringBuffer command = new StringBuffer();
File thumbFile = new File(CommonUtil.uploadPath()+ "upload/",entity.getSimpleName() + "/" + entityId);
if (!thumbFile.exists()){
thumbFile.mkdirs();
}
String path = FileLoad.class.getResource("").getPath();
int end = path.lastIndexOf("com");
int dot = fileName.lastIndexOf('.');
outFilePath = thumbFile + "/" + fileName.substring(0,dot) + ".jpg";
command.append("cmd /c ").append(path.substring(1, end)+"resources/")
.append("ffmpeg.exe").append(" -i ").append(inFilePath)
.append(" -y -f image2 -ss ").append(duration).append(" -vframes 1 -s ")
.append(DEFAULT_VIDEO_PHOTO_RESOLUTION).append(" ").append(outFilePath);
try {
System.out.println(command);
String systemOutText = "抓取视频图片所用时间为:";
process = execCommand(command.toString(),systemOutText);
} catch (Exception e) {
e.printStackTrace();
} finally{
if (null != process){
process.destroy();
}
}
}
}
}
/**
* 生成缩略图
*
* @author 周化益
* @param inFilePath 源文件
* @param outFilePath 生成的缩略图文件
*/
public synchronized static void makeSmallImage(String inFilePath,String outFilePath){
try {
String convertPath = " convert.exe";
String command = "cmd /c "+getFfrmpegPath()+convertPath+" -resize \"100x100>\" +profile \"*\" "+inFilePath+" "+ outFilePath;
String systemOutText = inFilePath+"文件的缩略图获取时间:";
execCommand(command,systemOutText);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 执行命令行语句
*
* @author 周化益
* @param command 命令行语句
* @param systemOutText 执行后的输出语句
*/
public static Process execCommand(String command,String systemOutText){
Process process = null;
try {
long startTime = System.currentTimeMillis();
String[] cmdArry = command.split(" ");
ProcessBuilder builder = new ProcessBuilder(cmdArry);
builder.redirectErrorStream(true);
process = builder.redirectErrorStream(true).start();
//防止线程堵塞
new PrintStream(process.getInputStream()).start();
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
process.waitFor();
br.close();
long endTime = System.currentTimeMillis();
if (null!=systemOutText && !systemOutText.equals("")){
System.out.println(systemOutText+(endTime-startTime)+"毫秒");
}
} catch (Exception e) {
e.printStackTrace();
}
return process;
}
/**
* 获取视频项目根目录
*
* @author zhy
* @return
*/
public static String uploadPath() {
String path = CommonUtil.class.getResource("") .getPath();
int end = path.lastIndexOf("WEB-INF");
path = path.substring(1, end);
return path+"/";
}
/**
* 获取多媒体文件长度
*
* @author 周化益
* @param filePath 文件路径
* @return int 文件长度
*/
public synchronized static int getMediaDuration(String filePath){
int duration = 0;
int readLen = 0;
byte[] byteTemp = new byte[5];
InputStream is = null;
Process process = null;
try {
StringBuffer command = new StringBuffer();
command.append("cmd /c ").append("MediaInfo.exe").append(" --Inform=General;")
.append("\"%Duration%\" \"").append(filePath).append("\"");
process = Runtime.getRuntime().exec(command.toString());
is = process.getInputStream();
command.setLength(0);
while((readLen = is.read(byteTemp))!=-1){
command.append(new String(byteTemp,0,readLen));
}
if (command.length()>0){
duration = Integer.parseInt(command.toString().replaceAll("\\s", ""));
}
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
if (null!=is){
is.close();
}
if (null!=process){
process.destroy();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return duration;
}
需要下载ffmpeg.exe和MediaInfo.exe