多个MP3音频合成

 FFmpeg详细安装步骤和使用笔记 - 知乎

package com.bootdo;

import java.io.BufferedReader;
import java.io.IOException;  
import java.io.InputStreamReader;  
  
public class Mp3Merger {  
  
    public static void main(String[] args) {  
        String ffmpegPath = "ffmpeg"; // 或者指定FFmpeg的完整路径,例如 "/usr/bin/ffmpeg"

        String input1 = "D:/ZHJQDoc/" + "智慧广播前奏.MP3";
        //合并音频
        String input2 = "D:/ZHJQDoc/烟花爆竹.MP3";
        // 输出文件
        String output = "D:/ZHJQDoc/merged.mp3";

  
        // 构建FFmpeg命令  
        String[] command = {  
            ffmpegPath,  
            "-i", input1,  
            "-i", input2,  
            "-filter_complex", "[0:a:0][1:a:0]concat=n=2:v=0:a=1[a]",  
            "-map", "[a]",  
            "-acodec", "libmp3lame",  
            "-ab", "159k",
            "-ar", "48000",
            output  
        };  
  
        // 执行FFmpeg命令  
        ProcessBuilder processBuilder = new ProcessBuilder(command);  
        processBuilder.redirectErrorStream(true); // 合并stdout和stderr  
  
        try {  
            Process process = processBuilder.start();  
  
            // 读取FFmpeg的输出  
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));  
            String line;  
            while ((line = reader.readLine()) != null) {  
                System.out.println(line);  
            }  
  
            // 等待进程完成  
            int exitCode = process.waitFor();  
            if (exitCode == 0) {  
                System.out.println("MP3 files merged successfully.");  
            } else {  
                System.out.println("FFmpeg exited with error code " + exitCode);  
            }  
  
        } catch (IOException | InterruptedException e) {  
            e.printStackTrace();  
        }  
    }  
}

你可能感兴趣的:(java)