c# - FFMPEG 进程太多

标签 c# loops process ffmpeg

我有一个视频文件列表,我想用 ffmpeg 转换它们。这是我的代码:

    public static void ConvertToMp3(String inputPath, String title)
    {
        String outputpath = "\"D:\\Mp3\\" + title + ".mp3\"";

        String _out;
        Process p = new Process();

        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardInput = true;
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.RedirectStandardError = true;
        p.StartInfo.CreateNoWindow = true;
        p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        p.StartInfo.FileName = "ffmpeg";
        p.StartInfo.Arguments = " -i \"" + inputPath + "\" -vn -f mp3 -ab 192k " + outputpath;

        p.Start();
        p.StandardOutput.ReadToEnd();
        _out = p.StandardError.ReadToEnd();
        p.WaitForExit();

        if(!p.HasExited)
            p.Kill();

        Console.WriteLine(_out);   
    }

它工作正常,但是当我在循环中调用此函数 n 次时,它打开了太多进程。我想一次只打开一个进程,完成后,转到下一个。

最佳答案

如何检查进程计数并仅在其小于 x 时才执行您的代码(例如 2)

int process = 0;
foreach (System.Diagnostics.Process myProc in System.Diagnostics.Process.GetProcesses())
{
   if (myProc.ProcessName == "process name")
      process++;

   if (process < 2)
      p.Start();
}

关于c# - FFMPEG 进程太多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14636216/

相关文章:

c# - 非常量开关的替代方案

c# - WPF 绑定(bind)到数组/集合

c# - 我没有源代码的程序集上的 PostSharp

python - 如何在Python中迭代一个数据帧的每一行并与另一个数据帧中的行进行比较?

C 程序测试具有可重复输入且无需重新启动的其他程序

c# - 防止非托管函数指针垃圾回收

基于向量元素运行循环

c++ - While 循环意外终止 (C/C++)

java - 可以使用 64 位 java 库执行 32 位导出的 jar 文件吗?

perl - 如何在 Perl CGI 脚本中生成长时间运行的进程?