importjava.io.File;importjava.io.IOException;importjava.util.ArrayList;importjava.util.LinkedList;importjava.util.List;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;public classScreenShotUtil {//private static Logger logger = LoggerFactory.getLogger(ScreenShotUtil.class);
private static final Logger logger = LoggerFactory.getLogger(ScreenShotController.class);public staticPointScreen shotByTimePoint(String screenshotEXEPath, String srcFileUrl, String timePoint,
String size, String outUrl) {
List cmd = new LinkedList();
cmd.add(screenshotEXEPath);//cmd.add("-y");
cmd.add("-i");
cmd.add(srcFileUrl);
cmd.add("-ss");
cmd.add(timePoint);//cmd.add("-i");//cmd.add(srcFileUrl);
cmd.add("-t");
cmd.add("0.001");
cmd.add("-s");
cmd.add(size);
cmd.add("-ignore-black");
cmd.add(outUrl+ "\\temp\\.jpg");//logger.debug("shotByTimePoint: " + cmd);//System.out.println(cmd);
ProcessBuilder pb = newProcessBuilder();
pb.command(cmd);
File imageFolder= new File(outUrl + "\\temp");
imageFolder.mkdirs();try{
Process process=pb.start();
process.waitFor();
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}catch(InterruptedException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
File newImageFile= null;for(File imageFile : imageFolder.listFiles()) {
String imageName= timePoint.replaceAll("[:|.]", "-");
newImageFile= new File(outUrl, imageName + ".jpg");
imageFile.renameTo(newImageFile);
imageFile.delete();
}return newPointScreen(newImageFile.getAbsolutePath(), timePoint);
}public static ListshotByKeyFrame(String screenshotEXEPath, String srcFileUrl, String size,
String outUrl) {/*** 转场分析抽取若干张图片(输出名称包含百纳秒) snapshot -i oceans-clip.webm -scene_transition 1
* oceans-clip-%013d.jpg 输出例为 oceans-clip-0000000000123.jpg,
* oceans-clip-0000000004567.jpg, oceans-clip-0000000089000.jpg*/List cmd = new LinkedList();
List pointScreens = new ArrayList<>();try{
cmd.add(screenshotEXEPath);
cmd.add("-i");
cmd.add(srcFileUrl);
cmd.add("-scene_transition");
cmd.add("1");
cmd.add("-s");
cmd.add(size);
cmd.add("-ignore-black");
cmd.add(outUrl+ "\\temp\\-%013d.jpg");
ProcessBuilder pb= newProcessBuilder();
pb.command(cmd);
File tempFolder= new File(outUrl + "\\temp");
tempFolder.mkdirs();//开始抽帧
Process process =pb.start();
process.waitFor();
File newImageFile= null;if (tempFolder.listFiles() != null) {for(File imageFile : tempFolder.listFiles()) {
String imageNameWithSuffix=imageFile.getName();
String timePoint=NanosecondsToTimeUtil
.nanosecondsToTime(imageNameWithSuffix.substring(1, imageNameWithSuffix.indexOf(".")));
String imageName= timePoint.replaceAll("[:|.]", "-");
newImageFile= new File(outUrl, imageName + ".jpg");//System.out.println(newImageFile);
imageFile.renameTo(newImageFile);
imageFile.delete();
pointScreens.add(newPointScreen(newImageFile.getAbsolutePath(), timePoint));
}
}
}catch(Exception e) {
logger.info("抽取关键帧报错: " +e.getMessage(), e);
}returnpointScreens;
}public staticString photoWall(String screenshotEXEPath, String srcFileUrl, String size, String outFileUrl) {
shotByKeyFrame(screenshotEXEPath, srcFileUrl, size, outFileUrl);
ImageAddWatermarkUtil.imageAddWatermark(outFileUrl);//ImageMergeUtil.mergeImage(outFileUrl, size);
returnImageMergeUtil.mergeImage(outFileUrl, size);
}public static voidmain(String[] args) {//System.out.println(shotByTimePoint(exePath,srcFileUrl,"00:00:00.000",size,outFileUrl));//0时间点截图测试//shotByKeyFrame(srcFileUrl, size, outFileUrl);//0关键帧截图测试//1.定时截图测试 时间间隔 intervalTime 秒//long intervalTime = 10;//long videoLength = VideoDuration.getVideoDuration(srcFileUrl);//List pointScreens = new ArrayList<>();//for (long i = 0; i <= videoLength; i = i + intervalTime) {//timePoint =//NanosecondsToTimeUtil.nanosecondsToTime(Long.toString(i*1000*10000));//pointScreens.add(ScreenShotUtil.shotByTimePoint(srcFileUrl, timePoint, size,//outFileUrl));//}//for (Iterator iterator = pointScreens.iterator();//iterator.hasNext();) {//System.out.println(iterator.next());//}//2.定点截图//List extraParam = new ArrayList<>();//extraParam.add("00:01:01.000");//extraParam.add("00:02:02.000");//extraParam.add("00:03:03.000");//extraParam.add("00:14:04.000");//for (Iterator iterator = extraParam.iterator(); iterator.hasNext();)//ScreenShotUtil.shotByTimePoint(srcFileUrl, iterator.next(), size,//outFileUrl);//3.定量截图//long videoDuration = VideoDuration.getVideoDuration(srcFileUrl);//int intervalTime = (int)(videoDuration/(10-1));//for (long i = 0; i <= videoDuration; i = i + intervalTime) {//String timePoint1 =//NanosecondsToTimeUtil.nanosecondsToTime(Long.toString(i*1000*10000));//ScreenShotUtil.shotByTimePoint(srcFileUrl, timePoint1, size, outFileUrl);//}//4.照片墙//System.out.println(photoWall(srcFileUrl, size, outFileUrl));
}
}