import ink.wgink.module.file.media.manager.MediaManager;
import ink.wgink.module.file.media.manager.domain.enums.CrfValueEnum;
import ink.wgink.module.file.media.manager.domain.enums.PresetVauleEnum;
import ink.wgink.module.file.media.manager.process.IMediaStream;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Time;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: MediaTest
 * @Description:
 * @Author: WangGeng
 * @Date: 2021/6/7 22:37
 * @Version: 1.0
 **/
public class MediaTest {
    static long fullTime = 0L;
    static long currentTime = 0L;

    @Test
    public void t1() {
        File videoFile = new File("I:\\电视剧\\神探狄仁杰\\第一部\\神探狄仁杰-01.mp4");
        File frameFile = new File("C:\\Users\\wenc0\\Desktop\\UploadFiles\\frame.gif");
        MediaManager instance = MediaManager.getInstance();
        instance.setFFmpegPath("D:\\ffmpeg-4.4-full_build\\ffmpeg-4.4-full_build\\bin\\ffmpeg.exe");
        instance.cutVideoFrame(videoFile, frameFile, new Time(0,0,30));
    }

    @Test
    public void t2() throws Exception {
        Process process = Runtime.getRuntime().exec("node -v");
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"));
        new Thread(() -> {
            try {
                for (String line; (line = bufferedReader.readLine()) != null; ) {
                    System.out.println(line);
                }
                bufferedReader.close();
            } catch (Exception e) {
            }
        }).start();
        process.waitFor();
        process.destroy();
    }

    @Test
    public void t3() {
        MediaManager.getInstance().setFFmpegPath("/Users/wanggeng/ffmpeg/ffmpeg");
        String sourcePath = "/Users/wanggeng/Desktop/UploadFiles";
        String sourceName = "720P_4000K_302139672.mp4";
        File sourceFile = new File(sourcePath + File.separator + sourceName);
        File outFile = new File(sourcePath + File.separator + sourceName + ".mp4");

        Pattern durationPattern = Pattern.compile("Duration: \\d{2}:\\d{2}:\\d{2}");
        Pattern timePattern = Pattern.compile("time=\\d{2}:\\d{2}:\\d{2}");

        MediaManager.getInstance().convertVideo(sourceFile, outFile, true, CrfValueEnum.LOW_QUALITY.getCode(), PresetVauleEnum.MAX_FAST_ZIP_SPEED.getPresetValue(), null, null, new IMediaStream() {
            @Override
            public void input(InputStream inputStream) throws Exception {
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                for (String line; (line = bufferedReader.readLine()) != null; ) {
                    System.out.println(line);
                }
            }

            @Override
            public void error(InputStream errorStream) throws Exception {
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(errorStream));
                for (String line; (line = bufferedReader.readLine()) != null; ) {
                    System.out.println(line);
                    Matcher durationMatcher = durationPattern.matcher(line);
                    Matcher timeMatcher = timePattern.matcher(line);
                    if (durationMatcher.find()) {
                        String duration = durationMatcher.group();
//                        System.out.println(duration);
                        String durationTime = duration.replace("Duration: ", "");
                        fullTime = durationToLongTime(durationTime);
                    }
                    if (timeMatcher.find()) {
                        String time = timeMatcher.group();
//                        System.out.println(time);
                        String timeTime = time.replace("time=", "");
                        currentTime = durationToLongTime(timeTime);
                    }
//                    System.out.println(fullTime + "-" + currentTime);
//                    if (fullTime > 0L) {
//                        System.out.println((currentTime / (fullTime * 1D) * 100D) + "%");
//                    }
                }
                System.out.println("123123");
            }

        });
    }

    private long durationToLongTime(String duration) {
        String[] durationArray = duration.split("\\:");
        int hour = Integer.parseInt(durationArray[0]);
        int minute = Integer.parseInt(durationArray[1]);
        int second = Integer.parseInt(durationArray[2]);
        return (hour * 3600 + minute * 60 + second);
    }

    @Test
    public void t4() {
        String line = "  Duration: 00:14:05.11, start: 0.000000, bitrate: 1022 kb/s";
        String line2 = "frame=    1 fps=0.0 q=0.0 size=       0kB time=00:00:00.13 bitrate=   2.8kbits/s speed=3.58x    ";
        Pattern durationPattern = Pattern.compile("Duration: \\d{2}:\\d{2}:\\d{2}\\.\\d{2}");
        Pattern timePattern = Pattern.compile("time=\\d{2}:\\d{2}:\\d{2}\\.\\d{2}");
        Matcher durationMatcher = durationPattern.matcher(line);
        Matcher timeMatcher = timePattern.matcher(line2);
        while (durationMatcher.find()) {
            String count = durationMatcher.group();
            System.out.println(count);
        }
        while (timeMatcher.find()) {
            String count = timeMatcher.group();
            System.out.println(count);
        }
    }


}
