def __init__(self, fpath): # Forcing the unicode through try : fpath = fpath.decode("utf-8") except : pass if not mmp.file_playable(fpath): raise BadSongFile(fpath) try : full_mutagen = mutagen.File(fpath, easy=True) except Exception : raise BadSongFile(fpath) self.path = fpath if not os.path.exists(self.path): self.logger.info("Attempting to read metadata of file \ that does not exist. Setting metadata to {}") self.__metadata = {} return # TODO : Simplify the way all of these rules are handled right now it's # extremely unclear and needs to be refactored. #if full_mutagen is None: raise BadSongFile(fpath) if full_mutagen is None: full_mutagen = FakeMutagen(fpath) self.__metadata = Metadata.airtime_dict(full_mutagen) # Now we extra the special values that are calculated from the mutagen # object itself: if mmp.extension(fpath) == 'wav': full_mutagen.set_length(mmp.read_wave_duration(fpath)) for special_key,f in airtime_special.iteritems(): try: new_val = f(full_mutagen) if new_val is not None: self.__metadata[special_key] = new_val except Exception as e: self.logger.info("Could not get special key %s for %s" % (special_key, fpath)) self.logger.info(str(e)) # Finally, we "normalize" all the metadata here: self.__metadata = mmp.normalized_metadata(self.__metadata, fpath) # Now we must load the md5: # TODO : perhaps we shouldn't hard code how many bytes we're reading # from the file? self.__metadata['MDATA_KEY_MD5'] = mmp.file_md5(fpath,max_length=100)
def normalize_mutagen(path): """ Consumes a path and reads the metadata using mutagen. normalizes some of the metadata that isn't read through the mutagen hash """ if not mmp.file_playable(path): raise BadSongFile(path) try: m = mutagen.File(path, easy=True) except Exception: raise BadSongFile(path) if m is None: m = FakeMutagen(path) try: if mmp.extension(path) == 'wav': m.set_length(mmp.read_wave_duration(path)) except Exception: raise BadSongFile(path) md = {} for k, v in m.iteritems(): if type(v) is list: if len(v) > 0: md[k] = v[0] else: md[k] = v # populate special metadata values md['length'] = getattr(m.info, 'length', 0.0) md['bitrate'] = getattr(m.info, 'bitrate', u'') md['sample_rate'] = getattr(m.info, 'sample_rate', 0) md['mime'] = m.mime[0] if len(m.mime) > 0 else u'' md['path'] = normpath(path) # silence detect(set default cue in and out) #try: #command = ['silan', '-b', '-f', 'JSON', md['path']] #proc = subprocess.Popen(command, stdout=subprocess.PIPE) #out = proc.communicate()[0].strip('\r\n') #info = json.loads(out) #md['cuein'] = info['sound'][0][0] #md['cueout'] = info['sound'][0][1] #except Exception: #self.logger.debug('silan is missing') if 'title' not in md: md['title'] = u'' return md
def normalize_mutagen(path): """ Consumes a path and reads the metadata using mutagen. normalizes some of the metadata that isn't read through the mutagen hash """ if not mmp.file_playable(path): raise BadSongFile(path) try : m = mutagen.File(path, easy=True) except Exception : raise BadSongFile(path) if m is None: m = FakeMutagen(path) try: if mmp.extension(path) == 'wav': m.set_length(mmp.read_wave_duration(path)) except Exception: raise BadSongFile(path) md = {} for k,v in m.iteritems(): if type(v) is list: if len(v) > 0: md[k] = v[0] else: md[k] = v # populate special metadata values md['length'] = getattr(m.info, 'length', 0.0) md['bitrate'] = getattr(m.info, 'bitrate', u'') md['sample_rate'] = getattr(m.info, 'sample_rate', 0) md['mime'] = m.mime[0] if len(m.mime) > 0 else u'' md['path'] = normpath(path) # silence detect(set default cue in and out) #try: #command = ['silan', '-b', '-f', 'JSON', md['path']] #proc = subprocess.Popen(command, stdout=subprocess.PIPE) #out = proc.communicate()[0].strip('\r\n') #info = json.loads(out) #md['cuein'] = info['sound'][0][0] #md['cueout'] = info['sound'][0][1] #except Exception: #self.logger.debug('silan is missing') if 'title' not in md: md['title'] = u'' return md