欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

Java 使用 ffmpeg 指令实现音频格式转换

最编程 2024-03-05 17:42:30
...

针对Linux环境下如何安装ffmpeg请看上一篇文章Linux上搭建并使用ffmpeg(Java)-****博客

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();
        }
    }

推荐阅读