def getTags(self): path = self.uri self["size"] = os.path.getsize(path) audio = common.MutagenFile(path, common.FORMATS) # print audio.tags.getID3().getall('TLEN') if audio is not None: tag_keys_override = TAGS_KEYS_OVERRIDE.get( audio.__class__.__name__, None) for file_tag in coreTags: if tag_keys_override and tag_keys_override.has_key(file_tag): file_tag = tag_keys_override[file_tag] if audio.has_key(file_tag) and audio[file_tag]: value = audio[file_tag] if isinstance(value, list) or isinstance(value, tuple): value = value[0] fix_value = fix_charset(value) if fix_value == "[Invalid Encoding]": if tag == "title": fix_value = song.get_filename() else: fix_value = "" self[file_tag] = fix_value else: self[file_tag] = None for key in ['sample_rate', 'bitrate', 'length']: try: if key == 'length': self['duration'] = getattr(audio.info, key) else: self[key] = getattr(audio.info, key) except Exception, e: print e
def getMp3FontCover(self): from common import EasyMP3 audio = common.MutagenFile(self.uri, common.FORMATS) ext = None img_data = None if isinstance(audio, EasyMP3): apics = audio.tags.getID3().getall('APIC') if len(apics) > 0: apic = apics[0] if apic.type == 3: mine = apic.mime ext = mine.split('/')[-1] img_data = apic.data return ext, img_data
def write_to_file(self): ''' Save tag information to file. ''' if self.get_scheme() != "file": self.last_error = self.get_scheme() + " " + "Scheme not supported" return False if not utils.exists(self.get("uri")): self.last_error = self.get_filename() + " doesn't exist" return False if not os.access(self.get_path(), os.W_OK): self.last_error = self.get_filename( ) + " doesn't have enough permission" return False try: audio = common.MutagenFile(self.get_path(), common.FORMATS) tag_keys_override = None if audio is not None: if audio.tags is None: audio.add_tags() tag_keys_override = TAGS_KEYS_OVERRIDE.get( audio.__class__.__name__, None) for file_tag, tag in TAG_KEYS.iteritems(): if tag_keys_override and tag_keys_override.has_key( file_tag): file_tag = tag_keys_override[file_tag] if self.get(tag): value = unicode(self.get(tag)) audio[file_tag] = value else: try: del (audio[file_tag]) # TEST except KeyError: pass audio.save() else: raise "w:Song:MutagenTag:No audio found" except Exception, e: # print traceback.format_exc() print "W: Error while writting (" + self.get( "uri") + ")Tracback :", e self.last_error = "Error while writting" + ": " + self.get_filename( ) return False
def saveTags(self): ''' Save tag information to file. ''' if not self.isLocalFile(): self.last_error = self.url + " " + "is not a local file" return False if not self.isExisted(): self.last_error = self.url + " doesn't exist" return False if not os.access(self.url, os.W_OK): self.last_error = self.url + " doesn't have enough permission" return False try: audio = common.MutagenFile(self.url, common.FORMATS) tag_keys_override = None if audio is not None: if audio.tags is None: audio.add_tags() tag_keys_override = TAGS_KEYS_OVERRIDE.get( audio.__class__.__name__, None) for file_tag, tag in TAG_KEYS.iteritems(): if tag_keys_override and tag_keys_override.has_key( file_tag): file_tag = tag_keys_override[file_tag] if self.get(tag): try: if audio.__class__.__name__ == "MP4" and tag == "tracknumber": _value = self.get(tag) _vtuple = _value.split(',') if len(_vtuple) == 2: value = [(int(_vtuple[0][1:].strip()), int(_vtuple[1][:-1].strip()))] else: value = self.get(tag) else: value = self.get(tag) except Exception, e: value = self.get(tag).decode('utf-8') audio[file_tag] = value else: try: del (audio[file_tag]) # TEST except KeyError: pass audio.save() else:
def getMp3FontCover(self): from common import EasyMP3 ext = None img_data = None try: audio = common.MutagenFile(self.url, common.FORMATS) if isinstance(audio, EasyMP3): if audio.tags: apics = audio.tags.getID3().getall('APIC') if len(apics) > 0: apic = apics[0] if apic.type == 3: mine = apic.mime ext = mine.split('/')[-1] img_data = apic.data except Exception, e: logger.error(e)
def __read_from_local_file(self): try: path = self.get_path() self["#size"] = os.path.getsize(path) self["#mtime"] = os.path.getmtime(path) self["#ctime"] = os.path.getctime(path) audio = common.MutagenFile(self.get_path(), common.FORMATS) tag_keys_override = None if audio is not None: tag_keys_override = TAGS_KEYS_OVERRIDE.get( audio.__class__.__name__, None) for file_tag, tag in TAG_KEYS.iteritems(): if tag_keys_override and tag_keys_override.has_key( file_tag): file_tag = tag_keys_override[file_tag] if audio.has_key(file_tag) and audio[file_tag]: value = audio[file_tag] if isinstance(value, list) or isinstance(value, tuple): value = value[0] fix_value = utils.fix_charset(value) if fix_value == "[Invalid Encoding]": if tag == "title": fix_value = self.get_filename() else: fix_value = "" self[tag] = fix_value self["#duration"] = int(audio.info.length) * 1000 try: self["#bitrate"] = int(audio.info.bitrate) except AttributeError: pass else: raise "W:Song:MutagenTag:No audio found" self.load_sort_object() except Exception, e: # print "W: Error while Loading (" + self.get_path() + ")Tracback :", e self.last_error = "Error while reading" + ":" + self.get_filename() return False
def getTags(self): path = self.url self["size"] = os.path.getsize(path) self["ext"] = self.ext audio = common.MutagenFile(path, common.FORMATS) if audio is not None: tag_keys_override = TAGS_KEYS_OVERRIDE.get( audio.__class__.__name__, None) for tag, file_tag in TAG_KEYS.iteritems(): if tag_keys_override and tag_keys_override.has_key(file_tag): file_tag = tag_keys_override[file_tag] if audio.has_key(file_tag) and audio[file_tag]: value = audio[file_tag] if isinstance(value, list) or isinstance(value, tuple): value = value[0] if isinstance(value, mutagen.asf.ASFUnicodeAttribute): value = value.value fix_value = common.fix_charset(value) if fix_value == "[Invalid Encoding]": if tag == "title": fix_value = self.fileName else: fix_value = "" self[tag] = fix_value else: if tag == 'composer': self[tag] = '' else: self[tag] = 0 for key in ['sample_rate', 'bitrate', 'length']: try: if hasattr(audio.info, key): if key == 'length': self['duration'] = getattr(audio.info, key) * 1000 else: self[key] = getattr(audio.info, key) except Exception, e: logger.error(e)