pom.xml
<!--javacv 处理视频-->
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv</artifactId>
<version>0.8</version>
</dependency>
------------
application.yml(这个是自己命名的)
-----------------
image-config:
#输入路径
inputUrl: D:/video/a1.mp4
#输出路径
outputUrl: D:/video/frame.jpg
#输出格式
outputFormat: jpg
# 截取第 n 帧
frameNum: 1
----------
controller
@Resource
private ImagePathConfig imagePathConfig;
@GetMapping("/getvideoFirstPic")
@Validated
@ApiOperation(value = "getvideoFirstPic", notes = "获取视频的第一帧图片")
public void getvideoFirstPic () throws Exception {
DealVideo.fetchFrameToFile(imagePathConfig.getInputUrl(),imagePathConfig.getOutputUrl(),imagePathConfig.getOutputFormat(),imagePathConfig.getFrameNum());
}
--------------
DealVideo.java (视频处理工具类)
public class DealVideo {
public static String DEFAULT_IMG_FORMAT = "jpg";
public static void fetchFrameToFile(String videoFile, String targetFile, int frameNum) throws FrameGrabber.Exception, IOException {
checkInAnOut(videoFile, targetFile);
try {
File frameFile = new File(targetFile);
FFmpegFrameGrabber ff = new FFmpegFrameGrabber(videoFile);
ff.start();
int length = ff.getLengthInFrames();
if (frameNum < 0) {
frameNum = 0;
}
if (frameNum > length) {
frameNum = length - 5;
}
ff.setFrameNumber(frameNum);
int i = 0;
Frame f = null;
while (i < length) {
f = ff.grabFrame();
if ((i >= 5) && (f.image != null)) {
break;
}
i++;
}
opencv_core.IplImage img = f.image;
int width = img.width();
int height = img.height();
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
bi.getGraphics().drawImage(f.image.getBufferedImage().getScaledInstance(width, height, Image.SCALE_SMOOTH),
0, 0, null);
ff.flush();
ff.stop();
ImageIO.write(bi, DEFAULT_IMG_FORMAT, frameFile);
} catch (Exception e) {
throw new RuntimeException("转换视频图片异常");
}
}
public static void fetchFrameToFile(String videoFile, String targetFile, String outImgFormat, int frameNum) throws FrameGrabber.Exception, IOException {
checkInAnOut(videoFile, targetFile);
try {
File frameFile = new File(targetFile);
FFmpegFrameGrabber ff = new FFmpegFrameGrabber(videoFile);
ff.start();
int length = ff.getLengthInFrames();
if (frameNum < 0) {
frameNum = 0;
}
if (frameNum > length) {
frameNum = length - 5;
}
ff.setFrameNumber(frameNum);
int i = 0;
Frame f = null;
while (i < length) {
f = ff.grabFrame();
if ((i >= 5) && (f.image != null)) {
break;
}
i++;
}
opencv_core.IplImage img = f.image;
int width = img.width();
int height = img.height();
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
bi.getGraphics().drawImage(f.image.getBufferedImage().getScaledInstance(width, height, Image.SCALE_SMOOTH),
0, 0, null);
ff.flush();
ff.stop();
BufferedImage ret = ImageUtils2.rotateClockwise0(bi);
ImageIO.write(ret, outImgFormat, frameFile);
} catch (Exception e) {
throw new RuntimeException("转换视频图片异常");
}
}
public static String fetchFrameToBase64(String videoFile, int frameNum) throws FrameGrabber.Exception, IOException {
checkVideoFile(videoFile);
try (ByteArrayOutputStream output = new ByteArrayOutputStream();) {
FFmpegFrameGrabber ff = new FFmpegFrameGrabber(videoFile);
ff.start();
int length = ff.getLengthInFrames();
if (frameNum < 0) {
frameNum = 0;
}
if (frameNum > length) {
frameNum = length - 5;
}
ff.setFrameNumber(frameNum);
int i = 0;
Frame f = null;
while (i < length) {
f = ff.grabFrame();
if ((i >= 5) && (f.image != null)) {
break;
}
i++;
}
opencv_core.IplImage img = f.image;
int width = img.width();
int height = img.height();
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
bi.getGraphics().drawImage(f.image.getBufferedImage().getScaledInstance(width, height, Image.SCALE_SMOOTH),
0, 0, null);
ImageIO.write(bi, DEFAULT_IMG_FORMAT, output);
ff.flush();
ff.stop();
return new BASE64Encoder().encode(output.toByteArray());
} catch (Exception e) {
throw new RuntimeException("转换视频图片异常");
}
}
public static void checkInAnOut(String videoFile, String targetFile) {
checkVideoFile(videoFile);
checkTargetFileDir(targetFile);
}
public static void checkTargetFileDir(String targetFile) {
String dirPath = targetFile.substring(0, targetFile.lastIndexOf(File.separator) + 1);
File dir = new File(dirPath);
if (!dir.exists()) {
dir.mkdirs();
}
}
public static void checkVideoFile(String videoFile) {
File file = new File(videoFile);
if (!file.exists()) {
throw new RuntimeException("文件不存在");
}
}
}
------------
ImagePathConfig.java(配置文件读取工具类)
@Data
@Configuration
@ConfigurationProperties(prefix = "image-config")
public class ImagePathConfig {
private String inputUrl;
private String outputUrl;
private String outputFormat;
private Integer frameNum;
}
----------
ImageUtils2.java (图片旋转工具类,辅助类)
public class ImageUtils2 {
public static BufferedImage rotateClockwise0(BufferedImage bi) {
return bi;
}
public static BufferedImage rotateClockwise90(BufferedImage bi) {
int width = bi.getWidth();
int height = bi.getHeight();
BufferedImage bufferedImage = new BufferedImage(height, width, bi.getType());
for (int i = 0; i < width; i++)
for (int j = 0; j < height; j++)
bufferedImage.setRGB(height - 1 - j, width - 1 - i, bi.getRGB(i, j));
return bufferedImage;
}
public static BufferedImage rotateCounterclockwise90(BufferedImage bi) {
int width = bi.getWidth();
int height = bi.getHeight();
BufferedImage bufferedImage = new BufferedImage(height, width, bi.getType());
for (int i = 0; i < width; i++)
for (int j = 0; j < height; j++)
bufferedImage.setRGB(j, i, bi.getRGB(i, j));
return bufferedImage;
}
public static BufferedImage rotate180(BufferedImage bi) {
int width = bi.getWidth();
int height = bi.getHeight();
BufferedImage bufferedImage = new BufferedImage(width,height,bi.getType());
for (int i = 0; i < width; i++)
for (int j = 0; j < height; j++)
bufferedImage.setRGB( width - i-1,height-j-1, bi.getRGB(i, j));
return bufferedImage;
}
public static BufferedImage rotateHorizon(BufferedImage bi) {
int width = bi.getWidth();
int height = bi.getHeight();
BufferedImage bufferedImage = new BufferedImage(width,height,bi.getType());
for (int i = 0; i < width; i++)
for (int j = 0; j < height; j++)
bufferedImage.setRGB( width - i-1,j, bi.getRGB(i, j));
return bufferedImage;
}
public static BufferedImage rotateVertical(BufferedImage bi) {
int width = bi.getWidth();
int height = bi.getHeight();
BufferedImage bufferedImage = new BufferedImage(width,height,bi.getType());
for (int i = 0; i < width; i++)
for (int j = 0; j < height; j++)
bufferedImage.setRGB(i,height-1-j, bi.getRGB(i, j));
return bufferedImage;
}
}