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
package helper;
import com.yanzuoguang.util.MediaHelper;
import com.yanzuoguang.util.MediaParameter;
import com.yanzuoguang.util.YzgError;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
public class TestMediaHelper {
public float[] sizes = new float[]{1f, 0.5f, 1f, 1f, 0.5f, 0.5f};
public float[] quotes = new float[]{1f, 1f, 0.5f, 0.25f, 0.5f, 0.25f};
public String[] fileNames = new String[]{"100M.mp4", "z001594372388232a3017ad69c82a342.MOV", "xuziming.mp4"};
private String getFile(String file) {
// 注意,路径应为文件在工程中的相对路径
File f = new File("src/test/java/helper/" + file);
if (!f.exists()) {
System.out.println(f.getAbsoluteFile());
throw YzgError.getRuntimeException("005");
}
return f.getAbsolutePath();
}
private String getTargetFile(String file) {
// 注意,路径应为文件在工程中的相对路径
File f = new File(file);
return f.getAbsolutePath();
}
@Test
public void testConvert() throws IOException {
for (String fileName : fileNames) {
for (int i = 0; i < sizes.length; i++) {
testConvert(fileName, new MediaParameter(sizes[i], quotes[i]));
}
}
}
@Test
public void testConvertSingle() throws IOException {
String fileName = "100M.mp4";
// String fileName = "z001594372388232a3017ad69c82a342.MOV";
testConvert(fileName, new MediaParameter(0.25f, 0.10f));
}
@Test
public void testMinZipSingle() throws IOException {
String fileName = "100M.mp4";
// String fileName = "z001594372388232a3017ad69c82a342.MOV";
testConvert(fileName, new MediaParameter(0.01f, 0.01f));
}
@Test
public void testVideoFirstImage() throws IOException {
String file = getFile("100M.mp4");
String targetFile = getTargetFile("target/100M.mp4");
MediaHelper.getVideoFirstImage(file, targetFile + ".jpg");
}
@Test
public void testVideoFirstImage1() throws IOException {
String file = getFile("xuziming.mp4");
String targetFile = getTargetFile("target/xuziming.mp4");
MediaHelper.getVideoFirstImage(file, targetFile + ".jpg");
}
/**
* 按照微信的压缩标准
*
* @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
public void testConvertSingle_Two() throws IOException {
String fileName = "100M.mp4.size_50.quot_50.mp4";
// String fileName = "z001594372388232a3017ad69c82a342.MOV";
testConvert(fileName, new MediaParameter(0.5f, 0.5f));
}
private void testConvert(String fileName, MediaParameter parameter) throws IOException {
if (parameter == null) {
parameter = new MediaParameter();
}
String file = getFile(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));
MediaHelper.zipVideoMp4(file, toFile, parameter);
}
}