2009-04-16 75 views
1

我寫了一個GUI包裝器,它將使用Process類執行openRTSP。我遇到的問題是將輸出重定向到mpeg4視頻文件。我通過在命令行上運行openRTSP來驗證我傳遞的參數是否正確。C#進程類重定向輸出到視頻文件

openRTSP.exe - 一些α參數 - 適用 - 視頻-4 RTSP://video.from.server> video.mp4

的 「> video.mp4」 是我無法複製的東西。

我看過其他使用Process類的例子,但它們似乎只能用ASCII文本工作。

編輯--- 下面是一些更詳細

this.outputStream = new StreamWriter(fileNameToUse, false, Encoding.Default); 

try 
{ 
    byte[] buffer; 

    // Start the process with the info we specified. 
    // Call WaitForExit and then the using statement will close. 
    using (Process exeProcess = new Process()) 
    { 
     // Assign start info to the process 
     exeProcess.StartInfo = startInfo; 

     // Set up the event handler to call back with each line of output 
     exeProcess.OutputDataReceived += new DataReceivedEventHandler(OnDataReceived); 

     // Start the Process 
     exeProcess.Start(); 

     exeProcess.BeginOutputReadLine(); 
     exeProcess.WaitForExit(); 
    } 
} 
catch (Exception ex) { PrintException(ex); } 
finally 
{ 
    this.outputStream.Flush(); 
    this.outputStream.Close(); 
} 

// Called asynchronously with a line of data 
private void OnDataReceived(object Sender, DataReceivedEventArgs e) 
{ 
    lock (this) 
    { 
     if (!string.IsNullOrEmpty(e.Data) && (this.outputStream != null)) 
      this.outputStream.WriteLine(e.Data); 
    } 
} 

使用的WriteLine寫入的數據接收,當我的應用程序退出的文件大小是一樣的,當我在命令行中運行openRTSP作爲產生「正確的」輸出,即可以播放的mpeg4視頻。當從命令行運行時,openRTSP正在輸出一個mpeg4文件,我將其重定向到mpeg4。

我試着將「> fileNameToUse」添加到分配給startInfo.Arguments的字符串的末尾,但這使得openRTSP立即失敗。

謝謝, 馬特

+0

您可能會發現[MedallionShell](https://github.com/madelson/MedallionShell)庫有趣。它使流程流的重定向非常簡單。 – ChaseMedallion 2014-08-24 18:10:49

回答

0

您可能需要捕獲標準輸出,並將其寫入一個文件,而不是傳遞> paramater在命令行上。

Process Class有一個可以訪問的StandardOutput流,您應該可以將其轉儲到文件。

另一種選擇可能是編寫臨時批處理文件並使用Process.Start()啓動該文件。

2

以下是我在做我自己的代碼類似的事情爲例,有必要在流複製的字節數,以獲得有效的輸出:

public void Example() { 
     //Prepare the Process 
     ProcessStartInfo start = new ProcessStartInfo(); 
     if (!_graphvizdir.Equals(String.Empty)) { 
      start.FileName = this._graphvizdir + "dot.exe"; 
     } else { 
      start.FileName = "dot.exe"; 
     } 
     start.Arguments = "-T" + this._format; 
     start.UseShellExecute = false; 
     start.RedirectStandardInput = true; 
     start.RedirectStandardOutput = true; 

     //Prepare the GraphVizWriter and Streams 
     GraphVizWriter gvzwriter = new GraphVizWriter(); 
     BinaryWriter output = new BinaryWriter(new FileStream(filename, FileMode.Create)); 

     //Start the Process 
     Process gvz = new Process(); 
     gvz.StartInfo = start; 
     gvz.Start(); 

     //Write to the Standard Input 
     gvzwriter.Save(g, gvz.StandardInput); 

     //Read the Standard Output 
     StreamCopy(gvz.StandardOutput.BaseStream, output.BaseStream); 
     output.Close(); 

     gvz.Close(); 
    } 

    public void StreamCopy(Stream input, Stream output) { 
     int i; 
     byte b; 

     i = input.ReadByte(); 

     while (i != -1) 
     { 
      b = (byte)i; 
      output.WriteByte(b); 

      i = input.ReadByte(); 
     } 
    } 

很明顯,你可以離開了寫入標準輸入位,因爲你不需要這個。最主要的是設置RedirectStandardOutput,然後將輸出複製到BinaryWriter,該文件寫入FileStream以獲得所需的輸出文件。

0

我有同樣的問題,並找到了答案(在手冊中),所以我想我會分享。

您需要將-v添加到參數中,這會將數據輸出到標準輸出,您可以在程序中讀取這些標準輸出。

提取單個流

能夠從會話只記錄音頻流,使用「-a」 命令行選項。 (同樣,只錄制視頻流,請使用「-v」選項 )。在這種情況下,輸出音頻(或視頻)流 將寫入「標準輸出」,而不是寫入文件(除非「 -P 「選項(見下面))。

此外,這將是二進制數據,所以得到Process.OutputStream的BaseStream和工作。