当前位置: 首页 > news >正文

Java使用FFmpegFrameGrabber进行视频拆帧,结合Thumbnails压缩图片保存到文件夹

引入依赖

        <dependency>
            <groupId>net.coobird</groupId>
            <artifactId>thumbnailator</artifactId>
            <version>0.4.17</version>
        </dependency>
        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>javacv</artifactId>
            <version>1.5.7</version>
        </dependency>
        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>javacv-platform</artifactId>
            <version>1.5.7</version>
        </dependency>

视频拆帧处理

    public static void main(String[] args) {
        File file = new File("C:\\Users\\EDY\\Desktop\\frame");
        File[] files = file.listFiles();
        System.out.println("files.length = " + files.length);
        long millis = System.currentTimeMillis();
        try (FFmpegFrameGrabber grabber = new FFmpegFrameGrabber("https://a803.hmgreat.com:9001/sucaiwang/2025-03-07/170bdbb2-72d0-49a3-9705-0b55d8bd6719.mp4")) {
            grabber.start();
            int length = grabber.getLengthInVideoFrames();
            System.out.println("length = " + length);
            Java2DFrameConverter converter = new Java2DFrameConverter();
            int frameCount = 0;
            Frame frame;
            while ((frame = grabber.grabImage()) != null) {
                //计数小于文件夹图片数量则表示已做过处理,需跳过不进行重复处理
                if (frameCount < files.length) {
                    frameCount++;//计数
                    continue;
                }
                frameCount++;//计数
                BufferedImage img = converter.getBufferedImage(frame);
                //压缩图片并且保存
                Thumbnails.of(img).scale(0.5).toFile(new File(file.getPath() + File.separator + "frame_" + frameCount + ".jpg"));
            }
            grabber.stop();
            System.out.println("耗时: " + ((System.currentTimeMillis() - millis) / 1000));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

执行效果

在这里插入图片描述
在这里插入图片描述

相关文章:

  • 一键秒连WiFi智能设备,uni-app全栈式物联开发指南。
  • 说说你对Java里Integer缓存的理解?
  • 专访LayaAir引擎最有价值专家-施杨
  • pipost 如何提升团队协作效率 [特殊字符]
  • Simulink学习——Hysteresis模块(滞回模块)
  • 【万字总结】前端全方位性能优化指南(四)——虚拟DOM批处理、文档碎片池、重排规避
  • SpringAI集成DeepSeek
  • 如何进行APP开发前的市场调研与需求分析
  • 能快速搭建网站的模板资源平台
  • Spring Cloud 服务监控 - Sleuth + Zipkin 全链路追踪实战
  • 2025-03-19 学习记录--C/C++-C 库函数 - qsort() 实现快速排序
  • [Linux] make自动化构建
  • 【多线程-第四天-SDWebImage介绍 Objective-C语言】
  • Ubuntu24.10编译Android12源码并运行于模拟器中
  • k8s中的组件
  • AI训练如何获取海量数据,论平台的重要性
  • 部署|Llama-Facory微调模型部署
  • element-plus中Dropdown下拉菜单组件的使用
  • word插入Mathtype公式居中和自动更新
  • PairRE: Knowledge Graph Embeddings via Paired Relation Vectors(论文笔记)
  • 优化师是做什么的/优化公司怎么优化网站的
  • wordpress自定义属性/天津海外seo
  • 制作外贸网站模板下载/最新新闻热点事件及评论
  • 沧州 中企动力提供网站建设/网络优化论文
  • 做旅游的网站那个便宜/陕西seo推广
  • 信息流广告名词解释/官网优化 报价