2017-01-23 22 views
0

ffmpegバイナリを呼び出すメソッドを作成してくれました。これは、標準のコンソールアプリケーションで完璧に機能しました。私はWindowsフォームアプリケーションのバージョンを作成しようとしていますが、問題はほとんどありません。 ffmpegプロセスがまだ動作しているとき、アプリケーションはフリーズします(ただし、進行状況バーは更新中です)。テキストボックスは更新されていません。私は、アプリケーションのウィンドウを移動することはできません。私はこれがループのためだと思うし、私はいくつかのものを探せて、私はこれを非同期的に行う必要があるかもしれないことを知ったが、どうすればそれを正確に行うのだろうか。ffmpegからの出力を非同期でストリームする方法

public void ffmpeg(string ffmpeg_exe, string args) 
     { 
      Process p = new Process(); 
      p.StartInfo.FileName = ffmpeg_exe; 
      p.StartInfo.RedirectStandardError = true; 
      p.StartInfo.CreateNoWindow = true; 
      p.StartInfo.Arguments = args; 
      p.StartInfo.UseShellExecute = false; 
      p.Start(); 

      StreamReader reader = p.StandardError; 
      string line; 

      string size = "", current_duration = "", duration = ""; 

      while ((line = reader.ReadLine()) != null) 
      { 
       if (!string.IsNullOrEmpty(line)) 
       { 
        if (line.Contains("Duration") && line.Contains("bitrate") && line.Contains("start")) 
        { 
         duration = RemoveWhitespace(Between(line, "Duration:", ", start")); 
         totaltime.Text = duration; 
        } 

        if (line.Contains("frame=") && line.Contains("size=") && line.Contains("time=")) 
        { 
         size = RemoveWhitespace(Between(line, "size=", " time")); 
         current_duration = RemoveWhitespace(Between(line, "time=", " bitrate")); 
         progressBar_main.Value = Convert.ToInt32(Math.Round(TimeSpan.Parse(current_duration.Substring(0, current_duration.Length -3)).TotalSeconds * 100/TimeSpan.Parse(duration.Substring(0,duration.Length-3)).TotalSeconds, 3)); 
         current_time.Text = current_duration; 
         filesize.Text = size; 
        } 
       } 
      } 

      p.Close(); 

      current_time.Text = ""; 
      filesize.Text = ""; 
      totaltime.Text = ""; 
      progressBar_main.Value = 0; 

     } 

答えて

0

このようなTaskを使用してみてください:

Action action =() => 
          { 
           //Do your streaming here 
          }; 

そして、それを起動します。

Task t2 = Task.Factory.StartNew(action); 

希望、これは有用でした。

+0

これで、タスクを作成して実行できるようになりました。それは正常に実行されたが、メインイベント(ボタンクリック)は終了するのを待たない。 –

+0

タスクが非同期で実行されるため待機しません。タスクが完了したときに発生するイベントを作成できます。 –

+0

ああ、大丈夫です。とった。 –

関連する問題