Get length of .wav from sox output

别来无恙 提交于 2019-12-03 01:01:22
phihag

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'
Andrew Kuklewicz

There is a better way:

soxi -D out.wav

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

This worked for me (in Windows):

sox --i -D out.wav

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

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

for ruby:

string = `sox --i -D file_wav 2>&1` 
string.strip.to_f

In CentOS

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

sox stat output to array and json encode

        $stats_raw = array();
        exec('sox file.wav -n stat 2>&1', $stats_raw);
        $stats = array();

        foreach($stats_raw as $stat) {
            $word = explode(':', $stat);
            $stats[] = array('name' => trim($word[0]), 'value' => trim($word[1]));
        } 
        echo json_encode($stats);
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!