前提是上传视频的时候想要视频的封面图内容是视频中的某一时刻的内容截图,如图
话不多说,demo如下,直接上依赖
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv-platform</artifactId>
<version>1.4.1</version>
</dependency>
String pngPath = "";
FFmpegFrameGrabber ff=null;
try {
ff = FFmpegFrameGrabber.createDefault(new File("D://picpath//video//111.mp4"));
} catch (Exception e) {
e.printStackTrace();
}
ff.start();
int ffLength = ff.getLengthInFrames();
Frame f;
int i = 0;
String weizhi="";
while (i < ffLength) {
f = ff.grabFrame();
//截取第20帧
if ((i > 20) && (f.image != null)) {
//生成图片的相对路径 例如:pic/uuid.png
pngPath = UUID.randomUUID().toString().replace("-", "")+".png";
//执行截图并放入指定位置
//System.out.println("存储图片 : " + (dir + pngPath));
weizhi = doExecuteFrame(f, "D://picpath//video//" + pngPath);
break;
}
i++;
}
ff.stop();
//返回图片生成的路径
private static String doExecuteFrame(Frame f, String targerFilePath) {
String imagemat = "png";
if (null == f || null == f.image) {
return null;
}
Java2DFrameConverter converter = new Java2DFrameConverter();
BufferedImage bi = converter.getBufferedImage(f);
File output = new File(targerFilePath);
System.err.println("output="+output);
try {
ImageIO.write(bi, imagemat, output);
return output.toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
把生成的图片weizhi显示到前台即可