Created
June 19, 2013 14:37
-
-
Save kyv/5814824 to your computer and use it in GitHub Desktop.
Compare wave files in two folders for length. Shows which files in second folder are closest to the length of files in first folder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2 | |
import os, sys | |
import contextlib | |
import wave | |
def main(): | |
folders = [] | |
dir1, dir2 = parseArgs(sys.argv) | |
print dir1, dir2 | |
for folder in (dir1, dir2): | |
filesInFolder = [] | |
for r,d,f in os.walk(folder): | |
for files in f: | |
if files.endswith(".wav"): | |
fdata = ( getDuration(os.path.join(r,files)), files ) | |
filesInFolder.append(fdata) | |
folders.append(sorted(filesInFolder)) | |
printBoth(folders) | |
def parseArgs(argv): | |
if len(argv) < 3: | |
sys.exit('Usage: %s folder1 folder2' % argv[0]) | |
return argv[1], argv[2] | |
def getLonger(folders): | |
"""Return longest list""" | |
folder = "" | |
if len(folders[0]) >= len(folders[1]): | |
return folders[0], folders[1] | |
if len(folders[1]) > len(folders[0]): | |
return folders[1], folders[0] | |
def printBoth(folders): | |
longer, shorter = getLonger(folders) | |
for i in longer: | |
# use min get min distance between values | |
value, name = min(shorter, key=lambda x:abs(x[0]-i[0])) | |
print "%s: %d, %s: %d" % ( i[1], i[0], name, value) | |
def getDuration(fname): | |
with contextlib.closing(wave.open(fname,'r')) as f: | |
frames=f.getnframes() | |
rate=f.getframerate() | |
duration=frames/float(rate) | |
return(duration) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment