You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Linux/Windows系统用Spring Boot Java结合FFmpeg提取音频及命令配置指导

Spring Boot 结合 FFmpeg 在 Linux/Windows 提取音频的实现方案

一、环境准备

Linux 系统

  • 通过包管理器安装 FFmpeg:
    • Debian/Ubuntu 系列:sudo apt update && sudo apt install ffmpeg
    • RHEL/CentOS 系列:sudo yum install epel-release && sudo yum install ffmpeg

Windows 系统

  • 下载 FFmpeg 官方编译好的二进制包,解压到本地目录(比如C:\ffmpeg)
  • 将解压后的bin目录(如C:\ffmpeg\bin)添加到系统环境变量PATH,确保命令行能直接调用ffmpeg.exe

二、修正 FFmpeg 命令参数

你提供的命令数组存在参数拼接错误:"-f", "mp3" + fileStorageLocation会把格式标识和输出路径混在一起,正确写法应将输出路径单独作为参数。修正后的命令数组如下:

// filename 为输入视频路径,outputPath 为输出音频的完整路径(例:"/opt/audio/output.mp3" 或 "D:\\audio\\output.mp3")
String[] command = {
    "ffmpeg", // Windows 未配置环境变量时需写全路径:"C:\\ffmpeg\\bin\\ffmpeg.exe"
    "-y", // 覆盖已存在的输出文件
    "-i", filename, // 指定输入文件
    "-vn", // 禁用视频流,只提取音频
    "-ar", "44100", // 设置音频采样率
    "-ac", "2", // 设置声道数
    "-b:a", "256k", // 设置音频比特率
    "-f", "mp3", // 指定输出格式为 MP3
    outputPath // 输出文件路径
};

三、Spring Boot 中执行 FFmpeg 命令

在 Spring Boot 里用ProcessBuilder执行系统命令,同时要处理输入输出流避免进程阻塞,以下是示例 Service 方法:

import org.springframework.stereotype.Service;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.TimeUnit;

@Service
public class AudioExtractService {

    public void extractAudio(String inputVideoPath, String outputAudioPath) throws IOException, InterruptedException {
        // 根据系统自动适配 FFmpeg 路径
        String ffmpegPath = System.getProperty("os.name").toLowerCase().contains("win") 
            ? "C:\\ffmpeg\\bin\\ffmpeg.exe" 
            : "ffmpeg";

        String[] command = {
            ffmpegPath,
            "-y",
            "-i", inputVideoPath,
            "-vn",
            "-ar", "44100",
            "-ac", "2",
            "-b:a", "256k",
            "-f", "mp3",
            outputAudioPath
        };

        ProcessBuilder processBuilder = new ProcessBuilder(command);
        processBuilder.redirectErrorStream(true); // 合并错误流和输出流,方便日志捕获
        Process process = processBuilder.start();

        // 读取命令输出,防止进程阻塞
        try (InputStream inputStream = process.getInputStream()) {
            byte[] buffer = new byte[1024];
            int len;
            while ((len = inputStream.read(buffer)) != -1) {
                // 可将输出内容写入项目日志
                System.out.print(new String(buffer, 0, len));
            }
        }

        // 设置命令执行超时时间
        boolean isFinished = process.waitFor(5, TimeUnit.MINUTES);
        if (!isFinished) {
            process.destroy();
            throw new RuntimeException("FFmpeg 命令执行超时");
        }

        int exitCode = process.exitValue();
        if (exitCode != 0) {
            throw new RuntimeException("音频提取失败,FFmpeg 退出码:" + exitCode);
        }
    }
}

四、注意事项

  • 权限问题:Linux 下确保项目运行用户对输入文件、输出目录有读写权限;Windows 避免将输出目录设为系统保护目录(如C:\Windows)。
  • 路径格式:Windows 路径用双反斜杠\\或正斜杠/;Linux 统一用正斜杠/。
  • 依赖验证:启动项目前先在命令行执行ffmpeg -version,确认 FFmpeg 可正常调用。

内容的提问来源于stack exchange,提问作者Makarand Burud

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 07:10:31