View Single Post
      11-28-2009, 01:07 AM   #6
doug_335ic
New Member
 
doug_335ic's Avatar
 
Drives: 2011 MINI Countryman
Join Date: Oct 2009
Location: TX

Posts: 7
iTrader: (0)

Idrive backup MP3 to USB (script)

Thanks! Flipping the bytes worked. Attached Python script (running on SUSE 11.1 32-bit PC) takes my OGG/VORBIS collection of music and puts it in proper format on a USB for a BMW iDrive restore. Output looks like:

Removing existing /media/disk/BMWData/Music/*
1/4 Tool,Lateralus,1:The Grudge... MP3, ID3, Flipped.
2/4 Tool,Lateralus,1:Eon Blue Apocalypse... MP3, ID3, Flipped.
3/4 System of a Down,Mezmerize,1:Soldier Side - Intro... MP3, ID3, Flipped.
4/4 System of a Down,Mezmerize,1:B.Y.O.B.... MP3, ID3, Flipped.
Writing out data_1 index
Done. Size in Mb should be under 125000 for a 2010 iDrive
18 /media/disk/BMWData/Music/

#!/usr/bin/python
import os,struct,sys,string
#----------------------------------------------------------------------
# In my case, I've got a number of ogg files with embedded vorbis
# comments. Script is run in some directory within that ogg
# collection, and it reads all the ogg files below that point
# converting the vorbis comments to id3 tags, creating the BR4
# files BMW wants. You do have to create the BMWData / Music /
# data_1 file with all the directory names to read for it to
# restore properly. So for a different car than my 2010, you could
# make a backup and then see if your structure is the same as my:
#
# BMWData
# BMWBackup.ver
# Music
# data_1 <-script creates this
# media_directory_1 <-script creates this
# media_directory_2 <-script creates this
#
# ...and update the script as necessary. Code_1 and Code_2
# could also be unique to my car or the year or model. I just
# copy a bunch of CD directories ripped in ogg format for other
# players into some fresh directory and then run this script
# from the top of that fresh directory to get a new USB load for
# the vehicle.
#----------------------------------------------------------------------

# base_directory=os.getenv('HOME')+'/conversion/BMWData/Music/'
base_directory='/media/disk/BMWData/Music/'
data_1=[]
code_1='1258287780'
code_2='8'
quality='160k' # Or you can go down to 128k rate MP3 if acceptable
# I think a -q6 setting ogg file is the equivalent of a 192k MP3, but
# in a car I dunno if anyone but the most refined audiophile could
# hear the difference on our BMW speakers between 160k and a higher
# fidelity (and thus much larger) file.
map={'.BR3':'.mp4','.BR4':'.mp3','.BR5':'.wma',
'.mp4':'.BR3','.mp3':'.BR4','.wma':'.BR5'}
number_of_files=0

print 'Removing existing',base_directory+'*'
# Caution: the following line removes existing USB music
# collection. In your environment change the base_directory and
# comment out this line until it all works to your satisfaction.
os.system('rm -rf '+base_directory+'*')

# Get the count of all .ogg files below this point...
for root, dirs, files in os.walk('.', topdown=True):
for name in files:
filename=os.path.join(root, name)
if filename.find('.ogg')>0:
number_of_files+=1
file_number=0

# And then process all those .ogg files into .BR4 files
for root, dirs, files in os.walk('.', topdown=True):
for name in files:
filename=os.path.join(root, name)
if filename.find('.ogg')>0:
file_number+=1
os.system('vorbiscomment -l '+filename+' > foo')
fh=open('foo')
fd=fh.readlines()
os.unlink('foo')
the_title=filename[filename.rfind('/')+1:-4]
the_artist='Unknown'
the_genre='Unknown'
the_date='2009'
the_album='Unknown'
the_track='1'
for l in fd:
if l.find('title=')==0: the_title=l[6:-1]
elif l.find('artist=')==0: the_artist=l[7:-1]
elif l.find('genre=')==0: the_genre=l[6:-1]
elif l.find('date=')==0: the_date=l[5:-1]
elif l.find('album=')==0: the_album=l[6:-1]
elif l.find('tracknumber=')==0: the_track=str(int(l[12:-1]))
album_directory=string.lower(the_album.replace(' ','_'))
data_1_entry='/'+album_directory+'/\t'+the_album+'\t'+code_1+'\t'+code_2+'\t\n'

if not os.access(base_directory+album_directory,os.R_OK):
os.mkdir(base_directory+album_directory)
output_filename=base_directory+album_directory+'/'+the_title+'.mp3'
sys.stdout.write(' '+str(file_number)+'/'+str(number_of_files)+' '+the_artist+','+the_album+','+the_track+':'+the_t itle+'...')
sys.stdout.flush()
# Create the mp3 from the ogg in the correct USB directory
if os.access(output_filename,os.R_OK):
os.unlink(output_filename)
command='ffmpeg -i "'+filename+'" -ab '+quality+' "'+output_filename+'" >& /dev/null'
os.system(command)
sys.stdout.write(' MP3,')
sys.stdout.flush()
# Put the tag back on
command= 'id3v2 -a "'+the_artist+'" -A "'+the_album+'" -t "'+the_title+\
'" -g "'+the_genre+'" -y "'+the_date+'" -T "'+the_track+'" "'+\
output_filename+'" >& /dev/null'
os.system(command)
sys.stdout.write(' ID3,')
sys.stdout.flush()
bmw_name=output_filename.replace('.mp3',map['.mp3'])
ih=open(output_filename,'rb')
oh=open(bmw_name,'wb')
while True:
element = ih.read(1)
if len(element)>0:
mybyte = struct.unpack("<b",element)[0]
inverted_data = ~ mybyte
oh.write(struct.pack(">b",inverted_data))
else: break
oh.close()
sys.stdout.write(' Flipped.\n')
os.unlink(output_filename)
# Remove the .mp3 file now that the .BR4 is there...
sys.stdout.flush()
if not data_1_entry in data_1:
data_1.append(data_1_entry)
elif filename.find('.BR5')>0:
# This was just to test/verify that a BR5 was just a flipped wma
ih=open(filename,'rb')
bmw_name=filename.replace('.BR5',map['.BR5'])
oh=open(bmw_name,'wb')
while True:
# I know... one byte at a time is too slow...
element = ih.read(1)
if len(element)>0:
mybyte = struct.unpack("<b",element)[0]
inverted_data = ~ mybyte
oh.write(struct.pack(">b",inverted_data))
else: break
oh.close()

print 'Writing out data_1 index'
data_1.sort()
fh=open(base_directory+'data_1','w')
fh.writelines(data_1)
fh.close()

print 'Done. Size in Mb should be under 125000 for a 2010 iDrive'
os.system('du -ms '+base_directory)
doug_335ic is offline  
0
Reply With Quote