tags:

views:

1726

answers:

2

I wrote a little GUI wrapper which will execute openRTSP using the Process class. The issue I am having is redirecting the output to a mpeg4 video file. I verified the parameters I am passing are correct by running openRTSP on the command line.

openRTSP.exe -some -parameters -for -video -4 rtsp://video.from.server > video.mp4

The "> video.mp4" is what I am having trouble reproducing.

I have looked at other examples of of using the Process class but they appear to work only with ASCII text.

Edit--- Here is some more detail

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);
    }
}

When using WriteLine to write the data received, when my application quits the file size is the same as when I run openRTSP from the command line which produces "correct" output, namely a mpeg4 video which is playable. When running from command line openRTSP is outputing an mpeg4 file which I am redirecting to a mpeg4.

I tried adding "> fileNameToUse" to the end of the string assigned to startInfo.Arguments but that made openRTSP fail right away.

Thanks, Matt

A: 

You may have to capture the standard output and write it to a file, instead of passing the > paramater on the command line.

The Process Class has a StandardOutput stream that you can access and you should be able to dump that to a file.

Another option might be to write a temporary batch file and launch that using Process.Start() instead.

Navaar
+2  A: 

The following is an example of me doing a similar thing in my own code, it's necessary to copy the stream as Bytes in order to get valid output:

 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();
        }
    }

Obviously you can leave out the writing to Standard Input bits as you don't need this. The main thing is to set RedirectStandardOutput and then copy the output to a BinaryWriter which writes to the FileStream for your desired output file.

RobV