|
|
@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
package com.supervision.manage.video;
|
|
|
|
|
|
|
|
import org.bytedeco.javacpp.opencv_core;
|
|
|
|
|
|
|
|
import org.bytedeco.javacv.*;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import javax.imageio.ImageIO;
|
|
|
|
|
|
|
|
import java.awt.image.BufferedImage;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class VideoPreviewGenerator {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 截取视频第一帧为图片展示
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param filePath 视频路径
|
|
|
|
|
|
|
|
* @param targetFilePath 第一帧图片存储位置
|
|
|
|
|
|
|
|
* @param targetFileName 图片名称
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static void getVideoFirstFrameImage(String filePath, String targetFilePath, String targetFileName) throws FrameGrabber.Exception {
|
|
|
|
|
|
|
|
FFmpegFrameGrabber ff = FFmpegFrameGrabber.createDefault(filePath);
|
|
|
|
|
|
|
|
ff.start();
|
|
|
|
|
|
|
|
String rotate = ff.getVideoMetadata("rotate");
|
|
|
|
|
|
|
|
Frame f;
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
while (i < 1) {
|
|
|
|
|
|
|
|
f = ff.grabImage();
|
|
|
|
|
|
|
|
opencv_core.IplImage src;
|
|
|
|
|
|
|
|
if (null != rotate && rotate.length() > 1) {
|
|
|
|
|
|
|
|
OpenCVFrameConverter.ToIplImage converter = new OpenCVFrameConverter.ToIplImage();
|
|
|
|
|
|
|
|
src = converter.convert(f);
|
|
|
|
|
|
|
|
f = converter.convert(rotate(src, Integer.parseInt(rotate)));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
doExecuteFrame(f, targetFilePath, targetFileName);
|
|
|
|
|
|
|
|
i++;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ff.stop();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 进行旋转角度操作(为了保证截取到的第一帧图片与视频中的角度方向保持一致)
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static opencv_core.IplImage rotate(opencv_core.IplImage src, int angle) {
|
|
|
|
|
|
|
|
opencv_core.IplImage img = opencv_core.IplImage.create(src.height(), src.width(), src.depth(), src.nChannels());
|
|
|
|
|
|
|
|
opencv_core.cvTranspose(src, img);
|
|
|
|
|
|
|
|
opencv_core.cvFlip(img, img, angle);
|
|
|
|
|
|
|
|
return img;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void doExecuteFrame(Frame f, String targetFilePath, String targetFileName) {
|
|
|
|
|
|
|
|
if (null == f || null == f.image) {
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Java2DFrameConverter converter = new Java2DFrameConverter();
|
|
|
|
|
|
|
|
String imageMat = "jpg";
|
|
|
|
|
|
|
|
String fileName = targetFilePath + File.separator + targetFileName + "." + imageMat;
|
|
|
|
|
|
|
|
BufferedImage bi = converter.getBufferedImage(f);
|
|
|
|
|
|
|
|
File output = new File(fileName);
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
ImageIO.write(bi, imageMat, output);
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) throws FrameGrabber.Exception {
|
|
|
|
|
|
|
|
String url = "F:\\tmp\\测试1.mp4";
|
|
|
|
|
|
|
|
getVideoFirstFrameImage(url, "F:\\tmp\\", "first");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|