我编写了一个小的图形用户界面包装器,它将使用Process类执行openRTSP。我遇到的问题是将输出重定向到mpeg4视频文件。我通过在命令行上运行openRTSP验证了我传递的参数是否正确。
openRTSP.exe -some -parameters -for -video -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失败。
谢谢,马特
发布于 2009-05-15 09:33:32
下面是我在自己的代码中执行类似操作的示例,为了获得有效的输出,有必要将流复制为Bytes:
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。
发布于 2009-04-16 02:39:34
您可能必须捕获标准输出并将其写入文件,而不是在命令行上传递>参数。
Process类有一个您可以访问的StandardOutput流,您应该能够将其转储到一个文件中。
另一种选择可能是编写一个临时批处理文件,然后使用Process.Start()启动该文件。
发布于 2014-05-14 05:13:46
我有同样的问题,并找到了答案(在手册中),所以我想我应该分享一下。
您需要在参数中添加-v,这将把数据输出到您可以在程序中读取的标准输出。
提取单个流
要只录制会话中的音频流,请使用"-a“命令行选项。(同样,要仅录制视频流,请使用"-v“选项。)在这种情况下,输出的音频(或视频)流将被写入'stdout',而不是一个文件(除非指定了"-P“选项(见下文))。
此外,这将是二进制数据,因此获取Process.OutputStream的BaseStream并使用它。
https://stackoverflow.com/questions/754485
复制相似问题