通过Windows服务运行ffmpeg.exe未能完成在处理大文件大文件、Windows、ffmpeg、exe

2023-09-04 23:34:43 作者:解不开的情愫

我使用的 ffmpeg.exe到视频文件转换为FLV格式。为此目的,我使用的 Windows服务在后台运行转换过程。虽然试图转换大文件(我经历过,当文件大小为> 14MB)通过的 Windows服务它被卡在该启动过程中的行(即的Process.Start();

但是,当我试图直接从命令执行ffmpeg.exe提示它与出任何问题的工作。

我的 code 在窗口服务是为如下:

 私人主题的WorkerThread;
保护覆盖无效的OnStart(字串[] args)
{

   的WorkerThread =新主题(新的ThreadStart(StartHandlingVideo));
   WorkerThread.Start();
}

保护覆盖无效的onStop()
{
   WorkerThread.Abort();
}

私人无效StartHandlingVideo()
{
   FilArgs =的String.Format( - 我{0} -ar 22050 -qscale 1 {1},inputFile,请OUTPUTFILE);
   流程PROC;
   PROC =新工艺();

   尝试
   {

     proc.StartInfo.FileName = SPATH +\\ \\的ffmpeg ffmpeg.exe;
     proc.StartInfo.Arguments = FilArgs;
     proc.StartInfo.UseShellExecute = FALSE;
     proc.StartInfo.CreateNoWindow = FALSE;
     proc.StartInfo.RedirectStandardOutput =真;
     proc.StartInfo.RedirectStandardError = TRUE;

     eventLog1.WriteEntry(要开始的皈依过程);

     proc.Start();

     字符串StdOutVideo = proc.StandardOutput.ReadToEnd();
     字符串StdErrVideo = proc.StandardError.ReadToEnd();

     eventLog1.WriteEntry(皈依成功);
     eventLog1.WriteEntry(StdErrVideo);
 }
 赶上(例外前)
 {
     eventLog1.WriteEntry(皈依失败);
     eventLog1.WriteEntry(ex.ToString());
 }
 最后
 {
     proc.WaitForExit();
     proc.Close();
 }
 

如何才能摆脱这种局面。

解决方案

看来你陷入死锁,因为你执行的同步读来既重定向数据流的结束。

从 MSDN 参考:

  

还有一个类似的问题,当你阅读   从标准输出的所有文本   和标准错误流。该   下面的C#code,例如,   执行在两个读操作   流。

  //不执行同步读来既年底
 //重定向数据流。
 //字符串输出= p.StandardOutput.ReadToEnd();
 //字符串错误= p.StandardError.ReadToEnd();
 // p.WaitForExit();
 //在液流中的至少一种使用异步读取操作。
 p.BeginOutputReadLine();
 字符串错误= p.StandardError.ReadToEnd();
 p.WaitForExit();
 

  

在code例如避免僵局   通过执行异步条件   看了就StandardOutput操作   流。死锁条件的结果   如果父进程调用   p.StandardOutput.ReadToEnd其次是   p.StandardError.ReadToEnd和   子进程写入足够多的文本来   填写其错误流。父   进程将无限期地等待   子进程关闭其   StandardOutput流。孩子   进程将无限期地等待   父母从全面看   StandardError的流。

     电脑系统小知识 Windows7音频服务未运行的处理步骤

您可以使用异步读   操作,以避免这些依赖   和他们的僵局潜力。   或者,你可以避开   通过创建两个死锁情况   线程和读出的每一个的输出   流在单独的线程

I am using ffmpeg.exe to convert video files to flv format. For that purpose i use a windows service to run the conversion process in background. While trying to convert large files(i experienced it when the file size is >14MB) through windows service it gets stuck at the line which starts the process(ie, process.start();).

But when i tried to execute ffmpeg.exe directly from command prompt it worked with out any problems.

My code in windows service is as follows:

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();
 }

How can I get rid of this situation.

解决方案

It seems you caught a deadlock because you performed a synchronous read to the end of both redirected streams.

A reference from MSDN:

There is a similar issue when you read all text from both the standard output and standard error streams. The following C# code, for example, performs a read operation on both streams.

 // 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();

The code example avoids the deadlock condition by performing asynchronous read operations on the StandardOutput stream. A deadlock condition results if the parent process calls p.StandardOutput.ReadToEnd followed by p.StandardError.ReadToEnd and the child process writes enough text to fill its error stream. The parent process would wait indefinitely for the child process to close its StandardOutput stream. The child process would wait indefinitely for the parent to read from the full StandardError stream.

You can use asynchronous read operations to avoid these dependencies and their deadlock potential. Alternately, you can avoid the deadlock condition by creating two threads and reading the output of each stream on a separate thread.

 
精彩推荐
图片推荐