Sox返回一些值,但StandardOutput.ReadToEnd()返回空

时间:2012-09-12 09:31:12

标签: c# console-application sox

try
{
        string filename = "E:\\sox-14-4-0\\mysamplevoice.wav";
        Process p = new Process();
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.FileName = "E:\\sox-14-4-0\\sox.exe ";
        p.StartInfo.Arguments = filename + " -n stat";
        p.Start();
        string output = p.StandardOutput.ReadToEnd();
        p.WaitForExit();
}
catch(Exception Ex)
{
        Console.WriteLine(Ex.Message);
}

输出始终为空。当我在命令提示符中运行sox命令时,我可以得到如下响应:

E:\sox-14-4-0>sox mysamplevoice.wav -n stat
Samples read:             26640
Length (seconds):      3.330000
Scaled by:         2147483647.0
Maximum amplitude:     0.515625
Minimum amplitude:    -0.734375
Midline amplitude:    -0.109375
Mean    norm:          0.058691
Mean    amplitude:     0.000122
RMS     amplitude:     0.101146
Maximum delta:         0.550781
Minimum delta:         0.000000
Mean    delta:         0.021387
RMS     delta:         0.041831
Rough   frequency:          526
Volume adjustment:        1.362

在C#中运行相同的命令时,我得到相同的结果,但“output”的值为空。

2 个答案:

答案 0 :(得分:4)

您确定sox.exe是否写入STDOUT而不是写入STDERR?

您可以尝试使用OutputDataReceived事件来阅读数据。

string filename = "E:\\sox-14-4-0\\mysamplevoice.wav";
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.FileName = "E:\\sox-14-4-0\\sox.exe ";
p.StartInfo.Arguments = filename + " -n stat";

p.OutputDataReceived += process_OutputDataReceived;
p.ErrorDataReceived += process_ErrorDataReceived;

p.Start();
p.BeginErrorReadLine();
p.BeginOutputReadLine();


void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
    string s = e.Data;
}

void process_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
    string s = e.Data;
}

答案 1 :(得分:0)

我也遇到了这个问题。为什么SoX写入StandardError?!

如果其他人遇到此问题,原始问题的解决方案可能只是添加2行

p.StartInfo.RedirectStandardError = true; // <-- this
...
string output = p.StandardOutput.ReadToEnd();
if(output == "") output = p.StandardError.ReadToEnd(); // <-- and this