TestMediaHelper.java 8.84 KB
Newer Older
yanzg's avatar
yanzg committed
1 2 3
package helper;

import com.yanzuoguang.util.MediaHelperExtend;
yanzg's avatar
yanzg committed
4
import com.yanzuoguang.util.helper.FileHelper;
yanzg's avatar
yanzg committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
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 testMediaType() throws EncoderException, IOException {
yanzg's avatar
yanzg committed
24 25 26 27
        System.out.println("100M.mp4   " + FileHelper.getMediaType(getFile("100M.mp4")));
        System.out.println("100M.mp4.jpg  " + FileHelper.getMediaType(getFile("100M.mp4.jpg")));
        System.out.println("xuziming.mp4  " + FileHelper.getMediaType(getFile("xuziming.mp4")));
        System.out.println("z001594372388232a3017ad69c82a342.MOV  " + FileHelper.getMediaType(getFile("z001594372388232a3017ad69c82a342.MOV")));
yanzg's avatar
yanzg committed
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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
    }

    @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<Runnable> 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("100M.mp4");
        String targetFile = getTargetFile("target/100M.mp4");
        String name = String.format("%s.size_%d.quot_%d.%s.mp4",
                targetFile, (int) (size * 100), (int) (quote * 100), item);
        MediaHelperExtend.zipVideo(file, name, "mp4", item, size, (int) (MediaHelperExtend.DEFAULT_BIT_RATE * quote));
    }


    private String getFile(String file) {
        // 注意,路径应为文件在工程中的相对路径
        File f = new File("src/test/java/helper/" + file);
        if (!f.exists()) {
            System.out.println(f.getAbsoluteFile());
            throw new RuntimeException("视频文件不存在");
        }
        return f.getAbsolutePath();
    }


    private String getTargetFile(String file) {
        // 注意,路径应为文件在工程中的相对路径
        File f = new File(file);
        return f.getAbsolutePath();
    }


    @Test
    public void testImageZip() throws IOException {
        List<Runnable> 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("target/100M.mp4") + ".jpg";
        String targetFile = getTargetFile("target/100M.mp4");
        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));
        MediaHelperExtend.compressPic(file, toName, quote, size);
        MediaHelperExtend.compressPicBySystem(file, toNameSystem, quote, size);
    }


    @Test
    public void testVideoZipFlv() {
        List<Runnable> 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("100M.mp4");
        String targetFile = getTargetFile("target/100M.mp4");
        String name = String.format("%s.size_%d.quot_%d.flv",
                targetFile, (int) (size * 100), (int) (quote * 100));
        MediaHelperExtend.zipVideoFlv(file, name, size, (int) (MediaHelperExtend.DEFAULT_BIT_RATE * quote));
    }


    @Test
    public void testVideoZipMpeg_50_50() throws EncoderException {
        testVideoZipMpeg(0.5f, 0.5f);
    }

    @Test
    public void testVideoZipMpeg() throws EncoderException {
        List<Runnable> 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("100M.mp4");
        String targetFile = getTargetFile("target/100M.mp4");
        String name = String.format("%s.size_%d.quot_%d.mpeg.mp4",
                targetFile, (int) (size * 100), (int) (quote * 100));
        MediaHelperExtend.zipVideoMpeg(file, name, size, (int) (MediaHelperExtend.DEFAULT_BIT_RATE * quote));
    }

    @Test
    public void testVideoZip3pg() throws EncoderException {
        List<Runnable> 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("100M.mp4");
        String targetFile = getTargetFile("target/100M.mp4");
        String name = String.format("%s.size_%d.quot_%d.3pg",
                targetFile, (int) (size * 100), (int) (quote * 100));
        MediaHelperExtend.zipVideo3pg(file, name, size, (int) (MediaHelperExtend.DEFAULT_BIT_RATE * quote));
    }


    @Test
    public void testVideoZip_50_50() throws EncoderException {
        testVideoZip(0.5f, 0.5f);
    }


    @Test
    public void testVideoZip() throws EncoderException {
        List<Runnable> 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("100M.mp4");
        String targetFile = getTargetFile("target/100M.mp4");
        String name = String.format("%s.size_%d.quot_%d.mp4",
                targetFile, (int) (size * 100), (int) (quote * 100));
        MediaHelperExtend.zipVideo(file, name, size, (int) (MediaHelperExtend.DEFAULT_BIT_RATE * quote));
    }
}