需求
*最近有这样的需求:
要求上传一个视频,然后截取视频中的六张图片,让用户选择其中一张图片作为封面 这里给大家展示后端的操作_
引入的依赖
下面展示一些 依赖的引入
。
// Javacv依赖,不太熟悉这个玩意的建议不要找阉割版的,老老实实导包,
//鄙人因为引用(这个建议不用,本地可以跑,挂Linux服务器上就直接GG)
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv</artifactId>
<version>0.8</version>
</dependency>
//上面版本的依赖导致在Linux跑不起来,我捣鼓了三个小时换了个依赖 完美解决
//以下是需要引入的依赖千万不要去引上面的!!!!!!!!!!!
//以下是需要引入的依赖千万不要去引上面的!!!!!!!!!!!
//以下是需要引入的依赖千万不要去引上面的!!!!!!!!!!!
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv</artifactId>
<version>1.4.1</version>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacpp</artifactId>
<version>1.4.1</version>
</dependency>
<dependency>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>opencv-platform</artifactId>
<version>3.4.1-1.4.1</version>
</dependency>
<dependency>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>ffmpeg-platform</artifactId>
<version>3.4.2-1.4.1</version>
</dependency>
后端代码
直接上————>后端代码
。
// 此类是控制器,截取六张图 这里的sys是我自己的存放路径
public AjaxResult listPageArrearsAccount(@RequestParam(value = "file", required = false) MultipartFile file) throws Exception {
if (null == file || file.isEmpty()) throw new CustomException("上传文件不能为空");
TimeInterval timer = DateUtil.timer();
// 上传视频到系统文件库
AjaxResult upload = sysOssService.upload(file, "video", "video");
// 根据 ossId 获取视频存放路径
SysOss sysOss = sysOssService.getById(upload.get("id").toString());
// 存储截取的图片存放系统文件库信息
List<AjaxResult> uploadImageList = new ArrayList<>();
FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber(sysOss.getRealPath());
frameGrabber.start();
int lenght = frameGrabber.getLengthInFrames();
for (int i = 1; i < 7; i++) {
double screenshot = lenght*i/7;
// 截取图片
String imageName = String.format("%s_%s", file.getOriginalFilename(), i);
MultipartFile multipartFile = fetchFrame(sysOss.getRealPath(), imageName, screenshot);
// 上传截取的图片到系统文件库
uploadImageList.add(sysOssService.upload(multipartFile, "video", "jpg"));
}
frameGrabber.flush();
frameGrabber.stop();
// 封装截取的图片信息
upload.put("list", uploadImageList);
log.info("DjVideoCutController - listPageArrearsAccount interval:{}", timer.interval());
return AjaxResult.success(upload);
};
/**
* 根据 视频URL和 帧数 截取图片
* @param videofile 视频地址
* @param imageName 截取图片名称
* @param let 帧数
* @return
* @throws Exception
*/
public static MultipartFile fetchFrame(String videofile, String imageName, double let) throws Exception {
FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber(videofile);
frameGrabber.start();
int lenght = frameGrabber.getLengthInFrames();// 截取视频获取视频的长度
int i = 0;
Frame frame = null;
while (i < lenght) {
frame = frameGrabber.grabFrame();
if ((i > let) && (frame.image != null)) break;
i++;
}
// opencv_core.IplImage img = frame.image;
// int owidth = img.width();
// int oheight = img.height();
Java2DFrameConverter converter =new Java2DFrameConverter();
BufferedImage srcBi = converter.getBufferedImage(frame);
int owidth = srcBi.getWidth();
int oheight = srcBi.getHeight();
// 对截取的帧进行等比例缩放
int width = 800;
int height = (int) (((double) width / owidth) * oheight);
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
image.getGraphics().drawImage(
srcBi.getScaledInstance(width, height, BufferedImage.SCALE_SMOOTH),
// frame.image.getBufferedImage().getScaledInstance(width, height, Image.SCALE_SMOOTH),
0, 0, null
);
frameGrabber.flush();
frameGrabber.stop();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", outputStream);
InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
MultipartFile multipartFile = new MockMultipartFile(imageName, String.format("%s.jpg", imageName), "image/jpeg", inputStream);
return multipartFile;
}
第一次写文,欢迎各位老师指导