简体   繁体   English

关于System.Diagnostics.Process中的重定向标准输出

[英]About redirected stdout in System.Diagnostics.Process

I've been recently working on a program that convert flac files to mp3 in C# using flac.exe and lame.exe, here are the code that do the job: 我最近一直在研究使用flac.exe和lame.exe将flac文件转换为C#中的mp3的程序,以下是完成此工作的代码:

ProcessStartInfo piFlac = new ProcessStartInfo( "flac.exe" );
piFlac.CreateNoWindow = true;
piFlac.UseShellExecute = false;
piFlac.RedirectStandardOutput = true;
piFlac.Arguments = string.Format( flacParam, SourceFile );

ProcessStartInfo piLame = new ProcessStartInfo( "lame.exe" );
piLame.CreateNoWindow = true;
piLame.UseShellExecute = false;
piLame.RedirectStandardInput = true;
piLame.RedirectStandardOutput = true;
piLame.Arguments = string.Format( lameParam, QualitySetting, ExtractTag( SourceFile ) );

Process flacp = null, lamep = null;
byte[] buffer = BufferPool.RequestBuffer();


flacp = Process.Start( piFlac );
lamep = new Process();
lamep.StartInfo = piLame;
lamep.OutputDataReceived += new DataReceivedEventHandler( this.ReadStdout );
lamep.Start();
lamep.BeginOutputReadLine();

int count = flacp.StandardOutput.BaseStream.Read( buffer, 0, buffer.Length );

while ( count != 0 )
{
    lamep.StandardInput.BaseStream.Write( buffer, 0, count );

    count = flacp.StandardOutput.BaseStream.Read( buffer, 0, buffer.Length );
}

Here I set the command line parameters to tell lame.exe to write its output to stdout, and make use of the Process.OutPutDataRecerved event to gather the output data, which is mostly binary data, but the DataReceivedEventArgs.Data is of type "string" and I have to convert it to byte[] before put it to cache, I think this is ugly and I tried this approach but the result is incorrect. 在这里,我设置了命令行参数,以告诉lame.exe将其输出写入stdout,并利用Process.OutPutDataRecerved事件来收集输出数据,该数据主要是二进制数据,但DataReceivedEventArgs.Data的类型为“字符串” “,我必须先将其转换为byte [],然后再将其放入缓存,我认为这很丑陋,我尝试了这种方法,但结果不正确。

Is there any way that I can read the raw redirected stdout stream, either synchronously or asynchronously, bypassing the OutputDataReceived event? 有什么方法可以绕过OutputDataReceived事件,以同步或异步方式读取原始重定向的stdout流?

PS: the reason why I don't use lame to write to disk directly is that I'm trying to convert several files in parallel, and direct writing to disk will cause severe fragmentation. PS:之所以我不使用la脚直接写入磁盘是因为我试图并行转换多个文件,而直接写入磁盘会导致严重的碎片。

Thanks a lot! 非常感谢!

I don't think lame and flac uses Stdout to output its converted data. 我不认为la脚和flac使用Stdout输出其转换后的数据。 I think they write the converted files directly to disk. 我认为他们将转换后的文件直接写入磁盘。 The StdOut is only used to output info/error messages. StdOut仅用于输出信息/错误消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM