.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程序,因此触发"执行格式错误"。
解决步骤
安装Linux版本FFmpeg
在Dockerfile中添加对应镜像的FFmpeg安装命令:- 基于Debian/Ubuntu的镜像:
RUN apt-get update && apt-get install -y ffmpeg - 基于Alpine的镜像:
RUN apk add --no-cache ffmpeg
- 基于Debian/Ubuntu的镜像:
修改代码中的FFmpeg路径
将代码中startInfo.FileName = GetDirectory + @"/ffmpeg.exe";替换为:startInfo.FileName = "ffmpeg"; // 安装后FFmpeg会加入系统PATH,直接调用即可也可以指定完整路径(如
/usr/bin/ffmpeg),视实际安装位置调整。移除Windows专属配置
删除代码中startInfo.WindowStyle = ProcessWindowStyle.Hidden;语句,该配置仅适用于Windows平台,Linux容器中无意义。
内容的提问来源于stack exchange,提问作者newby567
相关产品推荐
相关产品推荐

