java如何获取视频的时长

发布时间:2023年12月30日

引入依赖

		<dependency>
            <groupId>ws.schild</groupId>
            <artifactId>jave-all-deps</artifactId>
            <version>2.5.1</version>
        </dependency>

java代码

import lombok.extern.slf4j.Slf4j;

import java.io.File;

import ws.schild.jave.*;

@Slf4j
public class VideoTimeUtil {

    /**
     * 获取视频时长
     */
    public static String[] parseDuration(String fileUrl) {
        String[] length = new String[2];

        try {
            File videoFile = new File(fileUrl);
            MultimediaObject multimediaObject = new MultimediaObject(videoFile);
            MultimediaInfo result = multimediaObject.getInfo();
            long durationInSeconds = result.getDuration() / 1000;

            length[0] = String.valueOf(durationInSeconds);
            length[1] = formatDuration(durationInSeconds);

        } catch (Exception e) {
            log.error("Error parsing video duration for file {}: {}", fileUrl, e.getMessage());
        }

        return length;
    }

    private static String formatDuration(long durationInSeconds) {
        int hours = (int) (durationInSeconds / 3600);
        int minutes = (int) ((durationInSeconds % 3600) / 60);
        int seconds = (int) (durationInSeconds % 60);

        return String.format("%02d:%02d:%02d", hours, minutes, seconds);
    }
}

文章来源:https://blog.csdn.net/nyasm/article/details/135305976
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。