package com.gkhy.hazmat.common.utils;
|
|
import lombok.extern.slf4j.Slf4j;
|
import org.jaudiotagger.audio.AudioFileIO;
|
import org.jaudiotagger.audio.mp3.MP3AudioHeader;
|
import org.jaudiotagger.audio.mp3.MP3File;
|
|
import java.io.BufferedReader;
|
import java.io.File;
|
import java.io.FileReader;
|
import java.io.IOException;
|
|
@Slf4j
|
public class VideoUtils {
|
/**
|
* 获取mp3时长
|
* @param filePath
|
* @return
|
*/
|
public static long getMp3Duration(String filePath){
|
try{
|
File file=new File(filePath);
|
MP3File f= (MP3File) AudioFileIO.read(file);
|
MP3AudioHeader audioHeader= (MP3AudioHeader) f.getAudioHeader();
|
return (long)audioHeader.getTrackLength();
|
}catch (Exception e){
|
log.error("获取音频mp3时长失败:{}",e.getMessage());
|
return 0;
|
}
|
}
|
|
|
/**
|
* 获取视频时长
|
* @param m3u8Path
|
* @return
|
*/
|
public static long getMp4Duration(String m3u8Path) {
|
double totalDuration=0;
|
try {
|
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(m3u8Path))) {
|
String line;
|
while ((line = bufferedReader.readLine()) != null) {
|
if (line.contains("#EXTINF:")) {
|
String durationString = line.replace("#EXTINF:", "").replace(",", "").trim();
|
totalDuration += Double.parseDouble(durationString);
|
}
|
}
|
}
|
}catch (IOException e){
|
log.error("获取视频mp4时长失败:{}",e.getMessage());
|
}
|
return (long) totalDuration;
|
}
|
|
}
|