java获取视频截图作为封面图

前提是上传视频的时候想要视频的封面图内容是视频中的某一时刻的内容截图,如图
在这里插入图片描述
话不多说,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显示到前台即可

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值