因为需求的原因需要将多个音频(.mp3格式)合并为一个音频
1、在电脑/服务器安装 ffmpeg
brew install ffmpeg
java
public static Integer combine(String commandFix, List<String> filePaths, String combinePath) {
try {
StringBuilder command = new StringBuilder(commandFix + " -i \"concat:");
for (String path : filePaths) {
command.append(path).append("|");
}
command.append("\" -acodec copy ").append(combinePath).append(" -y");
logger.info("执行合并音频代码--" + command);
//拼接ffmpeg命令
Process process = null;
Thread.sleep(1000);
//执行本地命令
Runtime r = Runtime.getRuntime();
String[] cmd = new String[]{"sh", "-c", command.toString()};
process = r.exec(cmd);
return process.waitFor();
} catch (Exception e) {
logger.error("FFmpeg 执行失败,错误码",e);
return -1;
}
}
参数说明
java
commandFix:命令 sudo /usr/local/ffmpeg/bin/ffmpeg
filePaths: 文件列表 服务器对应的文件地址 如:/var/www/file/1.mp3
combinePath:合并后文件的路径 /var/www/file/new.mp3