Java 视频按帧分解成图片

使用JavaCV将视频按帧分解成图片

懒得画页面,直接用命令行输入视频地址和图片保存地址

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入视频位置:");
        // 原视频地址
        String sourceVideolUrl =  scanner.nextLine();
        File videoFile = new File(sourceVideolUrl);
        String sourceVideoPath = videoFile.getParent();
        // 视频图片保存地址
        String videoImageDir =  sourceVideoPath + "\\videoImage" ;
        // 视频转图片
        System.out.println("开始视频转图片。。。。");
        Util.VideoToPicture(sourceVideolUrl, videoImageDir);
        System.out.println("结束视频转图片。。。。");
public class Util {
    /**
     * 视频转图片
     *
     * @param videoPath 要转换的视频路径
     * @param imgPath   生成的图片路径
     * @return void
     */
    public static List<File> VideoToPicture(String videoPath, String imgPath) throws Exception {
        File imgFile = new File(imgPath);
        //判断保存的文件的文件夹是否存在,不存在创建。
        if (!imgFile.exists()) {
            imgFile.mkdirs();
        }
        File videoFile = new File(videoPath);
        List<File> imageList = new ArrayList<>();
        if (videoFile.exists()) {
            //实例化“截取视频”对象
            FFmpegFrameGrabber ff = new FFmpegFrameGrabber(videoFile);
            ff.start();
            int ftp = ff.getLengthInFrames();
            int flag = 0;
            while (flag <= ftp) {
                //获取帧
                Frame frame = ff.grabImage();
                if (frame == null) {
                    flag++;
                    continue;
                }
                File file = new File(imgPath + "\\" + flag + ".jpg");
                file.createNewFile();
                ImageIO.write(frameToBufferedImage(frame), "jpg", file);
                flag++;
                imageList.add(file);
            }
            ff.close();
            ff.stop();
            return imageList;
        } else {
            System.out.println("视频不存在:{}" + videoFile);
            return new ArrayList<>();
        }
    }

    /**
     * 帧转为流
     */
    private static RenderedImage frameToBufferedImage(Frame frame) {
        //创建BufferedImage对象
        Java2DFrameConverter converter = new Java2DFrameConverter();
        BufferedImage bufferedImage = converter.getBufferedImage(frame);
        return bufferedImage;
    }
}

代码下载地址

懒得下载或网络不好的,直接可去下方下载maven依赖包,放到本地的maven仓库即可
下载maven

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值