c#中的进程冻结



环境:Rider 2022.1.2, .NET 6.0, Windows 10

下面的代码应该复制给定的musicFile并在副本上添加封面,然后将其作为name:title - artist.extension放在同一目录中,例如never gonna give you up - rick astley.mp3

调试时,如果我在CMD终端中复制粘贴保存在变量p.StartInfo.Arguments中的内容,它可以完美地工作,但在我的c#代码中,程序在p.WaitForExit();冻结。

我做错了什么?

using (Process p = new Process())
{
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "CMD.exe";
p.StartInfo.Arguments = "ffmpeg -i ""+ musicFile +"" -i  ""+  albumInfo.Image.Uri.ToString() +"" -map 0:a -map 1 -codec copy -metadata:s:v title="Album cover" -metadata:s:v comment="Cover (front)" -disposition:v attached_pic "" + directoryFile + "\" + title + " - " + artist + "." + formatName + """;
p.Start();
p.WaitForExit();
}

运行命令后未退出。我敢说,在ffmpeg中没有退出标志,所以我会在这里使用标准命令。

试着这样做:

p.StartInfo.Arguments = "ffmpeg -i ""+ musicFile +"" -i  ""+  albumInfo.Image.Uri.ToString() +"" -map 0:a -map 1 -codec copy -metadata:s:v title="Album cover" -metadata:s:v comment="Cover (front)" -disposition:v attached_pic "" + directoryFile + "\" + title + " - " + artist + "." + formatName + "" & exit /b";

这将在ffmpeg完成后立即退出命令提示符。您可能需要考虑在退出之前捕获命令的输出。

另一种方法是直接运行命令而不使用命令提示符,因为Process类可以运行可执行文件。您可以获取ffmpeg可执行文件的路径并直接使用它。这样的:

Process.Start("[full path of ffmpeg.exe here]", "-i ""+ musicFile +"" -i  ""+  albumInfo.Image.Uri.ToString() +"" -map 0:a -map 1 -codec copy -metadata:s:v title="Album cover" -metadata:s:v comment="Cover (front)" -disposition:v attached_pic "" + directoryFile + "\" + title + " - " + artist + "." + formatName + """;

最新更新