我正在尝试在 Azure 功能上的 Linux 上运行打包到单个文件 ffmpeg:
var ffmpeg = $"/home/site/wwwroot/ffmpeg";
var videoPath = Path.GetFullPath($"SampleVideo1.mp4");
var ffmpegCommand = $"{ffmpeg} -i \"{videoPath}\"";
using Process process = new();
process.StartInfo.FileName = "bash";
process.StartInfo.Arguments = $"-c \"{ffmpegCommand}\"";
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.Start();
var output = await process.StandardOutput.ReadToEndAsync();
var error = await process.StandardError.ReadToEndAsync();
await process.WaitForExitAsync();
这应该是一个通用的解决方案,能够从任何可执行文件获取输出,而不仅仅是 ffmpeg。
我遇到的问题:
UseShellExecute = false
会导致权限被拒绝错误(使用 Process.Start() 表示权限被拒绝)UseShellExecute = true
导致无法从进程中获取输出/错误数据。我需要的是能够运行脚本/可执行文件并能够获取其输出。我找到了一个解决方案,我可以在没有权限被拒绝的情况下使用 bash,使用
-c
参数运行我的目标文件,UseShellExecute = false
,我将能够获得它的输出。
作为侧面解决方案,我尝试使用
UseShellExecute = true
运行 bash 并尝试使用 -c "commandhere > somefile.txt
将输出保存到文件中。
在这两种情况下我都会收到错误:
bash: line 1: /home/site/wwwroot/ffmpeg: Permission denied\n
我尝试过之前提到的组合,我也尝试过
var chmodStartInfo = new ProcessStartInfo
{
FileName = "chmod",
Arguments = $"+x {ffmpegPath}",
RedirectStandardOutput = false,
RedirectStandardError = false,
UseShellExecute = true,
CreateNoWindow = true
};
using (var chmodProcess = new Process { StartInfo = chmodStartInfo })
{
chmodProcess.Start();
await chmodProcess.WaitForExitAsync();
}
在 bash 进程之前,但会导致相同的权限被拒绝问题。
事实证明,这是在 Azure Devops 的 Pipelines 中构建应用程序时的常见问题。文件没有 +x 权限,在我们将
chmod +x
添加到特定文件作为部署阶段后,它有所帮助。