package api.common.util;

import lombok.SneakyThrows;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;

public class FfmpegUtil {

    /**
     * @param sourcePath 原始图片路径
     * @param videoPath  视频生成路径
     * @param filename   视频文件名称
     * @param ffmpeg  视频软件路径
     */
    @SneakyThrows
    public static void imgToMp4(String sourcePath, String videoPath, String filename, double inputFrameRate, String ffmpeg) {
        int nameLength = copyImgToTemp(sourcePath);
        String execute = LinuxUtil.execute(ffmpeg
                + " -r " + inputFrameRate
                + " -i " + FileUtil.addSeparator(sourcePath) + "tmp/image%0" + nameLength + "d.tga"
                + " -y "
                + videoPath + "/" + filename + ".mp4"
        );
    }

    /**
     * 计算帧率
     *
     * @param imageNumber   图片数量
     * @param second        视频长度
     * @param decimalPlaces 帧率保留几位小数
     * @return 帧率
     */
    public static double computeFramerate(int imageNumber, int second, int decimalPlaces) {
        return Math.round(imageNumber * Math.pow(10.0, decimalPlaces) / second) / Math.pow(10.0, decimalPlaces);
    }

    private static int copyImgToTemp(String sourcePath) throws IOException {
        File file = new File(sourcePath);
        File[] files = file.listFiles();
        assert files != null;
        Arrays.sort(files);
        String[] names = file.list();
        assert names != null;
        Arrays.sort(names);
        int nameLength = getLen(names.length);//获取图片总张数的位数大小，用于补齐图片名称，例如image000.jpg,image001.jpg ....image999.jpg
        int makeUpMaxNum = getMakeUpMaxNum(nameLength);//图片名字需要补齐的最大数
        if (names.length > 0) {
            String tmpPath = sourcePath + "/tmp";
            new File(tmpPath).mkdir();
            for (int i = 0; i < names.length; i++) {
                String name = names[i];
                if (name.lastIndexOf(".") > 0) {
                    int lo = name.lastIndexOf(".");
                    String lastName = name.substring(lo);
                    String forNeName = String.valueOf(i);
                    if (i <= makeUpMaxNum) {
                        StringBuilder zeString = new StringBuilder();
                        for (int j = 0; j < nameLength - forNeName.length(); j++) {
                            zeString.append("0");
                        }
                        forNeName = "image" + zeString + forNeName + lastName;
                    } else {
                        forNeName = "image" + forNeName + lastName;
                    }
                    FileUtils.copyFile(files[i], new File(tmpPath + "/" + forNeName));
                }
            }
        }
        return nameLength;
    }

    public static int getLen(int x) {
        if (x < 10) return 1;
        return getLen(x / 10) + 1;
    }

    public static int getMakeUpMaxNum(int len) {
        int num = 1;
        for (int i = 1; i < len; i++) {
            num = num * 10;
        }
        return num - 1;
    }

}
