导入jar:
<!--截取视频缩略图-->
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv</artifactId>
<version>1.4.3</version>
</dependency>
<dependency>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>ffmpeg-platform</artifactId>
<version>4.0.2-1.4.3</version>
</dependency>
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.collections.map.HashedMap;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameConverter;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import javax.imageio.ImageIO;
public class VideoUtil {
/**
* 将视频文件帧处理并以“jpg”格式进行存储。
* 依赖FrameToBufferedImage方法:将frame转换为bufferedImage对象
*
* @param filePath
*/
public static Map<String,Object> grabberVideoFramer(String filePath){
ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
Map<String,Object> jsonObject = new HashMap<>();
// 视频资源缩略图路径
String contentPath = attr.getRequest().getServletContext().getRealPath("/");
String videothumbPath = "/upload/tutorRes/thumb/";
//最后获取到的视频的图片的路径
String videPicture="";
//Frame对象
Frame frame = null;
//标识
int flag = 0;
try {
/*
获取视频文件
*/
File file = new File(contentPath + filePath);
if(file == null || !file.exists()){
return jsonObject;
}
FFmpegFrameGrabber fFmpegFrameGrabber = new FFmpegFrameGrabber(contentPath + filePath);
fFmpegFrameGrabber.start();
//获取视频总帧数
int ftp = fFmpegFrameGrabber.getLengthInFrames();
while (flag <= ftp) {
frame = fFmpegFrameGrabber.grabImage();
/*
对视频的第五帧进行处理
*/
if (frame != null && flag==5) {
//文件绝对路径+名字
String fileName = UUID.randomUUID() + "_" + new Date().getTime() + ".jpg";
//文件储存对象
File outPut = new File(contentPath + videothumbPath + fileName);
if (!outPut.getParentFile().exists()) {
outPut.getParentFile().mkdirs();
}
ImageIO.write(FrameToBufferedImage(frame), "jpg", outPut);
//视频第五帧图的路径
//String savedUrl = outPut.getPath();
videPicture=videothumbPath + fileName;
break;
}
flag++;
}
if(fFmpegFrameGrabber.getFrameRate() == 0){
jsonObject.put("duration",0);
}else{
jsonObject.put("duration",Math.round(ftp / fFmpegFrameGrabber.getFrameRate()));
}
jsonObject.put("thumbPath",videPicture);
fFmpegFrameGrabber.stop();
fFmpegFrameGrabber.close();
} catch (Exception E) {
E.printStackTrace();
}
return jsonObject;
}
public static BufferedImage FrameToBufferedImage(Frame frame) {
//创建BufferedImage对象
Java2DFrameConverter converter = new Java2DFrameConverter();
BufferedImage bufferedImage = converter.getBufferedImage(frame);
return bufferedImage;
}
/**
* 测试:
* 1、在D盘中新建一个test文件夹,test中再分成video和img,在video下存入一个视频,并命名为test
* D:/test/video D:/test/img
* @param args
*/
public static void main(String[] args) {
String filePath = "";
ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
filePath = attr.getRequest().getServletContext().getRealPath("/") + "/file/thumb/";
System.out.println(filePath);
// grabberVideoFramer(videoFileName);
}
}