gpt4 book ai didi

c# - System.Diagnostics.Process 程序继续运行

转载 作者:行者123 更新时间:2023-12-04 23:28:17 25 4
gpt4 key购买 nike

我正在尝试使用命令行应用程序中的 ffmpeg 库将一些 avi 文件转换为 mkv。我做了一个允许我这样做的方法:

public bool convertAvitoMkv(string path, string sourceName, string destName)
{

bool returncode = false;
try
{
string comando = string.Format("-i {0} -c:v libx264 -crf 19 -preset slow -c:a libfaac -b:a 192k -ac 2 {1}", string.Format("{0}\\{1}",path,sourceName), string.Format("{0}\\{1}",path,destName) + ".mkv");
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "ffmpeg.exe";
startInfo.WorkingDirectory = programPath;
startInfo.CreateNoWindow = true;
startInfo.Arguments = comando;
process.StartInfo = startInfo;
process.Start();
while (!process.HasExited)
{
process.Kill();
//continue hasta que termine la ejecucion
}

returncode = process.HasExited;
}
catch (Exception ex)
{

}
return returncode;
}

以前的版本没有 while (!process.HasExited),但我添加了它,因为我注意到有很多 ffmpeg.exe 正在运行的实例,我必须手动关闭。我如何保证每次转换都已完成并且 ffmpeg 的每个实例都已关闭?
问候

最佳答案

public bool concatenarArchivos(string archivos, string path, string destName)
{
bool returncode = false;
try
{
string[] extensions = {".mp4"};
string[] dirContents = System.IO.Directory.GetFiles(path, "*.*").Where(f => extensions.Contains(new FileInfo(f).Extension.ToLower())).ToArray();
if (dirContents.Length > 0)
{
string comando = string.Format("-f concat -i {0} -c copy {1}", archivos, string.Format("{0}\\{1}", path, destName));
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "ffmpeg.exe";
startInfo.WorkingDirectory = programPath;
startInfo.CreateNoWindow = true;
startInfo.Arguments = comando;
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
//process.Close();
process.Dispose();
//returncode = process.HasExited;
}
}
catch (Exception ex)
{

}
return returncode;
}

关于c# - System.Diagnostics.Process 程序继续运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33761344/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com