(noob alert) why doesn't this work?

B

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

Diez B. Roggisch

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

That should be pretty obvious: The UnboundLocalError comes up when you try
to access a variable that hasn't been assigned a value before. E.g try this
in an interactive python session:

foo = "hello"
print foo
print bar # This will raise UnboundLocalError

Now in your code, you have a conditional setting of diverse variables. So
only if

if 'Title' in line:
   title = line[6:-1]

executes, a title is there. Later, you _always_ use title. So you have to do
it like this:

title = "Unknown" # or empty or whatever
if 'Title' in line:
   title = line[6:-1]

Then title will always be there.

The reason that it works for _one_ but not for all is simply that by chance
the one file _had_ a title, but at least one of all the files hadn't. So it
crashes. If you'd only try that file, it would also crash with only one
file.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top