Java 获取 视频 时长

复制代码
<dependency>
    <groupId>org.bytedeco</groupId>
    <artifactId>javacv-platform</artifactId>
    <version>1.5.7</version> 
</dependency>

首先在 pom 中引入包

根据以下工具类,直接调用传入文件路径即可

复制代码
public static Integer getMp4Seconds(String filePaths){
        String filePath = filePaths; // 替换为你的MP4文件路径
        int seconds = 0;
        FFmpegFrameGrabber grabber = null;
        try {
            grabber = new FFmpegFrameGrabber(filePath);
            grabber.start();
            //这一步获取的是微妙,除以 1000000 获取保留整数部位的秒
            seconds = Math.toIntExact(grabber.getLengthInTime())/1000000;
            System.out.println("Duration of the MP4 file: " + seconds + " s");
            return seconds; // 返回秒
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        } finally {
            if (grabber != null) {
                try {
                    grabber.stop();
                } catch (Exception ignored) {
                }
            }
        }
    }
相关推荐
CodeStats10 小时前
《源纹天书》第121-125章:源匠归来——全栈重构与归元圣域的2.0时代
java·开发语言·源纹天书
AI人工智能+电脑小能手10 小时前
【大白话说Java面试题 第154题】【06_Spring篇】第14题:Spring 支持的 Bean 作用域
java·开发语言·spring·面试
wuminyu10 小时前
markword在高并发场景下变化剖析
java·linux·c语言·jvm·c++
love530love10 小时前
AI Agent + 本地 ComfyUI 无头模式实战:关闭 IDE 后 AI 独立重启并完成图文生成
ide·人工智能·windows·python·音视频·agent·devops
组合缺一10 小时前
用 ChatModel 构建 LLM 驱动的 Java 应用
java·开发语言·ai·llm·solon·rag
zzz_236810 小时前
【Java实习面试算法冲刺】哈希!
java·算法·面试
带刺的坐椅10 小时前
ReActAgent 使用指南:构建会思考、能行动的 AI Agent
java·ai·llm·solon·loop·react-agent
漂亮的摩托11 小时前
如何编写一个SpringBoot项目告警推送的Starter
java·spring boot·后端
盖伦暴打诺手11 小时前
类比推理知识点
java
ch.ju11 小时前
Java程序设计(第3版)第四章——类加载
java·开发语言