2011-09-01 47 views
0

我有這個函數,並且Debug.WriteLine命令所在的位置是我輸出值的位置。我如何獲得這些數據以打印到網頁中,以便我可以模擬加載屏幕?我通過* .ashx web服務文件從外部調用這個函數。如何使用c#做一個加載屏幕ffmpeg#asp.net

private string ConvertToFLV(string phyicalFilePath) 
     { 
      if (Path.GetExtension(phyicalFilePath).Equals(".flv")) return phyicalFilePath; 

      var argument = string.Format(@"-i ""{0}"" -vcodec flv -f flv -r 29.97 -s 320x240 -aspect 4:3 -b 300k -g 160 -cmp dct -subcmp dct -mbd 2 -flags +aic+cbp+mv0+mv4 -trellis 1 -ac 1 -ar 22050 -ab 56k ""{1}""", phyicalFilePath, Path.ChangeExtension(phyicalFilePath, "flv")); 
      libfaac -ar 48000 -ab 128k -coder 1 -flags +loop -cmp +chroma -partitions +parti4x4+partp8x8+partb8x8 -me_method hex -subq 6 -me_range 16 -g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -b_strategy 1 -threads 0 {1}", phyicalFilePath, Path.ChangeExtension(phyicalFilePath, "mp4")); 
      File.Delete(Path.ChangeExtension(phyicalFilePath, "flv")); 

      ProcessStartInfo process = new ProcessStartInfo(ffmpegPhysicalPath, argument); 
      Process proc = new Process(); 
      float duration = 0.00F, current = 0.00F; 
      proc.StartInfo = process; 
      proc.EnableRaisingEvents = false; 
      proc.StartInfo.UseShellExecute = false; 
      proc.StartInfo.CreateNoWindow = true; 
      proc.StartInfo.RedirectStandardOutput = true; 
      proc.StartInfo.RedirectStandardError = true; 

      proc.Start(); 

      StreamReader d = proc.StandardError; 

      do 
      { 
       string s = d.ReadLine(); 
       if (s.Contains("Duration: ")) 
       { 
        Debug.WriteLine("DURATION: " + s); 

       } 
       else 
       { 
        if (s.Contains("frame=")) 
        { 
         Debug.WriteLine("FRAME: " + s); 

        } 
       } 
      } while (!d.EndOfStream); 


      proc.WaitForExit(); 

      return Path.ChangeExtension(phyicalFilePath, "flv"); 

回答

2

您需要將流寫入某個可通過Web請求讀取的數據存儲。您將無法像我想的那樣處理此流。例如,將這些數據寫入緩存/數據庫,並讓客戶端不斷讀取它。 你可以嘗試一些像SignalR這樣的更新的東西,然後嘗試將其傳輸到客戶端,儘管這仍然需要從共享存儲中讀取它。所以你的Debug.WriteLines會改爲寫入隊列的方法。你的客戶端會從隊列中讀取和刪除這些消息。

如果你有興趣的實時持久連接看一下Scott Hanselman的帖子:

AsynchronousScalableWebApplicationsWithRealtimePersistentLongrunningConnectionsWithSignalR