package helper; import com.yanzuoguang.util.MediaHelper; import com.yanzuoguang.util.thread.RunnableListAuto; import it.sauronsoftware.jave.Encoder; import it.sauronsoftware.jave.EncoderException; import org.junit.Test; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class TestMediaHelper { private boolean isFirstImage = false; 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}; @Test public void printEncoding() throws EncoderException { Encoder encoder = new Encoder(); String[] audioEncoders = encoder.getVideoEncoders(); for (String item : audioEncoders) { System.out.println(item); } } @Test public void testConvert() throws EncoderException { Encoder encoder = new Encoder(); String[] audioEncoders = encoder.getVideoEncoders(); List list = new ArrayList<>(); for (String item : audioEncoders) { list.add(testConvertThread(item, 1f, 1f)); } RunnableListAuto.run(list); } private Runnable testConvertThread(String item, float size, float quote) { return new Runnable() { @Override public void run() { try { testConvert(item, size, quote); } catch (EncoderException e) { throw new RuntimeException(e); } } }; } private void testConvert(String item, float size, float quote) throws EncoderException { String file = getFile(); String targetFile = getTargetFile(); String name = String.format("%s.size_%d.quot_%d.%s.mp4", targetFile, (int) (size * 100), (int) (quote * 100), item); MediaHelper.zipVideo(file, name, "mp4", item, size, (int) (MediaHelper.DEFAULT_BIT_RATE * quote)); } private String getFile() { // 注意,路径应为文件在工程中的相对路径 File f = new File("src/test/java/helper/100M.mp4"); if (!f.exists()) { System.out.println(f.getAbsoluteFile()); throw new RuntimeException("视频文件不存在"); } return f.getAbsolutePath(); } private String getTargetFile() { // 注意,路径应为文件在工程中的相对路径 File f = new File("target/100M.mp4"); return f.getAbsolutePath(); } @Test public void testVideoFirstImage() throws IOException { if (isFirstImage) { return; } String file = getFile(); String targetFile = getTargetFile(); MediaHelper.getVideoFirstImage(file, targetFile + ".jpg"); isFirstImage = true; } @Test public void testImageZip() throws IOException { // 生成测试图片 testVideoFirstImage(); List list = new ArrayList<>(); for (int i = 0; i < sizes.length; i++) { list.add(testImageZipThread(sizes[i], quotes[i])); } RunnableListAuto.run(list); } private Runnable testImageZipThread(float size, float quote) { return new Runnable() { @Override public void run() { try { testImageZip(size, quote); } catch (IOException e) { throw new RuntimeException(e); } } }; } private void testImageZip(float size, float quote) throws IOException { String file = getTargetFile() + ".jpg"; String targetFile = getTargetFile(); String toName = String.format("%s.size_%d.quot_%d.jpg", targetFile, (int) (size * 100), (int) (quote * 100)); String toNameSystem = String.format("%s.size_%d.quot_%d.system.jpg", targetFile, (int) (size * 100), (int) (quote * 100)); MediaHelper.compressPic(file, toName, quote, size); MediaHelper.compressPicBySystem(file, toNameSystem, quote, size); } @Test public void testVideoZipFlv() { List list = new ArrayList<>(); for (int i = 0; i < sizes.length; i++) { list.add(testVideoZipFlvThread(sizes[i], quotes[i])); } RunnableListAuto.run(list); } private Runnable testVideoZipFlvThread(float size, float quote) { return new Runnable() { @Override public void run() { try { testVideoZipFlv(size, quote); } catch (EncoderException e) { throw new RuntimeException(e); } } }; } private void testVideoZipFlv(float size, float quote) throws EncoderException { String file = getFile(); String targetFile = getTargetFile(); String name = String.format("%s.size_%d.quot_%d.flv", targetFile, (int) (size * 100), (int) (quote * 100)); MediaHelper.zipVideoFlv(file, name, size, (int) (MediaHelper.DEFAULT_BIT_RATE * quote)); } @Test public void testVideoZipMpeg_50_50() throws EncoderException { testVideoZipMpeg(0.5f, 0.5f); } @Test public void testVideoZipMpeg() throws EncoderException { List list = new ArrayList<>(); for (int i = 0; i < sizes.length; i++) { list.add(testVideoZipMpegThread(sizes[i], quotes[i])); } RunnableListAuto.run(list); } private Runnable testVideoZipMpegThread(float size, float quote) { return new Runnable() { @Override public void run() { try { testVideoZipMpeg(size, quote); } catch (EncoderException e) { throw new RuntimeException(e); } } }; } private void testVideoZipMpeg(float size, float quote) throws EncoderException { String file = getFile(); String targetFile = getTargetFile(); String name = String.format("%s.size_%d.quot_%d.mpeg.mp4", targetFile, (int) (size * 100), (int) (quote * 100)); MediaHelper.zipVideoMpeg(file, name, size, (int) (MediaHelper.DEFAULT_BIT_RATE * quote)); } @Test public void testVideoZip3pg() throws EncoderException { List list = new ArrayList<>(); for (int i = 0; i < sizes.length; i++) { list.add(testVideoZip3pgThread(sizes[i], quotes[i])); } RunnableListAuto.run(list); } private Runnable testVideoZip3pgThread(float size, float quote) { return new Runnable() { @Override public void run() { try { testVideoZip3pg(size, quote); } catch (EncoderException e) { throw new RuntimeException(e); } } }; } private void testVideoZip3pg(float size, float quote) throws EncoderException { String file = getFile(); String targetFile = getTargetFile(); String name = String.format("%s.size_%d.quot_%d.3pg", targetFile, (int) (size * 100), (int) (quote * 100)); MediaHelper.zipVideo3pg(file, name, size, (int) (MediaHelper.DEFAULT_BIT_RATE * quote)); } @Test public void testVideoZip_50_50() throws EncoderException { testVideoZip(0.5f, 0.5f); } @Test public void testVideoZip() throws EncoderException { List list = new ArrayList<>(); for (int i = 0; i < sizes.length; i++) { list.add(testVideoZipThread(sizes[i], quotes[i])); } RunnableListAuto.run(list); } private Runnable testVideoZipThread(float size, float quote) { return new Runnable() { @Override public void run() { try { testVideoZip(size, quote); } catch (EncoderException e) { throw new RuntimeException(e); } } }; } private void testVideoZip(float size, float quote) throws EncoderException { String file = getFile(); String targetFile = getTargetFile(); String name = String.format("%s.size_%d.quot_%d.mp4", targetFile, (int) (size * 100), (int) (quote * 100)); MediaHelper.zipVideo(file, name, size, (int) (MediaHelper.DEFAULT_BIT_RATE * quote)); } }