2017-03-22 39 views
2

私はいくつかのビデオファイルで動作するようにプログラムを作っています。C#外部プログラムを実行して出力をキャプチャ(ストリーム)

私はffmpeg実行可能ファイルを使用して、複数のファイルを1つのファイルにマージしています。 このコマンドは終了するまでに数分かかるので、出力を「監視」し、GUIにプログレスバーを表示する方法が必要です。次のstackoverflowのトピックを見てみると

私はこのコードを作った:

Process ffmpeg = new Process 
{ 
    StartInfo = 
    { 
    FileName = @"d:\tmp\ffmpeg.exe", 
    Arguments = "-f concat -safe 0 -i __sync.txt -c copy output.mp4", 
    UseShellExecute = false, 
    RedirectStandardOutput = true, 
    CreateNoWindow = true, 
    WorkingDirectory = @"d:\tmp" 
    } 
} 

ffmpeg.EnableRaisingEvents = true; 
ffmpeg.OutputDataReceived += (s, e) => Debug.WriteLine(e.Data); 
ffmpeg.ErrorDataReceived += (s, e) => Debug.WriteLine([email protected]"Error: {e.Data}"); 
ffmpeg.Start(); 
ffmpeg.BeginOutputReadLine(); 
ffmpeg.WaitForExit(); 

このコードを実行すると、ffmpegがファイルのマージを開始し、Windowsタスクマネージャのffmpegプロセスが表示され、十分に待っているとffmpegはエラーなしでジョブを終了します。しかし、Debug.WriteLine(e.Data)は呼び出されません(デバッグウィンドウでは出力されません)。 Console.WriteLineにも変更しようとしました(出力はありません)。 ffmpegのが終了するまでWhile (!ffmpeg.StandardOutput.EndOfStream)

Process ffmpeg = new Process 
{ 
    StartInfo = 
    { 
    FileName = @"d:\tmp\ffmpeg.exe", 
    Arguments = "-f concat -safe 0 -i __sync.txt -c copy output.mp4", 
    UseShellExecute = false, 
    RedirectStandardOutput = true, 
    CreateNoWindow = true, 
    WorkingDirectory = @"d:\tmp" 
    } 
} 

ffmpeg.Start(); 
while (!ffmpeg.StandardOutput.EndOfStream) 
{ 
    var line = ffmpeg.StandardOutput.ReadLine(); 
    System.Diagnostics.Debug.WriteLine(line); 
    Console.WriteLine(line); 
} 
ffmpeg.WaitForExit(); 

ここでも、ffmpegのはエラーなしで開始されていますが、C#、「ハング」:

だから、この後、私はこの別のバージョンを試してみました。

Windowsのプロンプトでexactコマンドを実行すると、ffmpegの進行状況とともに多くの出力テキストが表示されます。

答えて

3

問題が見つかりました。

何らかの理由で、ffmpegがstdoutではなくstderrで進捗を出力します。

したがって、最初のバージョンでは、ffmpeg.BeginOutputReadLine();の後に、私は次の行を含めました: 。

ここでは、ffmpegの標準エラー出力を使用して進捗状況を監視できます。

決勝コード:

Process ffmpeg = new Process 
{ 
    StartInfo = { 
     FileName = @"d:\tmp\videos\ffmpeg.exe", 
     Arguments = "-f concat -safe 0 -i __sync.txt -c copy output.mp4", 
     UseShellExecute = false, 
     RedirectStandardOutput = true, 
     RedirectStandardError = true, 
     CreateNoWindow = true, 
     WorkingDirectory = @"d:\tmp\videos\gopro" 
    } 
}; 

ffmpeg.EnableRaisingEvents = true; 
ffmpeg.OutputDataReceived += (s, e) => Debug.WriteLine(e.Data); 
ffmpeg.ErrorDataReceived += (s, e) => Debug.WriteLine([email protected]"Error: {e.Data}"); 
ffmpeg.Start(); 
ffmpeg.BeginOutputReadLine(); 
ffmpeg.BeginErrorReadLine(); 
ffmpeg.WaitForExit(); 
関連する問題