2013-06-25 23 views
1

我正在編寫一個程序來註釋.wav文件,所以我需要播放它們並瞭解它們的持續時間。我可以使用winsound模塊播放(使用SND_ASYNC),但我不能使用wave模塊讀取文件,因爲我不支持使用的文件的壓縮。如何獲取波形模塊在python中不支持的.WAV文件的持續時間?

我應該使用另一個模塊來獲取.WAV文件的持續時間,還是應該使用一個模塊來播放和獲取有關文件的信息?我應該使用哪些模塊?

+0

先解壓,先讓它支持Python嗎? – Raptor

+0

我不知道該怎麼做。我們在這裏談論5000個文件。 – Lewistrick

+1

[this](http://stackoverflow.com/a/7842081/172176)是否有效? – Aya

回答

2

看着評論,這有效(我爲自己的可讀性做了一些改變)。謝謝@Aya!

import os 
path="c:\\windows\\system32\\loopymusic.wav" 
f=open(path,"rb") 

# read the ByteRate field from file (see the Microsoft RIFF WAVE file format) 
# https://ccrma.stanford.edu/courses/422/projects/WaveFormat/ 
# ByteRate is located at the first 28th byte 
f.seek(28) 
a = f.read(4) 

# convert string a into integer/longint value 
# a is little endian, so proper conversion is required 
byteRate = 0 
for i in range(4): 
    byteRate += a[i] * pow(256, i) 

# get the file size in bytes 
fileSize = os.path.getsize(path) 

# the duration of the data, in milliseconds, is given by 
ms = ((fileSize - 44) * 1000))/byteRate 

print "File duration in miliseconds : " % ms 
print "File duration in H,M,S,mS : " % ms/(3600 * 1000) % "," % ms/(60 * 1000) % "," % ms/1000 % "," ms % 1000 
print "Actual sound data (in bytes) : " % fileSize - 44 
f.close() 
相關問題