<!--截取视屏图片-->
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>ffmpeg-platform</artifactId>
<version>3.2.1-1.3</version>
</dependency>
//个人命名
private String bookImage = "assistantBook/bookImage/";
//oss地址
private String ossUrl = "http://www.baidu.com";
/**
* @param endPoint:
* @param accessKeyId:
* @param accessKeySecret:
* @param bucketName:
* @param videoFile: 需要截图的文件地址
* @return: java.lang.String
* @author: linyonghu
* @date: 2021/12/7 10:24
* @description:
*/
public String fetchFrame(String endPoint, String accessKeyId, String accessKeySecret, String bucketName,String videoFile){
String fileName = null;
try{
FFmpegFrameGrabber ff = new FFmpegFrameGrabber(ossUrl+videoFile);
ff.start();
int lenght = ff.getLengthInFrames();
int i = 0;
Frame f = null;
while (i < lenght) {
// 过滤前5帧,避免出现全黑的图片,依自己情况而定
f = ff.grabFrame();
if ((i > 48) && (f.image != null)) {
break;
}
i++;
}
int owidth = f.imageWidth;
int oheight = f.imageHeight;
// 对截取的帧进行等比例缩放
int width = 400;
int height = (int) (((double) width / owidth) * oheight);
Java2DFrameConverter converter = new Java2DFrameConverter();
BufferedImage fecthedImage = converter.getBufferedImage(f);
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
bi.getGraphics().drawImage(fecthedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ImageIO.write(bi,"jpg",bos);
byte[] byteArray = bos.toByteArray();
ByteArrayInputStream bis = new ByteArrayInputStream(byteArray);
fileName = bookImage + DateUtils.getDate("yyyyMMddHHmmss")+".jpg";
upload(endPoint,accessKeyId,accessKeySecret,bucketName,bis,fileName);
ff.flush();
ff.stop();
bos.flush();;
bos.close();
bis.close();
}catch (Exception e){
logger.info("生成图书图片异常{}",e.getMessage());
}
return fileName;
}
public void upload(String endPoint, String accessKeyId, String accessKeySecret, String bucketName, InputStream is, String path){
OSSClient client = new OSSClient(endPoint,accessKeyId, accessKeySecret);
try{
if(path.startsWith("/")){
path=path.substring(1);
}
client .putObject(bucketName, path, is);
} catch(RuntimeException e){
logger.error("oss上传文件流出错",e);
} finally {
client.shutdown();
}
}
转载:https://www.cnblogs.com/qinxiaofei/p/9766351.html