Running ffmpeg.exe through windows service fails to complete while dealing with large files

别等时光非礼了梦想. 提交于 2019-12-08 07:28:58

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/4265286/running-ffmpeg-exe-through-windows-service-fails-to-complete-while-dealing-with

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!