본문 바로가기

버그 및 이슈 관리

FFmpeg 프레임 추출

728x90

24/1을 더블로 변환 할 수 없다고 계속 NumberFormatException 발생했는데

values[0] = 24/1

values[1] = 4.16667

이였다 이걸 반대로

double duration = Double.parseDouble(values[0]);

이렇게 대입해서 계속 에러가 발생해서

변수 한 개씩 출력해서 오류 해결

오류 코드

에러 내용

{
    "timestamp": 1715058984138,
    "status": 500,
    "error": "Internal Server Error",
    "message": "For input string: \"24/1\"",
    "path": "/upscale/upscaling"
}

 

수정 코드

public VideoInfo extractVideoInfo(File file, String beforeUrl) throws IOException {
        // ffprobe 실행 경로를 가져옵니다.
        String ffprobePath = ffmpegConfig.getFfmpegPath() + "\\ffprobe.exe"; // .exe 확장자를 포함한 ffprobe 경로

        // ffprobe 명령을 실행하기 위한 ProcessBuilder를 설정합니다.
        ProcessBuilder processBuilder = new ProcessBuilder(
                ffprobePath, "-v", "error",
                "-show_entries", "stream=duration,r_frame_rate",
                "-of", "csv=p=0",
                file.getAbsolutePath()
        );

        processBuilder.redirectErrorStream(true);
        Process process = processBuilder.start();

        try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
            String line = reader.readLine();
            if (line != null) {
                // ffprobe 출력 값을 ','로 분리합니다.
                String[] values = line.split(",");
                if (values.length >= 2) {
                    // 비디오 길이를 가져옵니다.
                    double duration = Double.parseDouble(values[1]);
                    // 프레임 속도 정보를 분리합니다.
                    String[] frameRateParts = values[0].split("/");

                    if (frameRateParts.length == 2) {
                        try {
                            // 분자와 분모를 파싱하고 계산합니다.
                            double numerator = Double.parseDouble(frameRateParts[0]);
                            double denominator = Double.parseDouble(frameRateParts[1]);
                            double fps = numerator / denominator; // 프레임 속도 계산
                            
                            // 총 프레임 수를 계산합니다.
                            int totalFrames = (int) (fps * duration);

                            // 결과를 반환합니다.
                            return new VideoInfo(beforeUrl, totalFrames, fps);
                        } catch (NumberFormatException e) {
                            System.err.println("Error parsing frame rate: " + values[1] + ", error: " + e.getMessage());
                        }
                    } else {
                        System.err.println("Unexpected frame rate format: " + values[1]);
                    }
                } else {
                    System.err.println("Unexpected ffprobe output: " + line);
                }
            }
        }

        return null;
    }