2016-07-20 29 views
0

FFMPEGを使用してビデオの長さ/長さを取得するのに苦労しています。以下はGoogleから取得したコードですが、メソッドを実行すると空の文字列が返されます。私は何ができるかを微調整しましたが、成功しませんでした。誰でも私にここで間違っていることを教えてもらえますか?ffmpeg/ffprobeを使用してビデオの長さを取得していない

private static void GetVideoDuration() 
    { 
     string basePath = Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar; 
     string filePath = basePath + @"1.mp4";    
     string cmd = string.Format("-v error -select_streams v:0 -show_entries stream=duration -of default=noprint_wrappers=1:nokey=1 {0}", filePath); 
     Process proc = new Process(); 
     proc.StartInfo.FileName = Path.Combine(basePath, @"ffprobe.exe"); 
     proc.StartInfo.Arguments = cmd; 
     proc.StartInfo.RedirectStandardError = true; 
     proc.StartInfo.UseShellExecute = false;    

     if (!proc.Start()) 
     { 
      Console.WriteLine("Error starting"); 
      return; 
     } 
     StreamReader reader = proc.StandardError; 
     string line; 
     while ((line = reader.ReadToEnd()) != null) 
     { 
      Console.WriteLine(line); 
     } 
     proc.Close(); 
    } 
+0

スタンダード出力をリダイレクトし、FFMPEGからのすべての出力を読み取る必要があります。 FFMPEGはそこにたくさんのテキストを入れます。 FFMPEGを開始した後、最初にWaitForExitを実行して、FFMPEGがジョブを終了できるようにします。 –

+0

'ffprobe'コマンドは、手動で実行する場合、コマンドラインインターフェイス経由でスクリプトを使用しない場合は動作しますか?これは、スクリプトを作成しようとする前に試行する必要があります。 – LordNeckbeard

+0

いいえ私はコマンドラインインターフェイスを使ってffprobeを試してみませんでしたが、上記のコードでフィッティングしてffprobeコマンドをいくつか試しましたが、空の文字列が出力として返され、悲しい部分はビデオの再生時間私は欲しい。 –

答えて

1

私は答えを得ました。 ffprobeが

proc.StandardError; 

を使用して出力を返しませんが、ステートメントの上

proc.StandardOutput; 

を使用することはなく、ffprobeで、文の下にffmpegのと正常に動作しているようだ

はffprobeと協力しています。 誰かがそれを必要とする場合のための実例がここにあります。上記の方法

private static void GetVideoDuration() 
    { 
     string basePath = Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar; 
     string filePath = basePath + @"1.mp4"; 
     string cmd = string.Format("-v error -select_streams v:0 -show_entries stream=duration -sexagesimal -of default=noprint_wrappers=1:nokey=1 {0}", filePath); 
     Process proc = new Process(); 
     proc.StartInfo.FileName = Path.Combine(basePath, @"ffprobe.exe"); 
     proc.StartInfo.Arguments = cmd; 
     proc.StartInfo.CreateNoWindow = true; 
     proc.StartInfo.RedirectStandardOutput = true; 
     proc.StartInfo.RedirectStandardError = true; 
     proc.StartInfo.UseShellExecute = false; 
     proc.StartInfo.UseShellExecute = false; 
     if (!proc.Start()) 
     { 
      Console.WriteLine("Error starting"); 
      return; 
     } 
     string duration = proc.StandardOutput.ReadToEnd().Replace("\r\n", ""); 
     // Remove the milliseconds 
     duration = duration.Substring(0, duration.LastIndexOf(".")); 
     proc.WaitForExit(); 
     proc.Close(); 
    } 

HHにお時間を返します:MM:ss.fff TTフォーマット、ミリ秒を含むことができますが、秒単位で時間をしたい場合は、コマンドから-sexagesimalを削除してください。

関連する問題