下一步要开始进入高数强化了,为了提前了解需要的工作量和工作时间,使用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小时左右。