引入依赖
<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);
}
}