javacv的视频截图功能

发布于:2023-12-17 ⋅ 阅读:(144) ⋅ 点赞:(0)

之前做了一个资源库的小项目,因为上传资源文件包含视频等附件,所以就需要时用到这个功能。通过对视频截图,然后作为封面缩略图,达到美观效果。

首先呢,需要准备相关的jar包,之前我用的是低版本的1.4.2,后来就换成了高版本的1.5.9

因为我使用的是MyEclipse开发的,使用IDEA开发的可以参考其他博主的pom.xml依赖。

下面是1.5.9版本的
在这里插入图片描述
图片红框里的是linuxwindows下的jar包(注:根据运行环境选择,否则会报错)

java.lang.NoClassDefFoundError: Could not initialize class org.bytedeco.javacpp.avutil

jar包下载
链接: https://pan.baidu.com/s/1yx2YnEmahEP0FsLvoj1z-w
提取码:0912

我使用的方法是:

  • 通过java获取视频信息,使用FFmpeg方式截取视频第n帧生成图片

下面展示 java代码

FFmpegFrameGrabber ff = null;
try{
     //图片
     File targetFile = new File(logoAbsolutePath);
     //视频
     ff = new FFmpegFrameGrabber(yfResourceResource.getAbsolutePath());
     ff.start();
     int length = ff.getLengthInFrames();
     int j = 0;
     Frame f = null;
     while (j <= length) {
   		// 去掉前5帧,避免出现全黑的图片,依自己情况而定
   	 	f = ff.grabImage();
   	 	if ((j > 5) && (f.image != null)) {
      		break;
     	}
        j++;
     }
     ImageIO.write(FrameToBufferedImage(f), "jpg", targetFile);
     ff.stop();
     //生成的封面缩略图路径 logourl:我自定义的地址
     coverPath = request.getContextPath() + "/" + logourl + YfTools.getSubString(yfResourceResource.getAnnexName(),0,yfResourceResource.getAnnexName().length()-4)+".jpg?logoFlg=1";
         		    
}catch (Exception e) {
	log.error(e);
}finally {
	if(ff!=null){
    	try {
     		ff.close();
     	} catch (Exception ex) {
     		log.error(ex);
     	}
    }
    ff = null;
}

上传资源成功后
在这里插入图片描述


网站公告

今日签到

点亮在社区的每一天
去签到