Get length of .wav from sox output
41,004
Solution 1
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'
Solution 2
There is a better way:
soxi -D out.wav
Solution 3
This can be done by using:
soxi -D input.mp3
the output will be the duration directly in secondssoxi -d input.mp3
the output will be the duration with the following format hh:mm:ss.ss
Solution 4
This worked for me (in Windows):
sox --i -D out.wav
Solution 5
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
Related videos on Youtube
Author by
joshu
Updated on October 12, 2020Comments
-
joshu over 3 years
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
-
joshu over 13 yearsI've got no idea how you constructed this, but it works like a charm. Thank you!
-
MrCranky almost 12 yearsFor what it's worth, using sox v14.0.0 on Windows, the $ (EOL) marker caused this answer to fail to give the result expected (instead of parsing to the end of line, it simply parses till it finds something which isn't a digit or period.
-
Ivan Kochurkin about 11 yearsUnfortunately it returns wrong duration, distinguish from
sox output.wav -n stat
method in my case. -
Ivan Kochurkin about 11 yearsUnfortunately it returns wrong duration, distinguish from
sox output.wav -n stat
method in my case. -
Andrew Kuklewicz about 11 yearsI have never seen it to be wrong - can you distinguish in what situation this is incorrect?
-
Ivan Kochurkin about 11 yearsI've cropped the audio with mp3cut.net and got the warn from sox:
WARN mp3-util: MAD lost sync
with wrong duration. On the other handsox output.wav -n stat
execution returns correct duration in error output thread (see my answer for explanation). Also windows explorer shows correct duration. -
Andrew Kuklewicz over 10 yearsFWIW - I've used this method on linux and mac for years without any such issue - sounds windows specific.
-
Ruprecht von Waldenfels over 9 yearsI think the two differ in that soxi uses the header info, while sox looks at the body, too. SO if the header is wrong, the two give different outpu.
-
Matthias almost 8 years@RuprechtvonWaldenfels Interesting theory. Can anyone confirm this? I can't see anything on a quick glance at the man pages.
-
Hubbitus almost 7 yearsWhy theory? Man directly states it for
soxi
(sox --info
): "Displays information from the header of a given audio file or files.", for sox stat: "Display time and frequency domain statistical information about the audio. Audio is passed unmodified through the SoX processing chain." You could read further how it statistics collected and calculated also. -
ed22 over 6 yearsstat returned 139.389388 and this returned 139.407007. For my purposes it's OK. Thanks.
-
WestCoastProjects about 4 yearsThis is more reliable than
soxi
: this one involves traversing the audio file to calculate the length whereassoxi
simply reports what is in the header - regardless of accuracy