针对Linux环境下如何安装ffmpeg请看上一篇文章Linux上搭建并使用ffmpeg(Java)-CSDN博客
public static void voiceChangeFormat(String localPath, String targetPath) {
List<String> command = new ArrayList<>();
command.add("ffmpeg");
command.add("-i");
command.add(localPath);
command.add("-ar");
command.add("8000");
command.add("-ab");
command.add("12.2k");
command.add("-ac");
command.add("1");
command.add(targetPath);
commandStart(command);
}
/**
* 调用命令行执行
*
* @param command 命令行参数
*/
public static void commandStart(List<String> command) {
command.forEach(v -> System.out.print(v + " "));
System.out.println();
System.out.println();
ProcessBuilder builder = new ProcessBuilder();
//正常信息和错误信息合并输出
builder.redirectErrorStream(true);
builder.command(command);
//开始执行命令
Process process = null;
try {
process = builder.start();
//如果你想获取到执行完后的信息,那么下面的代码也是需要的
String line = "";
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}