无关风月
2 天以前 ce0651907f18a57dae80065e01589e975530f53e
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
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.ruoyi.common.utils;
 
 
import lombok.extern.slf4j.Slf4j;
import ws.schild.jave.MultimediaInfo;
import ws.schild.jave.MultimediaObject;
 
import java.io.File;
import java.net.URL;
import java.util.Arrays;
 
@Slf4j
public class VideoUtil {
 
 
    /**
     * 视频时长
     *
     * @param fileUrl
     * @return String[] 0=秒时长,1=展示时长(格式如 01:00:00)
     */
    public static String[] parseDuration(String fileUrl) {
        String[] length = new String[2];
        try {
            //
//            URL source = new URL(fileUrl);
            // 构造方法 接受URL对象
//            MultimediaObject instance = new MultimediaObject(source);
            // 构造方法 接受File对象
            MultimediaObject instance = new MultimediaObject(new File(fileUrl));
            MultimediaInfo result = instance.getInfo();
            Long ls = result.getDuration() / 1000;
            length[0] = String.valueOf(ls);
            Integer hour = (int) (ls / 3600);
            Integer minute = (int) (ls % 3600) / 60;
            Integer second = (int) (ls - hour * 3600 - minute * 60);
            String hr = hour.toString();
            String mi = minute.toString();
            String se = second.toString();
            if (hr.length() < 2) {
                hr = "0" + hr;
            }
            if (mi.length() < 2) {
                mi = "0" + mi;
            }
            if (se.length() < 2) {
                se = "0" + se;
            }
 
            String noHour = "00";
            if (noHour.equals(hr)) {
                length[1] = mi + ":" + se;
            } else {
                length[1] = hr + ":" + mi + ":" + se;
            }
 
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return length;
    }
 
    public static void main(String[] args) {
        String url = "C:/Users/Admin/Desktop/qrcode/2023-10-25/video.mp4";
        String[] strings = parseDuration(url);
        System.err.println(Arrays.toString(strings));
    }
 
}