Get length of .wav from sox output

2019-03-14 06:29发布

I need to get the length of a .wav file.

Using:

sox output.wav -n stat

Gives:

Samples read:            449718
Length (seconds):     28.107375
Scaled by:         2147483647.0
Maximum amplitude:     0.999969
Minimum amplitude:    -0.999969
Midline amplitude:     0.000000
Mean    norm:          0.145530
Mean    amplitude:     0.000291
RMS     amplitude:     0.249847
Maximum delta:         1.316925
Minimum delta:         0.000000
Mean    delta:         0.033336
RMS     delta:         0.064767
Rough   frequency:          660
Volume adjustment:        1.000

How do I use grep or some other method to only output the value of the length in the second column, i.e. 28.107375?

Thanks

9条回答
神经病院院长
2楼-- · 2019-03-14 06:59

This can be done by using:

  • soxi -D input.mp3 the output will be the duration directly in seconds
  • soxi -d input.mp3 the output will be the duration with the following format hh:mm:ss.ss
查看更多
Anthone
3楼-- · 2019-03-14 07:01

There is my solution for C# (unfortunately sox --i -D out.wav returns wrong result in some cases):

public static double GetAudioDuration(string soxPath, string audioPath)
{
    double duration = 0;
    var startInfo = new ProcessStartInfo(soxPath,
        string.Format("\"{0}\" -n stat", audioPath));
    startInfo.UseShellExecute = false;
    startInfo.CreateNoWindow = true;
    startInfo.RedirectStandardError = true;
    startInfo.RedirectStandardOutput = true;
    var process = Process.Start(startInfo);
    process.WaitForExit();

    string str;
    using (var outputThread = process.StandardError)
        str = outputThread.ReadToEnd();

    if (string.IsNullOrEmpty(str))
        using (var outputThread = process.StandardOutput)
            str = outputThread.ReadToEnd();

    try
    {
        string[] lines = str.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
        string lengthLine = lines.First(line => line.Contains("Length (seconds)"));
        duration = double.Parse(lengthLine.Split(':')[1]);
    }
    catch (Exception ex)
    {
    }

    return duration;
}
查看更多
家丑人穷心不美
4楼-- · 2019-03-14 07:06

I just added an option for JSON output on the 'stat' and 'stats' effects. This should make getting info about an audiofile a little bit easier.

https://github.com/kylophone/SoxJSONStatStats

$ sox somefile.wav -n stat -json
查看更多
可以哭但决不认输i
5楼-- · 2019-03-14 07:08

This worked for me (in Windows):

sox --i -D out.wav
查看更多
6楼-- · 2019-03-14 07:15

The stat effect sends its output to stderr, use 2>&1 to redirect to stdout. Use sed to extract the relevant bits:

sox out.wav -n stat 2>&1 | sed -n 's#^Length (seconds):[^0-9]*\([0-9.]*\)$#\1#p'
查看更多
三岁会撩人
7楼-- · 2019-03-14 07:16

In CentOS

sox out.wav -e stat 2>&1 | sed -n 's#^Length (seconds):[^0-9]([0-9.])$#\1#p'

查看更多
登录 后发表回答