2013-06-25 5 views
1

.wav 파일에 주석을 달기위한 프로그램을 작성 중이므로 해당 파일을 재생하고 재생 시간을 알아야합니다. winsound 모듈을 사용하여 (SND_ASYNC 사용) 재생할 수 있지만 사용하는 파일의 압축이 지원되지 않기 때문에 wave 모듈을 사용하여 파일을 읽을 수 없습니다.파이썬에서 웨이브 모듈이 지원하지 않는 .WAV 파일의 지속 시간을 얻는 방법은 무엇입니까?

.WAV 파일의 지속 시간을 가져 오기 위해 다른 모듈을 사용해야합니까? 아니면 파일 재생 및 가져 오기 모두에 하나의 모듈을 사용해야합니까? 어떤 모듈을 사용해야합니까?

+0

먼저 압축을 풀고 파이썬이 먼저 지원하도록 하시겠습니까? – Raptor

+0

나는 그것을 어떻게하는지 모른다. 여기에 5000 개의 파일이 있습니다. – Lewistrick

+1

[this] (http://stackoverflow.com/a/7842081/172176)이 효과가 있습니까? – Aya

답변

2

의견을 보면이 기능이 작동합니다 (필자 만의 가독성을 위해 약간의 변경을가 했음). 감사합니다 @ 아야!

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()