def set_metadata(self, metadata): """takes a MetaData object and sets this track's metadata this metadata includes track name, album name, and so on raises IOError if unable to write the file""" from audiotools.id3 import ID3v22Comment if metadata is None: return self.delete_metadata() elif self.get_metadata() is not None: # current file has metadata, so replace it with new metadata self.update_metadata(ID3v22Comment.converted(metadata)) else: # current file has no metadata, so append new ID3 block import os from audiotools.bitstream import BitstreamRecorder from audiotools import transfer_data, TemporaryFile if not os.access(self.filename, os.W_OK): raise IOError(self.filename) # turn our ID3v2.2 tag into a raw binary chunk id3_chunk = BitstreamRecorder(0) ID3v22Comment.converted(metadata).build(id3_chunk) # generate a temporary AIFF file in which our new ID3v2.2 chunk # is appended to the file's set of chunks new_aiff = TemporaryFile(self.filename) self.__class__.aiff_from_chunks( new_aiff, [c for c in self.chunks()] + [AIFF_Chunk(b"ID3 ", id3_chunk.bytes(), id3_chunk.data())]) new_aiff.close()
def get_metadata(self): """returns a MetaData object, or None raises IOError if unable to read the file""" from audiotools.bitstream import BitstreamReader from audiotools.id3 import ID3v22Comment for chunk in self.chunks(): if (chunk.id == 'ID3 '): return ID3v22Comment.parse(BitstreamReader(chunk.data(), 0)) else: return None