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

.NET控制台应用Docker容器中调用ffmpeg.exe报错求助

.NET控制台应用Docker化调用FFmpeg执行格式错误问题分析与解决

问题背景

将.NET控制台应用移植到Docker容器后,调用ffmpeg.exe时触发执行格式错误,无法正常运行。

相关代码

static void encoder(string inputFile, string outputFolder, string outputFileName)
{
    if (!Directory.Exists(outputFolder))
    {
        Directory.CreateDirectory(outputFolder);
    }

    var GetDirectory = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);

    // Part 1: use ProcessStartInfo class.
    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.CreateNoWindow = false;
    startInfo.UseShellExecute = false;
    startInfo.RedirectStandardInput = true;
    startInfo.FileName = GetDirectory + @"/ffmpeg.exe";
    startInfo.WindowStyle = ProcessWindowStyle.Hidden;

    // Part 2: set arguments.
    startInfo.Arguments = "-i " + inputFile + " -q:a 8 -filter:a loudnorm " + outputFolder + outputFileName;

    // Part 3: start with the info we specified.
    // ... Call WaitForExit.
    using (Process exeProcess = Process.Start(startInfo))
    {
        exeProcess.StandardInput.WriteLine("y");
        exeProcess.WaitForExit();
    }
}

错误信息(中文翻译)

OPUS-Encoder System.ComponentModel.Win32Exception (8): 尝试启动进程 '/App/ffmpeg.exe' 时出错,工作目录为 '/App'。执行格式错误
   在 System.Diagnostics.Process.ForkAndExecProcess(ProcessStartInfo startInfo, String resolvedFilename, String[] argv, String[] envp, String cwd, Boolean setCredentials, UInt32 userId, UInt32 groupId, UInt32[] groups, Int32& stdinFd, Int32& stdoutFd, Int32& stderrFd, Boolean usesTerminal, Boolean throwOnNoExec)
   在 System.Diagnostics.Process.StartCore(ProcessStartInfo startInfo)
   在 System.Diagnostics.Process.Start()
   在 System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
   在 Program.<<Main>$>g__encoder|0_0(String inputFile, String outputFolder, String outputFileName) 位置 /App/Program.cs:第77行
   在 Program.<Main>$(String[] args) 位置 /App/Program.cs:第34行

问题原因与解决方案

核心原因

你使用的是Linux环境的Docker容器,而ffmpeg.exe是Windows平台专属的可执行文件,Linux系统无法直接运行Windows的EXE程序,因此触发"执行格式错误"。

解决步骤

  1. 安装Linux版本FFmpeg
    在Dockerfile中添加对应镜像的FFmpeg安装命令:

    • 基于Debian/Ubuntu的镜像:
      RUN apt-get update && apt-get install -y ffmpeg
      
    • 基于Alpine的镜像:
      RUN apk add --no-cache ffmpeg
      
  2. 修改代码中的FFmpeg路径
    将代码中startInfo.FileName = GetDirectory + @"/ffmpeg.exe";替换为:

    startInfo.FileName = "ffmpeg"; // 安装后FFmpeg会加入系统PATH,直接调用即可
    

    也可以指定完整路径(如/usr/bin/ffmpeg),视实际安装位置调整。

  3. 移除Windows专属配置
    删除代码中startInfo.WindowStyle = ProcessWindowStyle.Hidden;语句,该配置仅适用于Windows平台,Linux容器中无意义。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 05:20:50