Added special handling for wav

This commit is contained in:
Rudi Grinberg 2012-09-13 16:26:31 -04:00
parent 251e0f054a
commit 4349dbe4fd
1 changed files with 21 additions and 5 deletions

View File

@ -3,6 +3,9 @@ import mutagen
import math import math
import os import os
import copy import copy
import wave
import contextlib
from collections import namedtuple
from mutagen.easymp4 import EasyMP4KeyError from mutagen.easymp4 import EasyMP4KeyError
from media.monitor.exceptions import BadSongFile from media.monitor.exceptions import BadSongFile
@ -43,15 +46,19 @@ airtime2mutagen = {
class FakeMutagen(dict): class FakeMutagen(dict):
""" """
Need this fake mutagen object so that airtime_special functions return a Need this fake mutagen object so that airtime_special functions
proper default value instead of throwing an exceptions for files that return a proper default value instead of throwing an exceptions for
mutagen doesn't recognize files that mutagen doesn't recognize
""" """
FakeInfo = namedtuple('FakeInfo','length bitrate')
def __init__(self,path): def __init__(self,path):
self.path = path self.path = path
self.mime = True self.mime = []
self.info = True self.info = FakeMutagen.FakeInfo(0.0, '')
dict.__init__(self) dict.__init__(self)
def set_length(self,l):
old_bitrate = self.info.bitrate
self.info = FakeMutagen.FakeInfo(l, old_bitrate)
# Some airtime attributes are special because they must use the mutagen object # Some airtime attributes are special because they must use the mutagen object
# itself to calculate the value that they need. The lambda associated with each # itself to calculate the value that they need. The lambda associated with each
@ -204,6 +211,15 @@ class Metadata(Loggable):
self.logger.info("Could not get special key %s for %s" % self.logger.info("Could not get special key %s for %s" %
(special_key, fpath)) (special_key, fpath))
self.logger.info(str(e)) self.logger.info(str(e))
# Hickity Hackity for .wav files. Properly do this later
if mmp.extension(fpath) == 'wav':
with contextlib.closing(wave.open(fpath,'r')) as f:
frames=f.getnframes()
rate=f.getframerate()
duration=frames/float(rate)
full_mutagen.set_length(duration)
# Finally, we "normalize" all the metadata here: # Finally, we "normalize" all the metadata here:
self.__metadata = mmp.normalized_metadata(self.__metadata, fpath) self.__metadata = mmp.normalized_metadata(self.__metadata, fpath)
# Now we must load the md5: # Now we must load the md5: