(noob alert) why doesn't this work?

2005-03-22 Thread Bouke Woudstra
Hi,

I'm a bit stuck with this python script. It's aim is to encode all flac files 
to wav and then to mp3. The only problem I have is to preserve the tags. The 
code works when there's just one flac file in a directory but fails for more. 
I can't see why the for loop fails here (flactags). 

The error thrown is: UnboundLocalError: local variable 'title' referenced 
before assignment

I don't understand why this isn't assigned. I suspect that self.wav2mp3 
doesn't wait for the first part to finish, but how to force it to wait?

Can somebody help please? 

The code:

#!/usr/bin/env python
import os
import glob
import sys

class program:

def makewavdir(self):
if os.path.isdir('wav'):
print There already exists a directory named wav
print The album is probably already decoded to wav
else:
os.mkdir('wav')

def flac2wav(self):
flacfiles = glob.glob('*flac')
for flac in flacfiles:
print '*' * 70
print 'Starting decoding %s to wav' % flac
os.system('flac -d -s %s -o wav/%s.wav' % (flac, 
flac[0:-5]))
print '*' * 70
print All files are decoded to wav in the folder wav
print '*' * 70
print

def makemp3dir(self):
if os.path.isdir('mp3'):
print There already exists a directory named mp3
print The album is probably already encoded in mp3
else:
os.mkdir('mp3')

def wav2mp3(self, flac, title, artist, album, year, number, genre):
os.system('lame --preset standard -V 2 --tt %s --ta %s --tl 
%s  --ty \ 
%s --tn %s --tg %s --id3v2-only wav/%s.wav 
mp3/%s.mp3' %(title, \
 
artist, album, year, number, genre, flac[:-5], flac[:-5]))

def flactags(self):
flacfiles = glob.glob('*flac')
flacfiles.sort()
for flac in flacfiles:
cmd = 'metaflac --export-tags=- %s' % flac
for line in os.popen(cmd).readlines():
if 'Artist' in line:
artist = line[7:-1]
if 'Album' in line:
album = line[6:-1]
if 'Date' in line:
year = line[5:-1]
if 'Title' in line:
title = line[6:-1]
if 'Tracknumber' in line:
number = line[12:-1]
if 'Genre' in line:
genre = line[6:-1]
self.wav2mp3(flac, title, artist, album, year, number, 
genre)


x = program()
x.makewavdir()
x.flac2wav()
x.makemp3dir()
x.flactags()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: (noob alert) why doesn't this work?

2005-03-22 Thread Bouke Woudstra
Thanks for all suggestions. Your first point I knew, but was too lazy to type 
it. It made no difference for the test files had all tags. Knowing that it 
was not a asynchrous thing helped me a lot though. There had to be something 
wrong with the command line for metaflac. 

It turned out that some flac files have tags like Artist=artistname and others 
have artist=artistname. Therefore it couldn't find the artist! So now I just 
look for 'rtist=' which works great.

Thanks for all the help!

Op dinsdag 22 maart 2005 12:18, schreef Simon Brunning:
 On Tue, 22 Mar 2005 12:10:50 +0100, Bouke Woudstra

 [EMAIL PROTECTED] wrote:
  Hi,
 
  I'm a bit stuck with this python script. It's aim is to encode all flac
  files to wav and then to mp3. The only problem I have is to preserve the
  tags. The code works when there's just one flac file in a directory but
  fails for more. I can't see why the for loop fails here (flactags).
 
  The error thrown is: UnboundLocalError: local variable 'title' referenced
  before assignment

 Perhaps you get to line 56 without having gone through line 51? Might
 one of your flac files not have a title line?

  I don't understand why this isn't assigned. I suspect that self.wav2mp3
  doesn't wait for the first part to finish, but how to force it to wait?

 Nope. Looks asynchronous to me.

 --
 Cheers,
 Simon B,
 [EMAIL PROTECTED],
 http://www.brunningonline.net/simon/blog/
-- 
http://mail.python.org/mailman/listinfo/python-list