java获取本地视频时长的代码实例,获取的结果单位是秒
maven依赖
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv-platform</artifactId>
<version>1.5.7</version>
</dependency>
测试代码
@Test
void test() throws Exception {
String path1 = "D:/123/test.mp4";
testFFmpeg(path1);
testJavaCV(path1);
}
//使用FFmpeg获取
public void testFFmpeg(String path) throws Exception {
// 读取视频文件
FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(path);
grabber.start();
// 获取视频长度(单位:秒)
int duration = grabber.getLengthInFrames() / (int) grabber.getFrameRate();
System.out.println("Video duration: " + duration + " seconds");
grabber.stop();
}
//使用JavaCV获取
public void testJavaCV(String path) throws Exception {
// 读取视频文件
FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(path);
grabber.start();
// 获取视频长度(单位:秒)
int duration = (int) (grabber.getLengthInTime() / avutil.AV_TIME_BASE);
System.out.println("Video duration: " + duration + " seconds");
grabber.stop();
}
测试结果
视频时长为4分36的视频
视频时长为10分51的视频