czt
2025-08-07 bc82af6e3664195bbcade1c769c8553457bfb09a
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package com.fzzy.web;
 
import com.bstek.dorado.util.DateUtils;
import com.fzzy.api.data.ApiParam;
import com.fzzy.async.fzzy30.Fzzy30SyncService12;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
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 ws.schild.jave.info.AudioInfo;
import ws.schild.jave.info.VideoSize;
 
 
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
 
/**
 * @author vince.xu
 * @Title: TestController
 * @ProjectName igds-api
 * @Description: TODO
 * @date 2022-9-2512:50
 */
@Slf4j
@Controller
@RequestMapping
public class TestController {
    @Autowired
    private Fzzy30SyncService12 fzzySyncService12;
 
    /**
     *
     */
    @RequestMapping(value = "test", method = RequestMethod.GET)
    public String test(@RequestParam(name = "kqdm") String kqdm, @RequestParam(name = "deptId") String deptId, @RequestParam(name = "start") String start, @RequestParam(name = "end") String end) {
        try {
            ApiParam apiParam = new ApiParam();
            apiParam.setKqdm(kqdm);
            apiParam.setDeptId(deptId);
            apiParam.setStart(DateUtils.parse("yyyy-MM-dd HH:mm:ss", start));
            apiParam.setEnd(DateUtils.parse("yyyy-MM-dd HH:mm:ss", end));
            fzzySyncService12.syncData(apiParam);
 
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            e.printStackTrace();
        }
        return "";
    }
 
 
    public static void main(String[] args) throws EncoderException {
        // 压缩前文件路径
        File source = new File("D:\\IGDS\\FILE\\COMMON\\202412\\20241218100601.mp4");
        // 压缩后的文件路径
        File target = new File("D:\\IGDS\\FILE\\COMMON\\202412\\202412181006.mp4");
        compre(source, target, 1);
 
    }
 
    /**
     * 视频压缩
     *
     * @param source 源文件
     * @param target 目标文件
     * @param rate   压缩比
     */
    public static void compre(File source, File target, Integer rate) throws EncoderException {
        try {
            System.out.println("---------------开始压缩---------------");
            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("mpeg4");
            //设置音频比特率,单位:b (比特率越高,清晰度/音质越好,当然文件也就越大 5600000 = 5600kb)
            // video.setBitRate(new Integer(5_600_000 / rate));
            video.setBitRate(new Integer(10 / rate));
 
            // 设置视频帧率(帧率越低,视频会出现断层,越高让人感觉越连续),这里 除1000是为了单位转换
            video.setFrameRate(new Integer(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);
 
 
            System.out.println("---------------结束压缩---------------");
            long end = System.currentTimeMillis();
            System.out.println("压缩前大小:" + source.length() + " 压缩后大小:" + target.length());
            System.out.println("压缩耗时:" + (end - start));
 
        } catch (EncoderException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
 
    }
 
    /**
     * 传视频File对象(这是一个具体的文件),返回压缩后File对象信息
     *
     * @param source
     */
    public static File compressionVideo(File source, String picName) {
        if (source == null) {
            return null;
        }
        String newPath = source.getAbsolutePath().substring(0, source.getAbsolutePath().lastIndexOf(File.separator)).concat(File.separator).concat(picName);
        File target = new File(newPath);
        try {
            MultimediaObject object = new MultimediaObject(source);
            AudioInfo audioInfo = object.getInfo().getAudio();
            // 根据视频大小来判断是否需要进行压缩,
            int maxSize = 5;
            double mb = Math.ceil(source.length() / 1048576);
            int second = (int) object.getInfo().getDuration() / 1000;
            BigDecimal bd = new BigDecimal(String.format("%.4f", mb / second));
            System.out.println("开始压缩视频了--> 视频每秒平均 " + bd + " MB ");
            // 视频 > 5MB, 或者每秒 > 0.5 MB 才做压缩, 不需要的话可以把判断去掉
            boolean temp = mb > maxSize || bd.compareTo(new BigDecimal(0.5)) > 0;
//            if(temp){
            long time = System.currentTimeMillis();
            //TODO 视频属性设置
            int maxBitRate = 128000;
            int maxSamplingRate = 44100;
            int bitRate = 800000;
            int maxFrameRate = 20;
            int maxWidth = 1280;
 
            AudioAttributes audio = new AudioAttributes();
            // 设置通用编码格式10                   audio.setCodec("aac");
            // 设置最大值:比特率越高,清晰度/音质越好
            // 设置音频比特率,单位:b (比特率越高,清晰度/音质越好,当然文件也就越大 128000 = 182kb)
            if (audioInfo.getBitRate() > maxBitRate) {
                audio.setBitRate(new Integer(maxBitRate));
            }
 
            // 设置重新编码的音频流中使用的声道数(1 =单声道,2 = 双声道(立体声))。如果未设置任何声道值,则编码器将选择默认值 0。
            audio.setChannels(audioInfo.getChannels());
            // 采样率越高声音的还原度越好,文件越大
            // 设置音频采样率,单位:赫兹 hz
            // 设置编码时候的音量值,未设置为0,如果256,则音量值不会改变
            // audio.setVolume(256);
            if (audioInfo.getSamplingRate() > maxSamplingRate) {
                audio.setSamplingRate(maxSamplingRate);
            }
 
            //TODO 视频编码属性配置
            ws.schild.jave.info.VideoInfo videoInfo = object.getInfo().getVideo();
            VideoAttributes video = new VideoAttributes();
            video.setCodec("h264");
            //设置音频比特率,单位:b (比特率越高,清晰度/音质越好,当然文件也就越大 800000 = 800kb)
            if (videoInfo.getBitRate() > bitRate) {
                video.setBitRate(bitRate);
            }
 
            // 视频帧率:15 f / s  帧率越低,效果越差
            // 设置视频帧率(帧率越低,视频会出现断层,越高让人感觉越连续),视频帧率(Frame rate)是用于测量显示帧数的量度。所谓的测量单位为每秒显示帧数(Frames per Second,简:FPS)或“赫兹”(Hz)。
            if (videoInfo.getFrameRate() > maxFrameRate) {
                video.setFrameRate(maxFrameRate);
            }
 
            // 限制视频宽高
            int width = videoInfo.getSize().getWidth();
            int height = videoInfo.getSize().getHeight();
            if (width > maxWidth) {
                float rat = (float) width / maxWidth;
                video.setSize(new VideoSize(maxWidth, (int) (height / rat)));
            }
 
            EncodingAttributes attr = new EncodingAttributes();
//                attr.setFormat("mp4");
            attr.setAudioAttributes(audio);
            attr.setVideoAttributes(video);
 
            // 速度最快的压缩方式, 压缩速度 从快到慢: ultrafast, superfast, veryfast, faster, fast, medium,  slow, slower, veryslow and placebo.
//                attr.setPreset(PresetUtil.VERYFAST);
//                attr.setCrf(27);
//                // 设置线程数
//                attr.setEncodingThreads(Runtime.getRuntime().availableProcessors()/2);
 
            Encoder encoder = new Encoder();
            encoder.encode(new MultimediaObject(source), target, attr);
            System.out.println("压缩总耗时:" + (System.currentTimeMillis() - time) / 1000);
            return target;
//            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (target.length() > 0) {
                source.delete();
            }
        }
        return source;
    }
}