CompressionVideo.java
7.09 KB
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
package cn.com.glsx.entmicro.util;
import lombok.extern.slf4j.Slf4j;
import ws.schild.jave.Encoder;
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.math.BigDecimal;
/**
* @Author yelq
* @Version 1.0
* @Description: 视频压缩(针对企微朋友圈接口压缩)
* @Date 2022/9/24 14:24
*/
@Slf4j
public class CompressionVideo {
/**
* 传视频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 = 10;
double mb = Math.ceil(source.length() / 1048576D);
int second = (int) object.getInfo().getDuration() / 1000;
BigDecimal bd = new BigDecimal(String.format("%.4f", mb / second));
log.info("开始压缩视频了--> 视频每秒平均 " + bd + " MB ");
// 视频 > 10MB, 或者每秒 > 3.5 MB 才做压缩, 不需要的话可以把判断去掉
boolean temp = mb > maxSize || bd.compareTo(new BigDecimal("3.3333")) > 0;
if (temp) {
long time = System.currentTimeMillis();
//TODO 视频属性设置
int maxBitRate = 128000; // 最大比特率
int maxSamplingRate = 44100; //音频采样率
int bitRate = 800000; // 比特率
int maxFrameRate = 30; //帧率30FPS
int maxWidth = 720; // 最大宽
int maxHeight = 1280; // 最大高
AudioAttributes audio = new AudioAttributes();
// 设置通用编码格式10
// audio.setCodec("aac");
// 设置最大值:比特率越高,清晰度/音质越好
// 设置音频比特率,单位:b (比特率越高,清晰度/音质越好,当然文件也就越大 128000 = 182kb)
if (audioInfo.getBitRate() > maxBitRate) {
audio.setBitRate(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 && height <= maxHeight) {
float rat = (float) width / maxWidth;
//视频参数必须是2的倍数,所以减一是为了取偶数
video.setSize(new VideoSize(maxWidth, (int) (height / rat)%2==0?(int) (height / rat):(int) (height / rat)-1));
} else if (width <= maxWidth && height > maxHeight) {
float rat = (float) height / maxHeight;
video.setSize(new VideoSize((int) (width / rat)%2==0?(int) (width / rat):(int) (width / rat)-1, maxHeight));
} else if (width > maxWidth) {
float rat1 = (float) width / maxWidth;
float rat2 = (float) height / maxHeight;
if (rat1>=rat2){
video.setSize(new VideoSize(maxWidth, (int) (height / rat1)%2==0?(int) (height / rat1):(int) (height / rat1)-1));
}else {
video.setSize(new VideoSize((int) (width / rat2)%2==0?(int) (width / rat2):(int) (width / rat2)-1, maxHeight));
}
}
EncodingAttributes attr = new EncodingAttributes();
// attr.setOutputFormat("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.setDecodingThreads(Runtime.getRuntime().availableProcessors()/2);
//用于转换的最大内核/cpu数。未设置意味着我们使用ffmpeg的默认值
// attr.setFilterThreads(3);
// 设置线程数 用于编码的线程数(如果编解码器支持)
attr.setEncodingThreads(Runtime.getRuntime().availableProcessors()/2);
Encoder encoder = new Encoder();
encoder.encode(new MultimediaObject(source), target, attr);
log.info("压缩总耗时:" + (System.currentTimeMillis() - time) / 1000);
return target;
}
} catch (Exception e) {
log.info("视频压缩失败:{}",e);
new Thread(target::delete).start();
} finally {
if (target.length() > 0) {
new Thread(source::delete).start();
}
}
return source;
}
}