[LAU] ID3/Ogg metadata copying?

Paul Winkler pw_lists at slinkp.com
Wed Oct 24 23:38:30 EDT 2007


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



More information about the Linux-audio-user mailing list