heheng
7 天以前 d8012ee77b6a9e86611aae9074d5925826f4210d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com.gkhy.exam.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;
    }
 
}