java如何获取视频的时长

引入依赖

		<dependency>
            <groupId>ws.schildgroupId>
            <artifactId>jave-all-depsartifactId>
            <version>2.5.1version>
        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);
    }
}

你可能感兴趣的:(java,python,开发语言,java获取视频时长)