使用Java统计文件夹视频总时长。

下一步要开始进入高数强化了,为了提前了解需要的工作量和工作时间,使用Java来控制统计一下强化高数的视频总时间,以此来估计所需要的总的学习时间。

两个问题,日志等级不改的话会打印一堆破烂东西,不是自己想看的。java没有分析视频的功能,这里使用了外部的依赖包。

maven依赖如下:

<dependencies>
    <dependency>
        <groupId>org.bytedeco</groupId>
        <artifactId>javacv-platform</artifactId>
        <version>1.5.7</version>
    </dependency>
</dependencies>

代码如下:

package com.company.kaoyan;

import java.io.File;
import org.bytedeco.ffmpeg.global.avutil;
import org.bytedeco.javacv.FFmpegFrameGrabber;

public class Test01 {
    /**
     * 统计文件夹地址mp4视频时常
     * @param args
     */
    public static void main(String[] args) {
//      修改日志等级
        avutil.av_log_set_level(avutil.AV_LOG_WARNING);
//      文件夹地址
        String folderPath = "D:\\deskTop\\25数学视频\\高数文件夹";
        calculateDurations(folderPath);
    }

    public static void calculateDurations(String folderPath) {
        File folder = new File(folderPath);
        File[] files = folder.listFiles();
        if (files == null) {
            return;
        }

        long totalDurationMillis = 0;

        for (File file : files) {
            if (file.isFile() && file.getName().endsWith(".mp4")) {
                long duration = getVideoDuration(file);
                int totalSeconds = (int) (duration / 1000);
                int hours = totalSeconds / 3600;
                int minutes = (totalSeconds % 3600) / 60;
                int seconds = totalSeconds % 60;
                System.out.println(file.getName() + ":" + hours + " 小时 " + minutes + " 分钟 " + seconds + " 秒");
                totalDurationMillis += duration;
            } else if (file.isDirectory()) {
                calculateDurations(file.getPath());
            }
        }

        int totalSeconds = (int) (totalDurationMillis / 1000);
        int hours = totalSeconds / 3600;
        int minutes = (totalSeconds % 3600) / 60;
        int seconds = totalSeconds % 60;
        System.out.println("总时长:" + hours + " 小时 " + minutes + " 分钟 " + seconds + " 秒");
    }
    public static long getVideoDuration(File file) {
        try (FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(file)) {
            grabber.start();
            long durationInMillis = (long) (grabber.getLengthInTime() / 1000);
            grabber.stop();
            return durationInMillis;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
}

输出结果这样的:

张宇老师2025考研高数强化,去除一部分的数一不用的,这里是数二部分的,部分数一部分被我删去了。一共26小时左右。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值