Esempio n. 1
0
    def readID3v2Frame(self):
        frameHeader = self.readFromTag(10, "ID3v2 frame header")

        if ord(frameHeader[:1]) == 0:
            self.expectPadding(frameHeader[1:])
            return

        frameId         = frameHeader[:4]
        frameBodyLength = bigEndianInteger(frameHeader[4:8])
        frameFlags      = bigEndianInteger(frameHeader[9:10])

        if frameFlags & 0x20:
            self.readFromTag(1, "ID3v2 frame group identifier byte")

        try:
            frameBody = self.readFromTag(frameBodyLength, "ID3v2 '%s' frame body" % (frameId))

        except Mp3FileError as error:
            if getOption('warning'):
                sys.stderr.write("warning: " + str(error) + ": skipping remainder of ID3v2 tag\n")

            return False

        if (self.expect("ignoring '%s' frame: compression flag" % (frameId), frameFlags & 0x80, 0) or
            self.expect("ignoring '%s' frame: encryption flag"  % (frameId), frameFlags & 0x40, 0)):
            return True

        if frameId[0] == "T" or frameId == "IPLS":
            encoding = ord(frameBody[0])
            self.requireMember("encoding descriptor for %s frame" % (frameId), encoding, {0, 1, 2, 3})
            value = frameBody[1:].decode(id3v2EncodingToPythonEncoding[encoding], errors="strict")

            if frameId == "TRCK":
                if value == "":
                    warn("TRCK tag is present but empty (ignoring it)", self.filePath, self.stream.tell())
                    return False

                self.track = int(value.split("/")[0])

        else:
            value = frameBody

        self.frames[frameId] = value
        return True
Esempio n. 2
0
    def __init__(self, filePath, rootPath=None):
        MuseFile.__init__(self, filePath, rootPath)
        self.artist = None
        self.album  = None
        self.md5    = None
        self.frames = {}

        # Determine meta-data from file path

        match = AudioFile.splitPattern.match(filePath)

        if not match:
            raise AudioFileError(filePath, "Failed to parse file path")

        (dirName, fileName, fileExt) = match.group(1, 2, 3)
        match = AudioFile.dirPattern.match(dirName)

        if not match:
           raise AudioFileError(filePath, "Failed to parse directory name " + dirName)

        (dirLetter, dirArtist, dirAlbum) = match.group(1, 2, 3)

        if not match.group(2) and match.group(3):
            dirAlbum  = None

            if dirLetter or len(match.group(3)) > 1:
                dirArtist = match.group(3)
            else:
                dirLetter = match.group(3)

        #print "Directory: Letter %s Artist %s Album %s" % (dirLetter, dirArtist, dirAlbum)
        match = AudioFile.filePattern.match(fileName)

        if not match:
            raise AudioFileError(filePath, "Failed to parse file name " + fileName)

        (fileArtist, fileAlbum, self.track, self.title) = match.group(1, 2, 3, 4)
        #print ("File name: Letter %s Artist %s Album %s Track %s Song %s Ext %s" %
        #       (dirLetter, fileArtist, fileAlbum, self.track, self.title, fileExt))

        self.artist = reconcileStrings(dirArtist, fileArtist, default="Unknown")

        if self.artist == "Unknown":
            warn("Failed to determine an artist from the filepath", filePath)

        elif self.artist == None:
            self.artist = dirArtist
            match       = AudioFile.numericPattern.match(fileArtist)

            if match and self.track == None:
                self.track = match.group(1)
                fileArtist = match.group(2)
                fileAlbum  = None

            if fileArtist:
                match = AudioFile.artistAlbumPattern.match(fileArtist)

                if match and reconcileStrings(dirArtist, match.group(1)) and reconcileStrings(dirAlbum, match.group(2)):
                    fileAlbum = match.group(2)
                else:
                    match = AudioFile.withArtistPattern.match(fileArtist)

                    if (not (match and (reconcileStrings(match.group(1), dirArtist) or reconcileStrings(match.group(2), dirArtist)))
                        and not simpleString(dirArtist) == simpleString(fileArtist).replace("_", " ")):
                        error("Directory artist '%s' differs from file name artist '%s'" % (dirArtist, fileArtist), filePath)

        self.album = reconcileStrings(dirAlbum, fileAlbum, default="Unknown")

        if self.album == "Unknown":
            info("Failed to determine an album from the filepath", filePath)    # Too common to warn on

        elif self.album == None:
            self.album = dirAlbum

            if fileArtist.isdigit() and reconcileStrings(fileAlbum, dirArtist):
                self.track = fileArtist

            elif not reconcileStrings(fileAlbum, dirAlbum):
                match = AudioFile.yearAlbumPattern.match(dirAlbum)

                if match and reconcileStrings(fileAlbum, match.group(2)):
                    self.year  = match.group(1)
                    self.album = match.group(2)

                else:
                    error("Directory album '%s' differs from file name album '%s'" % (dirAlbum, fileAlbum), filePath)

        newPath = ("%s/%s%s%s%s.%s"
                   % (dirName, safeAppend(self.artist, " - ", suppress="Unknown"), safeAppend(self.album, " - ", suppress="Unknown"),
                      safeAppend(self.track, " - "), self.title, fileExt))

        if getOption("fix", default=False) and newPath != filePath and takeAction("rename %s to %s" % (filePath, newPath)):
            os.rename(filePath, newPath)
            os.utime(newPath, None)