Commit 7715ebec authored by yanzg's avatar yanzg

视频压缩

parent 259d179a
...@@ -130,7 +130,7 @@ public class MediaParameter extends BaseVo { ...@@ -130,7 +130,7 @@ public class MediaParameter extends BaseVo {
* @return * @return
*/ */
public int getAudioBitRateFinally() { public int getAudioBitRateFinally() {
return Math.max(this.minAudioBitRate, (int) (this.audioBitRate * this.audioBitRateZip)); return getMinValue(this.minAudioBitRate, this.audioBitRate, this.audioBitRateZip);
} }
/** /**
...@@ -139,7 +139,7 @@ public class MediaParameter extends BaseVo { ...@@ -139,7 +139,7 @@ public class MediaParameter extends BaseVo {
* @return * @return
*/ */
public int getVideoBitRateFinally() { public int getVideoBitRateFinally() {
return Math.max(this.minVideoBitRate, (int) (this.videoBitRate * this.videoBitRateZip)); return getMinValue(this.minVideoBitRate, this.videoBitRate, this.videoBitRateZip);
} }
/** /**
...@@ -161,10 +161,46 @@ public class MediaParameter extends BaseVo { ...@@ -161,10 +161,46 @@ public class MediaParameter extends BaseVo {
} }
private int getWidthHeightFinally(boolean width) { private int getWidthHeightFinally(boolean width) {
// 最大宽度和最大高度
int maxWidth = Math.max(this.videoWidth, this.videoHeight);
int maxHeight = Math.min(this.videoWidth, this.videoHeight);
// 获取允许的最小宽度和高度
int minWidth = Math.max(this.minVideoWidth, this.minVideoHeight);
int minHeight = Math.min(this.minVideoWidth, this.minVideoHeight);
// 计算最小宽度和高度
int calcMinWidth = maxWidth * minHeight / maxHeight;
int calcMinHeight = maxHeight * minWidth / maxWidth;
// 设置最小的宽高
if (minWidth == 0) {
minWidth = calcMinWidth;
calcMinHeight = minHeight;
}
if (minHeight == 0) {
minHeight = calcMinHeight;
calcMinWidth = minWidth;
}
// 判断宽高比列
if (calcMinHeight > minHeight) {
minHeight = calcMinHeight;
} else {
minWidth = calcMinWidth;
}
if (width) { if (width) {
return Math.max(this.minVideoWidth, (int) (this.videoWidth * this.videoSizeZip)); int minValue = this.videoWidth > this.videoHeight ? minWidth : minHeight;
return getMinValue(minValue, this.videoWidth, this.videoSizeZip);
} else {
int minValue = this.videoWidth < this.videoHeight ? minWidth : minHeight;
return getMinValue(minValue, this.videoHeight, this.videoSizeZip);
}
}
private int getMinValue(int min, int from, float zip) {
if (from < min) {
return from;
} else { } else {
return Math.max(this.minVideoHeight, (int) (this.videoHeight * this.videoSizeZip)); return Math.max(min, (int) (from * zip));
} }
} }
...@@ -271,16 +307,67 @@ public class MediaParameter extends BaseVo { ...@@ -271,16 +307,67 @@ public class MediaParameter extends BaseVo {
// recorder.setVideoQuality(this.videoBitRateZip); // recorder.setVideoQuality(this.videoBitRateZip);
} }
public void initZipMp4() { /**
this.initMp4(); * 设置最小压缩比特率
*/
public void initZipBitrate() {
// 设置视频压缩率 // 设置视频压缩率
this.audioBitRateZip = StringHelper.getFirst(this.audioBitRateZip, 0.25f); this.audioBitRateZip = StringHelper.getFirst(this.audioBitRateZip, 0.25f);
// 设置压缩大小
this.videoSizeZip = StringHelper.getFirst(this.videoSizeZip, 0.25f);
// 设置视频压缩率 // 设置视频压缩率
this.videoBitRateZip = StringHelper.getFirst(this.videoBitRateZip, 0.25f); this.videoBitRateZip = StringHelper.getFirst(this.videoBitRateZip, 0.1f);
// 设置压缩大小
this.videoSizeZip = StringHelper.getFirst(this.videoSizeZip, 0.1f);
// 设置音频最小比特率
this.minAudioBitRate = StringHelper.getFirst(this.minAudioBitRate, 48043);
} }
/**
* 设置宽高最小480 *360
*/
public void initZip480_360() {
// 设置最小宽度,指的是宽度和高度中的大值,宽度>高度
this.minVideoWidth = StringHelper.getFirst(this.minVideoWidth, 480);
// 设置最小高度,指的是宽度和高度中的小值,宽度>高度
this.minVideoHeight = StringHelper.getFirst(this.minVideoHeight, 360);
// 设置视频最小比特率
this.minVideoBitRate = StringHelper.getFirst(this.minAudioBitRate, 295581);
}
/**
* 设置宽高最小800*600
*/
public void initZip800_600() {
// 设置最小宽度,指的是宽度和高度中的大值,宽度>高度
this.minVideoWidth = StringHelper.getFirst(this.minVideoWidth, 800);
// 设置最小高度,指的是宽度和高度中的小值,宽度>高度
this.minVideoHeight = StringHelper.getFirst(this.minVideoHeight, 600);
}
/**
* 设置宽最小960,高度根据宽度自适应
*/
public void initZip960() {
// 设置最小宽度,指的是宽度和高度中的大值,宽度>高度
this.minVideoWidth = StringHelper.getFirst(this.minVideoWidth, 960);
// 设置视频最小比特率
this.minVideoBitRate = StringHelper.getFirst(this.minAudioBitRate, 2955811);
}
/**
* 设置宽高最小1920*1080
*/
public void initZip1920_1080() {
// 设置最小宽度,指的是宽度和高度中的大值,宽度>高度
this.minVideoWidth = StringHelper.getFirst(this.minVideoWidth, 1920);
// 设置最小高度,指的是宽度和高度中的小值,宽度>高度
this.minVideoHeight = StringHelper.getFirst(this.minVideoHeight, 1080);
// 设置视频最小比特率
this.minVideoBitRate = StringHelper.getFirst(this.minAudioBitRate, 8311136);
}
/**
* 抓码为mp4
*/
public void initMp4() { public void initMp4() {
this.audioCodec = SOUND_CODEC_MP3; this.audioCodec = SOUND_CODEC_MP3;
this.videoCodeC = VIDEO_CODEC_MP4; this.videoCodeC = VIDEO_CODEC_MP4;
...@@ -288,6 +375,18 @@ public class MediaParameter extends BaseVo { ...@@ -288,6 +375,18 @@ public class MediaParameter extends BaseVo {
this.videoPixelFormat = VIDEO_FIXED_MP4; this.videoPixelFormat = VIDEO_FIXED_MP4;
} }
/**
* 压缩mp4
*/
public void initZipMp4() {
// 设置结果为mp4
this.initMp4();
// 设置最小宽高
this.initZip960();
// 设置最小压缩比特率
this.initZipBitrate();
}
public int getAudioCodec() { public int getAudioCodec() {
return audioCodec; return audioCodec;
} }
......
...@@ -48,6 +48,14 @@ public class TestMediaHelper { ...@@ -48,6 +48,14 @@ public class TestMediaHelper {
testConvert(fileName, 0.25f, 0.10f); testConvert(fileName, 0.25f, 0.10f);
} }
@Test
public void testMinZipSingle() throws IOException {
String fileName = "100M.mp4";
// String fileName = "z001594372388232a3017ad69c82a342.MOV";
testConvert(fileName, 0.01f, 0.01f);
}
@Test @Test
public void testConvertSingle_Two() throws IOException { public void testConvertSingle_Two() throws IOException {
String fileName = "100M.mp4.size_50.quot_50.mp4"; String fileName = "100M.mp4.size_50.quot_50.mp4";
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment