1、引入依赖
由于整包过大,这里仅引入功能相关的部分资源,
<dependency>
<groupId>org.bytedecogroupId>
<artifactId>javacvartifactId>
<version>1.4.4version>
<exclusions>
<exclusion>
<groupId>org.bytedecogroupId>
<artifactId>javacppartifactId>
exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presetsgroupId>
<artifactId>flycaptureartifactId>
exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presetsgroupId>
<artifactId>libdc1394artifactId>
exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presetsgroupId>
<artifactId>libfreenectartifactId>
exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presetsgroupId>
<artifactId>libfreenect2artifactId>
exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presetsgroupId>
<artifactId>librealsenseartifactId>
exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presetsgroupId>
<artifactId>videoinputartifactId>
exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presetsgroupId>
<artifactId>opencvartifactId>
exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presetsgroupId>
<artifactId>tesseractartifactId>
exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presetsgroupId>
<artifactId>leptonicaartifactId>
exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presetsgroupId>
<artifactId>flandmarkartifactId>
exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presetsgroupId>
<artifactId>artoolkitplusartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.bytedecogroupId>
<artifactId>javacv-platformartifactId>
<version>1.4.4version>
<exclusions>
<exclusion>
<groupId>org.bytedecogroupId>
<artifactId>javacvartifactId>
exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presetsgroupId>
<artifactId>flycapture-platformartifactId>
exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presetsgroupId>
<artifactId>libdc1394-platformartifactId>
exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presetsgroupId>
<artifactId>libfreenect-platformartifactId>
exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presetsgroupId>
<artifactId>libfreenect2-platformartifactId>
exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presetsgroupId>
<artifactId>librealsense-platformartifactId>
exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presetsgroupId>
<artifactId>videoinput-platformartifactId>
exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presetsgroupId>
<artifactId>opencv-platformartifactId>
exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presetsgroupId>
<artifactId>tesseract-platformartifactId>
exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presetsgroupId>
<artifactId>leptonica-platformartifactId>
exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presetsgroupId>
<artifactId>flandmark-platformartifactId>
exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presetsgroupId>
<artifactId>artoolkitplus-platformartifactId>
exclusion>
exclusions>
dependency>
如果不是mvn项目,可以单独下载以下jar包后引入,或点击这里打包下载
# ffmpeg-4.1-1.4.4*.jar 主要是为了适配各种环境,可根据实际需求修改
ffmpeg-4.1-1.4.4.jar
ffmpeg-4.1-1.4.4-windows-x86_64.jar
ffmpeg-4.1-1.4.4-windows-x86.jar
ffmpeg-4.1-1.4.4-macosx-x86_64.jar
ffmpeg-4.1-1.4.4-linux-ppc64le.jar
ffmpeg-4.1-1.4.4-linux-armhf.jar
ffmpeg-4.1-1.4.4-linux-x86_64.jar
ffmpeg-4.1-1.4.4-linux-x86.jar
ffmpeg-4.1-1.4.4-android-x86_64.jar
ffmpeg-4.1-1.4.4-android-x86.jar
ffmpeg-4.1-1.4.4-android-arm64.jar
ffmpeg-4.1-1.4.4-android-arm.jar
ffmpeg-platform-4.1-1.4.4.jar
javacpp-1.4.4.jar
javacv-1.4.4.jar
javacv-platform-1.4.4.jar
2、java截图程序
package com.rangz.common.util;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameConverter;
/**
* 视频工具类
* @author RANGZHI
*/
public class VideoUtils {
/**
* 获取指定视频的帧并保存为图片至指定目录
* @param videofile 源视频文件路径
* @param framefile 截取帧的图片存放位置
* @throws Exception
*/
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 frame = null;
while (i < lenght) {
//此处网上分享经验,保证不会截取到黑屏,大都从第5帧截取,根据实际需求自行修改
frame = ff.grabFrame();
if(frame.image != null) break;
i++;
}
BufferedImage img = FrameToBufferedImage(frame);
int owidth = img.getWidth();
int oheight = img.getHeight();
// 对截取的帧进行等比例缩放
int width = 350;
int height = (int) (((double) width / owidth) * oheight);
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
bi.getGraphics().drawImage(img.getScaledInstance(width, height, Image.SCALE_SMOOTH),
0, 0, null);
ImageIO.write(bi, "jpg", targetFile);
ff.flush();
ff.stop();
ff.close();
System.out.println("生成视频缩略图,耗时:"+(System.currentTimeMillis() - start)+"ms");
}
/**
* 图片资源Frame转BufferedImage
* @param frame
* @return
*/
public static BufferedImage FrameToBufferedImage(Frame frame) {
Java2DFrameConverter converter = new Java2DFrameConverter();
BufferedImage bufferedImage = converter.getBufferedImage(frame);
return bufferedImage;
}
public static void main(String[] args) {
String videofile = "/home/rangz/upload/202303/202303018997.mp4";
String framefile = "/home/rangz/upload/202303/202303018997scale.jpg";
try {
fetchFrame(videofile, framefile);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}