一、简介
环境:SpringBoot 2.2.2.RELEASE、Java8、Maven
pom依赖:thumbnailator(处理图片)、javacv-platform(处理视频)
二、实现
2.1 依赖
<!--图片压缩-->
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.8</version>
</dependency>
<!--视频处理 截取视频缩略图 -->
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv-platform</artifactId>
<version>1.3.1</version>
</dependency>
2.2 视频抽帧(截取封面)
//创建FFmpegFrameGrabber 对象
FFmpegFrameGrabber ff = FFmpegFrameGrabber.createDefault(videoPath);
ff.start();
int ffLength = ff.getLengthInFrames();
Frame f;
int i = 0;
while (i < ffLength) {
f = ff.grabImage();
//截取第6帧
if((i>5) && (f.image != null)){
//生成图片的相对路径 例如:pic/uuid.png
pngPath = "来电名片mp4_缩略图"+".png";
//执行截图并放入指定位置
doExecuteFrame(f, dir+pngPath);
break;
}
i++;
}
ff.stop();
/**
* 截取缩略图
* @param f Frame
* @param targerFilePath:封面图片存放路径
*/
private static void doExecuteFrame(Frame f, String targerFilePath) {
String imagemat = "png";
if (null == f || null == f.image) {
return;
}
Java2DFrameConverter converter = new Java2DFrameConverter();
BufferedImage bi = converter.getBufferedImage(f);
File output = new File(targerFilePath);
try {
ImageIO.write(bi, imagemat, output);
} catch (IOException e) {
e.printStackTrace();
}
}
2.3 图片压缩
Thumbnails.of(new File("/Users/LiuShihao/IdeaProjects/besttone-newmsg-5g/files/Beijing.png"))
//图片大小(长宽)压缩比例 从0-1,1表示原图
.scale(0.3f)
//图片质量压缩比例 从0-1,越接近1质量越好
.outputQuality(0.3f)
.toOutputStream(new FileOutputStream("/Users/LiuShihao/IdeaProjects/besttone-newmsg-5g/files/showqrcode_50.jpg"));
System.out.println("完成");
三、源码
package com.haobai;
import net.coobird.thumbnailator.Thumbnails;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameConverter;
import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;
/**
* @author :LiuShihao
* @date :Created in 2022/1/7 12:26 下午
* @desc :截取视频封面并进行压缩
*/
@SpringBootTest
public class VideoTest {
@Test
public void test() throws IOException {
String dir = "files/";
String pngPath = "";
String videoPath = "files/来电名片.mp4";
FFmpegFrameGrabber ff = FFmpegFrameGrabber.createDefault(videoPath);
ff.start();
int ffLength = ff.getLengthInFrames();
Frame f;
int i = 0;
while (i < ffLength) {
f = ff.grabImage();
//截取第6帧
if((i>5) && (f.image != null)){
//生成图片的相对路径 例如:pic/uuid.png
pngPath = "来电名片mp4_缩略图"+".png";
//执行截图并放入指定位置
doExecuteFrame(f, dir+pngPath);
break;
}
i++;
}
ff.stop();
//压缩视频缩略图
Thumbnails.of(new File(dir+pngPath))
.scale(0.5f) //图片大小(长宽)压缩比例 从0-1,1表示原图
.outputQuality(0.1f) //图片质量压缩比例 从0-1,越接近1质量越好
.toOutputStream(new FileOutputStream("files/来电名片mp4压缩后缩略图.png"));
System.out.println("=============完成=============");
}
/**
* 截取缩略图
* @param f Frame
* @param targerFilePath:封面图片存放路径
*/
private static void doExecuteFrame(Frame f, String targerFilePath) {
String imagemat = "png";
if (null == f || null == f.image) {
return;
}
Java2DFrameConverter converter = new Java2DFrameConverter();
BufferedImage bi = converter.getBufferedImage(f);
File output = new File(targerFilePath);
try {
ImageIO.write(bi, imagemat, output);
} catch (IOException e) {
e.printStackTrace();
}
}
}