导入依赖包
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv</artifactId>
<version>1.5.3</version>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>ffmpeg-platform</artifactId>
<version>4.2.2-1.5.3</version>
</dependency>
代码: ?
// 视频抓取器
String img_type = "jpg";
FFmpegFrameGrabber ff = FFmpegFrameGrabber.createDefault(videoPath);
Java2DFrameConverter converter = new Java2DFrameConverter();
ff.start();
while (true){
// 获取每一帧的数据
Frame frame = ff.grabImage();
if(frame == null) break;
// 根据每帧的时间戳进行文件命名
long timestamp = frame.timestamp;
String curTime = simpleDateFormat.format(new Date(parse.getTime() + timestamp));
// 写出图片
BufferedImage bi = converter.getBufferedImage(frame);
String out_file_name = file_name + "@" + curTime + "." + img_type;
String file_name_path = out_directory_path + File.separator + out_file_name;
File output = new File(file_name_path);
try{
ImageIO.write(bi, img_type, output);
} catch (IOException e) {
e.printStackTrace();
}
}
ff.stop();
|