def set_key(self, key, value): self.audio[key] = value self.audio.save() audio = M4A(self.audio.filename) self.failUnless(key in audio) self.failUnlessEqual(audio[key], value) self.faad()
def get_play_length(file_name, file_format): """Find the length of the track in seconds""" from mutagen.mp3 import MP3 from mutagen.m4a import M4A if file_format == 'mp3': audio = MP3(file_name) elif file_format == 'm4a': audio = M4A(file_name) else: raise Exception('Cannot get play length for format %s', file_format) play_length = audio.info.length logging.debug('Play length %s', play_length) return play_length
def test_fail(self): self.assertRaises(error, M4A, self.SOME_FILE) self.assertRaises(error, delete, self.SOME_FILE) self.assertRaises(error, delete, self.SOME_FILE) M4AInfo # flake8 with warnings.catch_warnings(): warnings.simplefilter("ignore", DeprecationWarning) a = M4A() a.add_tags() self.assertEqual(a.tags.items(), []) some_cover = M4ACover(b"foo", M4ACover.FORMAT_JPEG) self.assertEqual(some_cover.imageformat, M4ACover.FORMAT_JPEG) tags = M4ATags() self.assertRaises(error, tags.save, self.SOME_FILE)
def test_shrink(self): map(self.audio.__delitem__, self.audio.keys()) self.audio.save() audio = M4A(self.audio.filename) self.failIf(self.audio.tags)
def test_module_delete(self): delete(self.filename) audio = M4A(self.audio.filename) self.failIf(audio.tags) self.faad()
def test_delete(self): self.audio.delete() audio = M4A(self.audio.filename) self.failIf(audio.tags) self.faad()
def setUp(self): fd, self.filename = mkstemp(suffix='m4a') os.close(fd) shutil.copy(self.original, self.filename) self.audio = M4A(self.filename)
def save(self, force_insert=False, force_update=False): # don't Call the real save method # super(SongFile, self).save(force_insert, force_update) # Get the temporary file try: temp_file = self.file.file original_file = self.file.file.file except AttributeError: temp_file = self.file original_file = temp_file # Get the filetype filetype = (temp_file.name.rpartition(".")[2]).lower() # Save backup of old names old_name = self.name old_artist = self.artist old_album = self.album old_track_number = self.track_number # First obtain all metadata try: if filetype == "mp3": from mutagen.mp3 import MP3 mp3 = MP3(original_file.name) self.name = mp3["TIT2"].text[0] self.artist = mp3["TPE1"].text[0] self.album = mp3["TALB"].text[0] try: self.track_number = int(mp3["TRCK"][0].partition("/")[0]) except (ValueError, KeyError): self.track_number = 0 elif filetype in ("flac","ogg"): info = None if filetype == "flac": from mutagen.flac import FLAC info = FLAC(original_file.name) elif filetype == "ogg": from mutagen.oggvorbis import OggVorbis info = OggVorbis(original_file.name) self.name = self.print_list(info["title"]) self.artist = self.print_list(info["artist"]) self.album = self.print_list(info["album"]) try: self.track_number = int(self.print_list(info["tracknumber"])) except ValueError: self.track_number = 0 elif filetype == "m4a": from mutagen.m4a import M4A info = M4A(original_file.name) self.name = info["\xa9nam"] self.artist = info["\xa9ART"] self.album = info["\xa9alb"] try: self.track_number = int(info["trkn"][0]) except (ValueError, KeyError): self.track_number = 0 else: raise KeyError except KeyError: self.name = original_file.name.rpartition("/")[2] self.artist = "Unknown" self.album = "Unknown" self.track_number = 0 # Check if manual data was entered if old_name: self.name = old_name if old_artist: self.artist = old_artist if old_album: self.album = old_album if old_track_number: self.track_number = old_track_number # Generate relevant paths # /home/doppler/Project/git/partybeat/music music_directory = "{0}music".format(settings.MEDIA_ROOT) # artist/album/ music_sub_directory = "{0}/{1}/".format(self.artist, self.album).lower().replace(" ","_") # /home/doppler/Project/git/partybeat/music/artist/album destination_directory = "{0}/{1}".format(music_directory, music_sub_directory) # name.ext music_file = "{0}.{1}".format(self.name, filetype).lower().replace(" ","_") # Now move the temporary file to its resting place mkdir_p(destination_directory) destination_file = "".join([destination_directory, music_file]) tmp_file_name = original_file.name shutil.copy(tmp_file_name, destination_file) os.remove(tmp_file_name) # Also remove the directory if its now empty prune_dir(tmp_file_name) self.file = destination_file super(SongFile, self).save(force_insert, force_update)
def __init__(self, file_path): self.audio_info = M4A(file_path)
def __init__(self, path): self.path = path self.audio = M4A(path) self.metadata = {'format': 'm4a'} self.loadMetadata()