- 最近公司要求把一堆视频文件放到指定数据库里 最后实在是太多了 受不了了 写个程序偷个懒
需求 : 把视频名称及对应的在文件服务器的路径和视频第一帧图片放到对应数据库中
- 下面吧添加数据库的代码删除了 有需要可小伙伴可以自己根据自己的业务添加
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv</artifactId>
<version>0.8</version>
</dependency>
package com.example.upload.controller;
import com.example.upload.mapper.LivebroadcastMapper;
import com.example.upload.pojo.Livebroadcast;
import org.apache.commons.lang.RandomStringUtils;
import org.bytedeco.javacpp.opencv_core;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
@RestController
public class IOutil {
@GetMapping("/test")
public void test(String inpath,String outPathImg,String outPathVideo) throws Exception {
String path = "F:\\download\\压缩片\\" + inpath;
String outPath = "F:\\download\\" + outPathImg + "\\";
String outPathVi = "F:\\download\\" + outPathVideo + "\\";
File file = new File(path);
File[] files = file.listFiles();
for (File fi : files) {
String random = RandomStringUtils.randomNumeric(16);
String fileName = random + ".mp4";
String imageName = random + ".jpg";
if (fi.isFile()) {
IOutil.fetchFrame(fi.toString(),outPath + imageName);
FileInputStream fr = new FileInputStream(fi);
FileOutputStream fw = new FileOutputStream(outPathVi + fileName);
byte[] chars = new byte[1024];
int len;
while ((len = fr.read(chars)) != -1){
fw.write(chars,0,len);
}
fr.close();
fw.close();
}
}
}
public static void fetchFrame(String videofile, String framefile)
throws Exception {
long start = System.currentTimeMillis();
File targetFile = new File(framefile);
FFmpegFrameGrabber ff = new FFmpegFrameGrabber(videofile);
ff.start();
int lenght = ff.getLengthInFrames();
int i = 0;
Frame f = null;
while (i < lenght) {
f = ff.grabFrame();
if ((i > 5) && (f.image != null)) {
break;
}
i++;
}
opencv_core.IplImage img = f.image;
int owidth = img.width();
int oheight = img.height();
int width = 800;
int height = (int) (((double) width / owidth) * oheight);
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, "jpg", targetFile);
ff.stop();
System.out.println(System.currentTimeMillis() - start);
}
}