/**
* TODO
*
* @author Administrator
* @version 1.0
* @date 2021/11/5 16:55
*/
public class vedio {
/**
* 傳視頻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();
// 根據(jù)視頻大小來判斷是否需要進行壓縮,
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 maxBitRate = 16000;
// int maxSamplingRate = 44100;
int maxSamplingRate = 16000;
int bitRate = 100000;
// int bitRate = 800000;
int maxFrameRate = 20;
// int maxWidth = 1280;
int maxWidth = 720;
AudioAttributes audio = new AudioAttributes();
// 設置通用編碼格式10 audio.setCodec("aac");
// 設置最大值:比特率越高,清晰度/音質越好
// 設置音頻比特率,單位:b (比特率越高,清晰度/音質越好,當然文件也就越大 128000 = 182kb)
if (audioInfo.getBitRate() > maxBitRate) {
audio.setBitRate(new Integer(maxBitRate));
}
// 設置重新編碼的音頻流中使用的聲道數(shù)(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 幀率越低,效果越差
// 設置視頻幀率(幀率越低,視頻會出現(xiàn)斷層,越高讓人感覺越連續(xù)),視頻幀率(Frame rate)是用于測量顯示幀數(shù)的量度。所謂的測量單位為每秒顯示幀數(shù)(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);
// // 設置線程數(shù)
// 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;
}
public static void main(String[] args) {
File f = new File("E://big.mp4");
File file = compressionVideo(f, "22.mp4");
}
}