czt
2025-06-30 b6faf70a938ca9e014d34b235dec449f2a21abb4
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package com.fzzy.api.utils;
 
import com.fzzy.data.ConfigData;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ws.schild.jave.Encoder;
import ws.schild.jave.EncoderException;
import ws.schild.jave.MultimediaObject;
import ws.schild.jave.encode.AudioAttributes;
import ws.schild.jave.encode.EncodingAttributes;
import ws.schild.jave.encode.VideoAttributes;
 
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
 
/**
 * 将文件转为二进制流工具类
 *
 * @author czt
 */
@Slf4j
@Service(FileUtils.BEAN_ID)
public class FileUtils {
    public static final String BEAN_ID = "base.fileUtil";
 
    @Autowired
    private ConfigData configData;
 
 
    /**
     * 根据文件路径将文件转为二进制文件流字符串
     *
     * @param filePath:文件路径
     * @return
     */
    public static String fileToByteString(String filePath) {
        byte[] bytes = file2byte(filePath);
        return toHexString(bytes);
    }
 
    /**
     * 根据文件路径将文件转为二进制数组
     *
     * @param filePath:文件路径
     * @return
     */
    public static byte[] file2byte(String filePath) {
        byte[] buffer = null;
        try {
            File file = new File(filePath);
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buffer;
    }
 
    /**
     * 将二进制数组转为字符串
     *
     * @param byteArray
     * @return
     */
    private static String toHexString(byte[] byteArray) {
        if (byteArray == null || byteArray.length < 1)
            throw new IllegalArgumentException(
                    "this byteArray must not be null or empty");
 
        final StringBuilder hexString = new StringBuilder();
        for (int i = 0; i < byteArray.length; i++) {
            if ((byteArray[i] & 0xff) < 0x10)// 0~F前面不零
                hexString.append("0");
            hexString.append(Integer.toHexString(0xFF & byteArray[i]));
        }
        return hexString.toString().toLowerCase();
    }
 
    public String getInoutFilePath(Date date) {
        if (null == date) {
            date = new Date();
        }
 
        String basePath = configData.getImgPath() + "INOUT/"
                + DateFormatUtils.format(date, "yyyyMM") + "/";
 
        File file = new File(basePath);
        if (!file.exists()) {
            file.mkdirs();
        }
 
        return basePath;
    }
 
    public String getSnapFilePath(Date date) {
        if (null == date) {
            date = new Date();
        }
 
        String basePath = configData.getImgPath() + "SNAP/"
                + DateFormatUtils.format(date, "yyyyMM") + "/";
        File file = new File(basePath);
        if (!file.exists()) {
            file.mkdirs();
        }
        return basePath;
    }
 
    public String getCommonFilePath(Date date) {
        if (null == date)
            date = new Date();
 
        String basePath = configData.getImgPath() + "COMMON/"
                + DateFormatUtils.format(date, "yyyyMM") + "/";
 
        File file = new File(basePath);
        if (!file.exists()) {
            file.mkdirs();
        }
        return basePath;
    }
 
    /**
     * 视频压缩
     *
     * @param source 源文件
     * @param target 目标文件
     * @param rate   压缩比
     */
    public static void compre(File source, File target, Integer rate) throws EncoderException {
        try {
            log.info("---------------开始压缩---------------");
            long start = System.currentTimeMillis();
 
            // 音频编码属性配置
            AudioAttributes audio = new AudioAttributes();
            audio.setCodec("libmp3lame");
            // 设置音频比特率,单位:b (比特率越高,清晰度/音质越好,当然文件也就越大 56000 = 56kb)
            // audio.setBitRate(new Integer(56_000));
            audio.setBitRate(new Integer(10));
            // 设置重新编码的音频流中使用的声道数(1 =单声道,2 = 双声道(立体声))
            audio.setChannels(1);
            // 采样率越高声音的还原度越好,文件越大
            // audio.setSamplingRate(new Integer(44100));
            audio.setSamplingRate(new Integer(22050));
            // 视频编码属性配置
            VideoAttributes video = new VideoAttributes();
            // 设置编码
            video.setCodec("h254");
            //设置音频比特率,单位:b (比特率越高,清晰度/音质越好,当然文件也就越大 5600000 = 5600kb)
            // video.setBitRate(new Integer(5_600_000 / rate));
            video.setBitRate(10 / rate);
 
            // 设置视频帧率(帧率越低,视频会出现断层,越高让人感觉越连续),这里 除1000是为了单位转换
            video.setFrameRate(15);
 
 
            // 编码设置
            EncodingAttributes attr = new EncodingAttributes();
            attr.setOutputFormat("mp4");
            attr.setAudioAttributes(audio);
            attr.setVideoAttributes(video);
 
            // 设置值编码
            Encoder ec = new Encoder();
            ec.encode(new MultimediaObject(source), target, attr);
 
 
            log.info("---------------结束压缩---------------");
            long end = System.currentTimeMillis();
            log.info("压缩前大小:" + source.length() + " 压缩后大小:" + target.length());
            log.info("压缩耗时:" + (end - start));
 
        } catch (EncoderException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
 
    }
}