47

我需要获取 .wav 文件的长度。

使用:

sox output.wav -n stat

给出:

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

如何使用 grep 或其他方法仅输出第二列中的长度值,即 28.107375?

谢谢

4

9 回答 9

59

有一个更好的方法:

soxi -D out.wav
于 2011-05-25T07:00:48.443 回答
42

效果将stat其输出发送到stderr,用于2>&1重定向到stdout。用于sed提取相关位:

sox out.wav -n stat 2>&1 | sed -n 's#^Length (seconds):[^0-9]*\([0-9.]*\)$#\1#p'
于 2010-12-26T16:07:56.337 回答
14

这可以通过使用来完成:

  • soxi -D input.mp3输出将直接以秒为单位的持续时间
  • soxi -d input.mp3输出将是具有以下格式的持续时间 hh:mm:ss.ss
于 2012-11-11T14:18:04.090 回答
7

这对我有用(在 Windows 中):

sox --i -D out.wav
于 2012-07-11T21:25:05.910 回答
4

我刚刚在“stat”和“stats”效果上添加了一个 JSON 输出选项。这应该使获取有关音频文件的信息更容易一些。

https://github.com/kylophone/SoxJSONStatStats

$ sox somefile.wav -n stat -json
于 2014-04-25T18:38:14.467 回答
2

对于红宝石:

string = `sox --i -D file_wav 2>&1` 
string.strip.to_f
于 2018-10-11T11:03:44.113 回答
1

有我的 C# 解决方案(不幸sox --i -D out.wav的是,在某些情况下返回错误的结果):

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;
}
于 2013-03-09T19:30:46.263 回答
0

在 CentOS 中

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

于 2014-02-11T11:30:56.590 回答
0

sox stat 输出到数组和 json 编码

        $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);
于 2015-06-02T15:57:45.610 回答