2013-01-31 60 views
0

我有一个视频文件列表,我想用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); 
    } 

它工作正常,但是当我把在一个循环中此功能ñ倍,它会打开过多的进程。我想一次只打开一个进程,当它完成时,转到下一个进程。

+1

'p.WaitForExit();'应该让线程等待,直到进程退出。 ffmepg是否会产生新的进程?从命令行运行此命令时会发生什么?它是否等待转码完成? – JohnD

+0

是的,它等待... –

回答

0

怎么样检查过程中计数,仅执行你的代码,如果它小于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(); 
} 
0

之前WaitForExit,添加此命令

p.Exited += (sender, e) => 
      { 
       // Thread.Sleep(1000 * 60); 
       // Thread thread = new Thread(() => callProcess()); 
       // thread.Start();      
      }; 

这将工作时的过程完了。我通常使用新的线程。