Commit 725262b1 authored by yanzg's avatar yanzg

视频压缩

parent 0e2090d2
...@@ -123,6 +123,35 @@ public class MediaParameter extends BaseVo { ...@@ -123,6 +123,35 @@ public class MediaParameter extends BaseVo {
@ApiModelProperty(notes = "视频比特率压缩比,算法为: 最终比特率= Math.max(minAudioBitRate,audioBitRate * audioBitRateZip)") @ApiModelProperty(notes = "视频比特率压缩比,算法为: 最终比特率= Math.max(minAudioBitRate,audioBitRate * audioBitRateZip)")
private float videoBitRateZip = 0; private float videoBitRateZip = 0;
/**
* 默认构造函数
*/
public MediaParameter() {
}
/**
* 视频压缩
*
* @param videoSizeZip 宽,高,压缩比
* @param videoBitRateZip 比特率压缩比,相当于视频质量
*/
public MediaParameter(float videoSizeZip, float videoBitRateZip) {
this.videoSizeZip = videoSizeZip;
this.videoBitRateZip = videoBitRateZip;
}
/**
* 视频压缩
*
* @param audioBitRateZip 音频质量压缩比
* @param videoSizeZip 宽,高,压缩比
* @param videoBitRateZip 比特率压缩比,相当于视频质量
*/
public MediaParameter(float audioBitRateZip, float videoSizeZip, float videoBitRateZip) {
this.audioBitRateZip = audioBitRateZip;
this.videoSizeZip = videoSizeZip;
this.videoBitRateZip = videoBitRateZip;
}
/** /**
* 音频最终比特率 * 音频最终比特率
...@@ -317,6 +346,7 @@ public class MediaParameter extends BaseVo { ...@@ -317,6 +346,7 @@ public class MediaParameter extends BaseVo {
this.videoBitRateZip = StringHelper.getFirst(this.videoBitRateZip, 0.1f); this.videoBitRateZip = StringHelper.getFirst(this.videoBitRateZip, 0.1f);
// 设置压缩大小 // 设置压缩大小
this.videoSizeZip = StringHelper.getFirst(this.videoSizeZip, 0.1f); this.videoSizeZip = StringHelper.getFirst(this.videoSizeZip, 0.1f);
// 设置音频最小比特率 // 设置音频最小比特率
this.minAudioBitRate = StringHelper.getFirst(this.minAudioBitRate, 48043); this.minAudioBitRate = StringHelper.getFirst(this.minAudioBitRate, 48043);
} }
...@@ -330,7 +360,7 @@ public class MediaParameter extends BaseVo { ...@@ -330,7 +360,7 @@ public class MediaParameter extends BaseVo {
// 设置最小高度,指的是宽度和高度中的小值,宽度>高度 // 设置最小高度,指的是宽度和高度中的小值,宽度>高度
this.minVideoHeight = StringHelper.getFirst(this.minVideoHeight, 360); this.minVideoHeight = StringHelper.getFirst(this.minVideoHeight, 360);
// 设置视频最小比特率 // 设置视频最小比特率
this.minVideoBitRate = StringHelper.getFirst(this.minAudioBitRate, 295581); this.minVideoBitRate = StringHelper.getFirst(this.minVideoBitRate, 295581);
} }
/** /**
...@@ -341,6 +371,8 @@ public class MediaParameter extends BaseVo { ...@@ -341,6 +371,8 @@ public class MediaParameter extends BaseVo {
this.minVideoWidth = StringHelper.getFirst(this.minVideoWidth, 800); this.minVideoWidth = StringHelper.getFirst(this.minVideoWidth, 800);
// 设置最小高度,指的是宽度和高度中的小值,宽度>高度 // 设置最小高度,指的是宽度和高度中的小值,宽度>高度
this.minVideoHeight = StringHelper.getFirst(this.minVideoHeight, 600); this.minVideoHeight = StringHelper.getFirst(this.minVideoHeight, 600);
// 设置视频最小比特率
this.minVideoBitRate = StringHelper.getFirst(this.minVideoBitRate, 600000);
} }
/** /**
...@@ -350,7 +382,7 @@ public class MediaParameter extends BaseVo { ...@@ -350,7 +382,7 @@ public class MediaParameter extends BaseVo {
// 设置最小宽度,指的是宽度和高度中的大值,宽度>高度 // 设置最小宽度,指的是宽度和高度中的大值,宽度>高度
this.minVideoWidth = StringHelper.getFirst(this.minVideoWidth, 960); this.minVideoWidth = StringHelper.getFirst(this.minVideoWidth, 960);
// 设置视频最小比特率 // 设置视频最小比特率
this.minVideoBitRate = StringHelper.getFirst(this.minAudioBitRate, 2955811); this.minVideoBitRate = StringHelper.getFirst(this.minVideoBitRate, 2955811);
} }
/** /**
...@@ -362,7 +394,7 @@ public class MediaParameter extends BaseVo { ...@@ -362,7 +394,7 @@ public class MediaParameter extends BaseVo {
// 设置最小高度,指的是宽度和高度中的小值,宽度>高度 // 设置最小高度,指的是宽度和高度中的小值,宽度>高度
this.minVideoHeight = StringHelper.getFirst(this.minVideoHeight, 1080); this.minVideoHeight = StringHelper.getFirst(this.minVideoHeight, 1080);
// 设置视频最小比特率 // 设置视频最小比特率
this.minVideoBitRate = StringHelper.getFirst(this.minAudioBitRate, 8311136); this.minVideoBitRate = StringHelper.getFirst(this.minVideoBitRate, 8311136);
} }
/** /**
......
...@@ -36,7 +36,7 @@ public class TestMediaHelper { ...@@ -36,7 +36,7 @@ public class TestMediaHelper {
public void testConvert() throws IOException { public void testConvert() throws IOException {
for (String fileName : fileNames) { for (String fileName : fileNames) {
for (int i = 0; i < sizes.length; i++) { for (int i = 0; i < sizes.length; i++) {
testConvert(fileName, sizes[i], quotes[i]); testConvert(fileName, new MediaParameter(sizes[i], quotes[i]));
} }
} }
} }
...@@ -45,7 +45,7 @@ public class TestMediaHelper { ...@@ -45,7 +45,7 @@ public class TestMediaHelper {
public void testConvertSingle() throws IOException { public void testConvertSingle() throws IOException {
String fileName = "100M.mp4"; String fileName = "100M.mp4";
// String fileName = "z001594372388232a3017ad69c82a342.MOV"; // String fileName = "z001594372388232a3017ad69c82a342.MOV";
testConvert(fileName, 0.25f, 0.10f); testConvert(fileName, new MediaParameter(0.25f, 0.10f));
} }
...@@ -53,27 +53,38 @@ public class TestMediaHelper { ...@@ -53,27 +53,38 @@ public class TestMediaHelper {
public void testMinZipSingle() throws IOException { public void testMinZipSingle() throws IOException {
String fileName = "100M.mp4"; String fileName = "100M.mp4";
// String fileName = "z001594372388232a3017ad69c82a342.MOV"; // String fileName = "z001594372388232a3017ad69c82a342.MOV";
testConvert(fileName, 0.01f, 0.01f); testConvert(fileName, new MediaParameter(0.01f, 0.01f));
}
/**
* 按照微信的压缩标准
*
* @throws IOException
*/
@Test
public void testMinZipWeixin() throws IOException {
String fileName = "100M.mp4";
// String fileName = "z001594372388232a3017ad69c82a342.MOV";
MediaParameter parameter = new MediaParameter(0.01f, 0.01f);
parameter.initZip1920_1080();
testConvert(fileName, parameter);
} }
@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";
// String fileName = "z001594372388232a3017ad69c82a342.MOV"; // String fileName = "z001594372388232a3017ad69c82a342.MOV";
testConvert(fileName, 0.5f, 0.5f); testConvert(fileName, new MediaParameter(0.5f, 0.5f));
} }
private void testConvert(String fileName, float size, float quote) throws IOException { private void testConvert(String fileName, MediaParameter parameter) throws IOException {
if (parameter == null) {
parameter = new MediaParameter();
}
String file = getFile(fileName); String file = getFile(fileName);
String targetFile = getTargetFile("target/" + fileName); String targetFile = getTargetFile("target/" + fileName);
String toFile = String.format("%s.size_%d.quot_%d.mp4", targetFile, (int) (parameter.getVideoSizeZip() * 100), (int) (parameter.getVideoBitRateZip() * 100));
String toFile = String.format("%s.size_%d.quot_%d.mp4", targetFile, (int) (size * 100), (int) (quote * 100));
MediaParameter parameter = new MediaParameter();
parameter.setVideoSizeZip(size);
parameter.setVideoBitRateZip(quote);
MediaHelper.zipVideoMp4(file, toFile, parameter); MediaHelper.zipVideoMp4(file, toFile, parameter);
} }
} }
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