#!/usr/bin/env python # overwrites the samplerate in a WAV file with a new value import sys from struct import * def replace_samplerate(fname, samprate): n = open(fname, 'r+') head = n.read(44) e = unpack('<22shiihh8s', head) f = [] f.extend(e) f[2] = samprate # ByteRate = SampleRate * NumChannels * BitsPerSample/8 f[3] = f[2] * f[1] * f[5]/8 head2 = pack('<22shiihh8s', f[0],f[1],f[2],f[3],f[4],f[5],f[6]) n.seek(0) n.write(head2) n.close() if len(sys.argv) > 1: # first arg is new samplerate newrate = int(sys.argv[1]) # remaining args are filenames for fname in sys.argv[2:]: try: replace_samplerate(fname, newrate) print 'converted %s to %d' % (fname, newrate) except Exception,ex: print "Error converting %s: %s" % (fname, str(ex))