package shop.alien.util.common;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import shop.alien.util.system.OSUtil;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * 命令行调用FFMpeg截取图片
 *
 * @author ssk
 * @version 1.0
 * @date 2024/12/24 10:13
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class VideoUtils {

    /**
     * 视频截取图片
     *
     * @param videoFilePath 视频媒体文件
     */
    public String getImg(String videoFilePath) {
        log.info("VideoUtils.getImg videoFilePath={}", videoFilePath);
        String[] file = videoFilePath.split("\\.");
        log.info("VideoUtils.getImg file={}", file);
        // 截图保存位置
        String imgFilePath = file[0] + ".jpg";
        log.info("VideoUtils.getImg imgFilePath={}", imgFilePath);
        ProcessBuilder processBuilder = new ProcessBuilder();
        // 重定向错误流,防阻塞
        processBuilder.redirectErrorStream(true);

        // 如果为本地测试，ffmpegPath地址需求修改为本地安装程序的地址（环境变量不好用）
        String ffmpegPath = "ffmpeg";
        if ("windows".equals(OSUtil.getOsName())) {
            ffmpegPath = "C:/Program Files (x86)/ffmpeg-6.0/bin/ffmpeg.exe";
        }

        // 调用ffmpeg 执行截取命令,需要服务器中安装了ffmpeg并配置了环境变量
        processBuilder.command(ffmpegPath, "-i", videoFilePath, "-ss", "00:00:01", "-vframes", "1", imgFilePath);
        try {
            Process process = processBuilder.start();
            // 获取流信息
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
            System.out.println(process.waitFor());
            if (0 == process.waitFor()) {
                return imgFilePath;
            }
            return "";
        } catch (IOException | InterruptedException e) {
            log.error("VideoUtils.generateCover ERROR Msg={}", e.getMessage());
            return "";
        }
    }

    /**
     * FFMpeg视频截取图片
     *
     * @param videoFilePath 视频媒体文件
     */
    public String getVideoTime(String videoFilePath) {
        log.info("CommandUtil.getVideoTime videoFilePath={}", videoFilePath);
        // 截图保存位置
        ProcessBuilder processBuilder = new ProcessBuilder();
        // 重定向错误流,防阻塞
        processBuilder.redirectErrorStream(true);
        // 调用ffmpeg 执行截取命令,需要服务器中安装了ffmpeg并配置了环境变量
        //ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 2497682.mp4
        processBuilder.command("ffprobe", "-v", "error", "-show_entries", "format=duration", "-of", "default=noprint_wrappers=1:nokey=1", videoFilePath);
        try {
            Process process = processBuilder.start();
            // 获取流信息
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
            System.out.println(process.waitFor());
            if (0 == process.waitFor()) {
                return line;
            }
            return "";
        } catch (IOException | InterruptedException e) {
            log.error("CommandUtil.captureImage ERROR Msg={}", e.getMessage());
            return "";
        }
    }
}
