The user creates two text listfiles. The first listfile contains the names of the WAV files. The second listfile contains the names of the compressed files. Both lists must be ordered in the same way because the script pulls the file durations out of the WAV list and the file sizes out of the compressed list.
Then the program is invoked by:
python bitrate.py [wavlist] [compressedlist]
It outputs the bitrates (kbit/s) of each file in the list, and the overall average bitrate.
I envision this utility being used to help Roberto when he's verifying VBR bitrates in his next test.
Here is the script itself, which is named "bitrate.py"
CODE
# python script to calculate average bitrates
# invoked as follows:
# python bitrate.py [wavlist] [compressedlist]
# wavlist is a text file which contains a list of WAV filenames
# compressedlist is a text file which contains a list of the corresponding
# compressed filenames. The ordering of the filenames must be the same
# as in wavlist.
import sys
import os
import wave
import string
duration = []
inf1 = file(sys.argv[1], 'r')
for line in inf1.readlines():
sline = string.strip(line)
inw = wave.open(sline, 'rb')
duration.append(1.0 * inw.getnframes() / inw.getframerate())
inw.close()
inf1.close()
fsize = []
inf2 = file(sys.argv[2], 'r')
for line in inf2.readlines():
sline = string.strip(line)
st = os.stat(sline)
fsize.append(st.st_size)
inf2.close()
bitrate = []
i = 0
sumduration = 0
sumsize = 0
for fs in fsize:
sumduration = sumduration + duration[i]
sumsize = sumsize + fsize[i]
bitrate.append(fsize[i]/duration[i] * 8.0 / 1000)
i = i + 1
print bitrate
average_bitrate = sumsize/sumduration * 8.0 / 1000
print average_bitrate