Re: [LAU] ID3/Ogg metadata copying?

From: Paul Winkler <pw_lists@email-addr-hidden>
Date: Thu Oct 25 2007 - 06:38:30 EEST

Hey Ken, I've done something similar.
The python libraries I ended up using were:
ogg.vorbis
pyid3lib

In one of my scripts, I have some code that might be a useful starting
point, though taken out of context it will surely need some fixing
up...

    def tag_all(self):
        """
        Tag all mp3 files in mp3dir with info from the corresponding
        ogg.
        """
        artist, title = self.artist, self.title
        mp3files = sorted(glob.glob(self.mp3dir + '/*mp3'))
        oggfiles = sorted(glob.glob(self.oggdir + '/*ogg'))
        self.log("Tagging %d files." % len(oggfiles))
        assert len(oggfiles) == len(mp3files)
        files = zip(oggfiles, mp3files)

        for i, t in enumerate(files):
            oggpath, mp3path = t
            self.comment_to_id3(i, len(files), oggpath, mp3path)

    def comment_to_id3(self, n, ntracks, oggpath, mp3path):
        # XXX note we could also have just used lame command line options
        # to insert the tag during encoding. Oh well, this works.
        oggfile = ogg.vorbis.VorbisFile(oggpath)
        comment = oggfile.comment().as_dict()
        mp3tag = pyid3lib.tag(mp3path)

        def _get(key, default=''):
            # Helper for getting single ascii strings out of weird
        vorbis
            # comments, and cleaning up fallback values that may be
            # filenames.
            value = comment.get(key)
            if value == None:
                value = [default.replace('_', ' ')]
            value = value[0].encode('ascii', 'ignore')
            return value

        #mp3tag.genre = _get('GENRE') #id3v2 has given up on genre, it was dumb
        mp3tag.artist = _get('ARTIST', self.artist)
        mp3tag.album = _get('ALBUM', self.title)
        default_name = os.path.basename(oggpath)[:-4]
        mp3tag.title = _get('TITLE', default_name)
        mp3tag.date = _get('DATE')
        tracknumber = _get('TRACKNUMBER', '%d/%d' % (n + 1, ntracks))
        tracknumber = [int(i) for i in tracknumber.split('/')]
        mp3tag.track = tuple(tracknumber)
        mp3tag.update()

-- 
Paul Winkler
http://www.slinkp.com
_______________________________________________
Linux-audio-user mailing list
Linux-audio-user@email-addr-hidden
http://lists.linuxaudio.org/mailman/listinfo/linux-audio-user
Received on Thu Oct 25 08:15:02 2007

This archive was generated by hypermail 2.1.8 : Thu Oct 25 2007 - 08:15:02 EEST