I have a folder with several hundred wav
files. I want to get the minimum length, in milliseconds, of the shortest wav file and the maximum length, respectively. I also want to get the total length of all files.
I know I can use sox
and sed
to get the length of a single wav file, like this
sox some_file.wav -n stat 2>&1 | sed -n 's#^Length (seconds):[^0-9]*\([0-9.]*\)$#\1#p'
The easiest way I can think of is to use a Python
script that loops trough all my files, since they all have generic, consecutive file names ({001-800}.wav), and call the above code. However, I'm not sure how to do that. I know subprocess
should be the module to use, but I couldn't figure out how to pipe.
Currently I'm stuck with something along this line:
import subprocess
import shlex
min = 1000
max = 0
total = 0
for i in range(1,801):
cmd = "sox %03d.wav -n stat 2>&1 | sed -n 's#^Length (seconds):[^0-9]*\([0-9.]*\)$#\1#p" % i
subprocess.call(shlex.split(cmd))
# here is where I would catch the output and do the calculations with min, max, and total