[Tutor] How to map different keys together ?

spir ☣ denis.spir at gmail.com
Sun Apr 18 20:03:52 CEST 2010


On Sat, 17 Apr 2010 11:57:28 -0400
Damon Timm <damontimm at gmail.com> wrote:

> Hello - I am writing a script that converts an entire music library
> into a single desired output format.  The source music library has a
> variety of music filetypes (flac, mp3, m4a, ogg, etc) and I am
> attempting to use mutagen (a music file tagging module,
> http://code.google.com/p/mutagen/) in order to handle the tagging.
> 
> I am struggling, on a theoretical level, on how to map the various
> filetype's tag/key naming conventions.  I have already created my own
> MusicFile objects to handle each filetype (with functions for
> encoding/decoding) but I can't wrap my head around how to map the
> tags.
> 
> Here is a relevant (I think) example from mutagen for three different
> filetypes all with the same tags (but different keys identifying
> them).
> 
> >>> flac.keys()
> ['album', 'disctotal', 'artist', 'title', 'tracktotal', 'genre',
> 'composer', 'date', 'tracknumber', 'discnumber']
> >>> mp3.keys()
> ['TPOS', u'APIC:', 'TDRC', 'TIT2', 'TPE2', 'TPE1', 'TALB', 'TCON', 'TCOM']
> >>> mp4.keys()
> ['\xa9alb', 'tmpo', '\xa9ART', '\xa9cmt', '\xa9too', 'cpil',
> '----:com.apple.iTunes:iTunSMPB', '\xa9wrt', '\xa9nam', 'pgap',
> '\xa9gen', 'covr', 'disk', '----:com.apple.iTunes:Encoding Params',
> '----:com.apple.iTunes:iTunNORM']

Without the issue below with mp3, you could just do a triple (!) dict lookup. Using at best constants like TITLE to identify in a type-independant manner the field accessed, this could give:
   key = keys_per_type[FLAC][TITLE]
   title = album_data[key]
or in one go
   title = album_data[keys_per_type[FLAC][TITLE]]

> And here is what I would need to do to find the song's TITLE text:
> 
> >>> flac['title']
> [u'Christmas Waltz']
> >>> mp3['TIT2'].text  #notice this one takes another additional step, as well, by specifying text !

Yo, annoying!...

> [u'Christmas Waltz']
> >>> mp4['\xa9nam']
> [u"Christmas Waltz"]

... so maybe the best method is to write an access method per field, taking the type as param. Or a super access method taking both the type and field.

> In the end, after "the mapping", I would like to be able to do
> something along these approaches:
> 
> [1] >>> target_file.tags = src_file.tags
> [2] >>> target_file.set_tags(src_file.get_tags())

I would make an intermediate AlbumData type to handle the per-type mess, abstracting it for you as user.

album_data = AlbumData(src_file.tags)	# maybe type needed here
album_data.write(target_file)		# ditto?
or
target_file.write(album_data.format(type))
or something like that.

> However, none of the keys match, so I need to somehow map them to a
> central common source first ... and I am not sure about how to
> approach this.  I know I could manually assign each key to a class
> property (using the @property tag) ... but this seems tedious:

Not so sure it's more tedious. At least it's clear in serving code. (Just make sure there it is also clear on client code side.)

> Any insight on where I can start with mapping all these together?


> Thanks,
> Damon

Denis
________________________________

vit esse estrany ☣

spir.wikidot.com


More information about the Tutor mailing list