codekasten/normalizer.py

115 lines
2.9 KiB
Python
Raw Normal View History

2006-06-07 11:03:58 +02:00
#!/usr/bin/env python
## this is a dirty, lame hack by a dirty, lame guy
## - reads a .m3u file (e.g. generated by xmms)
## - converts any audiofile to wavefiles using incredible mplayer
## - provides normalized wavefiles using transcode
## - burns an audio-cd
# !!feel free to copy, modify, redistribute and at most _use_ it!!
# AGE/02004
import os,sys
import string
#recorder = "ATAPI:0,0,0" #fuer cdrecord mit 2.6 kernel und ide brenner
recorder = "0,0,0" #fuer cdrecord mit 2.6 kernel und ide brenner
__doc__ = """usage: %s file.m3u wav-destination""" % sys.argv[0]
################
### init checks
try:
m3ufile = sys.argv[1]
wavdir = sys.argv[2]
if not os.access(wavdir,os.W_OK) :
print "%s not writeable" % wavdir
sys.exit(3)
except:
print >>sys.stderr, __doc__
sys.exit(1)
print "\nDirty, lame hack in action:\n"
################
### m3u einlesen
try:
temp = open(m3ufile,"r")
m3u = temp.read()
temp.close()
except:
print "I can't find the .m3u file"
sys.exit(2)
# M's Kommentar Schnibbler
notfound = 0
while notfound == 0:
startpos = string.find(m3u,"#")
if startpos <0:
notfound +=1
endpos = string.find(m3u,"\n",startpos)
cutme = m3u[startpos:endpos]
m3u = string.replace(m3u,cutme,"")
valuepairs = string.split(m3u,"\n")
m3u = []
for i in valuepairs:
if i != "":
m3u.append(i)
# im m3u-array stehen jetzt hoffentlich ;) nur noch pfadangaben
# checken ob's stimmt & und ob alle files existieren
abort = 0
for i in m3u:
if not os.path.exists(i):
abort = abort + 1
print "===> not found: %s " % i
m3u.remove(i)
if abort > 0:
print "\nI can't find %i audiofiles - giving up" % abort
#TODO
# abfrage ob ohne die files mit gekuertzter m3u weiter gemacht werden
# soll..
sys.exit(0)
else:
print "\nI found %i audiofiles to convert, normalize and burn:" % len(m3u)
##############
### ogg/mp3 -> wav
count = 0
for file in m3u:
count+=1
# fuehrende nullen ranballern
#if count < 10:
#count0 = "0"+str(count)
#else:
#count0 = str(count)
count0 = "%.3i" % count
command = "mplayer \""+file+"\" -vc dummy -vo null -ao pcm -aofile "+wavdir+"/"+str(count0)+".wav &> /dev/null"
print "\nexec: "+command
session = os.popen(command,"w")
session.write("")
session.flush()
session.close()
#########################
### wav -> normalized wav
wavefiles = os.listdir(wavdir)
for wave in wavefiles:
command = "transcode -J normalize -y wav -i "+wavdir+"/"+wave+" -m "+wavdir+"/"+ wave+".norm &> /dev/null"
print "\nexec: "+command
session = os.popen(command,"w")
session.write("")
session.flush()
session.close()
##################
### wav -> audiocd
command = "sudo cdrecord -v -dev="+recorder+" -pad -audio "+wavdir+"/*.wav.norm"
print "\nexec: "+command
print "\nwarning: If this fails, try to change the \"recorder\" variable!"
session = os.popen(command,"w")
session.write("")
session.flush()
session.close()
#print "\n That's it! Eine Stunde coden und dafuer jetzt den Sommer geniessen..."
print "done"