본문 바로가기

Spring Boot

[SpringBoot] FFmpeg mp4파일 공간음향 처리.

반응형

스프링 부트 응용 프로그램에서 Runtime.exec() 메서드를 사용하여 ffmpeg를 사용하여 비디오에 공간 오디오를 추가하는 방법의 예입니다.

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

@RestController
public class SpatialAudioController {

    @GetMapping("/spatial-audio")
    public String addSpatialAudio() {
        try {
            // Set the input video file path
            String inputFile = "/path/to/your/video.mp4";

            // Set the output video file path
            String outputFile = "/path/to/your/spatial-audio-video.mp4";

            // Set the audio channel layout (e.g., stereo)
            String channelLayout = "stereo";

            // Use the Runtime.exec() method to call ffmpeg and add spatial audio
            Process process = Runtime.getRuntime().exec(new String[]{"ffmpeg", "-i", inputFile, "-ac", "2", "-channel_layout", channelLayout, "-map_channel", "0.0:0.0", "-map_channel", "0.0:0.1", "-c:a", "copy", outputFile});

            // Read the output from the process
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            StringBuilder builder = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
                builder.append(System.getProperty("line.separator"));
            }
            String result = builder.toString();

            // Return the result
            return result;
        } catch (Exception e) {
            return "Error adding spatial audio: " + e.getMessage();
        }
    }
}

시스템에 ffmpeg가 설치되어 있고 명령줄에서 액세스할 수 있다고 가정합니다. ffmpeg를 설치하지 않은 경우 이 코드를 사용하려면 먼저 설치해야 합니다. 여기서 사용되는 특정 명령줄 인수는 입력 비디오에 두 개의 오디오 채널이 있어야 하고 채널 레이아웃이 스테레오여야 하며 오디오 채널을 출력 비디오에 복사해야 한다고 지정합니다. 비디오 및 오디오 파일의 세부 사항에 따라 이러한 인수를 수정해야 할 수도 있습니다.

반응형