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); } }