在处理大文件时,通过Windows服务运行ffmpeg.exe无法完成

我使用ffmpeg.exe将video文件转换为flv格式 。 为此,我使用Windows服务在后台运行转换过程 。 在尝试通过Windows服务 转换大文件 (我在文件大小> 14MB时经历过)时, 它会卡在启动进程的行 (即process.start(); )。

但是当我试图直接从命令提示符执行ffmpeg.exe时,它解决了任何问题。

我在windows服务中的 代码 如下:

 private Thread WorkerThread; protected override void OnStart(string[] args) { WorkerThread = new Thread(new ThreadStart(StartHandlingVideo)); WorkerThread.Start(); } protected override void OnStop() { WorkerThread.Abort(); } private void StartHandlingVideo() { FilArgs = string.Format("-i {0} -ar 22050 -qscale 1 {1}", InputFile, OutputFile); Process proc; proc = new Process(); try { proc.StartInfo.FileName = spath + "\\ffmpeg\\ffmpeg.exe"; proc.StartInfo.Arguments = FilArgs; proc.StartInfo.UseShellExecute = false; proc.StartInfo.CreateNoWindow = false; proc.StartInfo.RedirectStandardOutput = true; proc.StartInfo.RedirectStandardError = true; eventLog1.WriteEntry("Going to start process of convertion"); proc.Start(); string StdOutVideo = proc.StandardOutput.ReadToEnd(); string StdErrVideo = proc.StandardError.ReadToEnd(); eventLog1.WriteEntry("Convertion Successful"); eventLog1.WriteEntry(StdErrVideo); } catch (Exception ex) { eventLog1.WriteEntry("Convertion Failed"); eventLog1.WriteEntry(ex.ToString()); } finally { proc.WaitForExit(); proc.Close(); } 

我怎样才能摆脱这种局面。

您似乎遇到了死锁,因为您在两个重定向流的末尾执行了同步读取。

来自MSDN的参考:

当您从标准输出和标准错误流中读取所有文本时,会出现类似的问题。 例如,以下C#代码对两个流执行读取操作。

  // Do not perform a synchronous read to the end of both // redirected streams. // string output = p.StandardOutput.ReadToEnd(); // string error = p.StandardError.ReadToEnd(); // p.WaitForExit(); // Use asynchronous read operations on at least one of the streams. p.BeginOutputReadLine(); string error = p.StandardError.ReadToEnd(); p.WaitForExit(); 

该代码示例通过对StandardOutput流执行异步读取操作来避免死锁条件。 如果父进程调用p.StandardOutput.ReadToEnd后跟p.StandardError.ReadToEnd并且子进程写入足够的文本来填充其错误流,则会导致死锁条件。 父进程将无限期地等待子进程关闭其StandardOutput流。 子进程将无限期地等待父进程从完整的StandardError流中读取。

您可以使用异步读取操作来避免这些依赖关系及其死锁潜力。 或者,您可以通过创建两个线程并在单独的线程上读取每个流的输出来避免死锁条件。