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 delete_metadata(self): """deletes the track's MetaData this removes or unsets tags as necessary in order to remove all data raises IOError if unable to write the file""" import os from audiotools import (TemporaryFile, LimitedFileReader, transfer_data) #this works a lot like update_metadata #but without any new metadata to set if (not os.access(self.filename, os.W_OK)): raise IOError(self.filename) new_mp3 = TemporaryFile(self.filename) #get the original MP3 data old_mp3 = open(self.filename, "rb") MP3Audio.__find_last_mp3_frame__(old_mp3) data_end = old_mp3.tell() old_mp3.seek(0, 0) MP3Audio.__find_mp3_start__(old_mp3) data_start = old_mp3.tell() old_mp3 = LimitedFileReader(old_mp3, data_end - data_start) #write data to file transfer_data(old_mp3.read, new_mp3.write) #commit change to disk old_mp3.close() new_mp3.close()
def delete_metadata(self): """deletes the track's MetaData this removes or unsets tags as necessary in order to remove all data raises IOError if unable to write the file""" import os from audiotools import (transfer_data, LimitedFileReader, TemporaryFile) from audiotools.id3 import skip_id3v2_comment if (not os.access(self.filename, os.W_OK)): raise IOError(self.filename) # overwrite original with no tags attached old_tta = open(self.filename, "rb") skip_id3v2_comment(old_tta) old_tta = LimitedFileReader(old_tta, self.data_size()) new_tta = TemporaryFile(self.filename) transfer_data(old_tta.read, new_tta.write) old_tta.close() new_tta.close()
def update_metadata(self, metadata): """takes this track's current MetaData object as returned by get_metadata() and sets this track's metadata with any fields updated in that object raises IOError if unable to write the file """ from audiotools import transfer_data, TemporaryFile from audiotools.id3 import ID3v22Comment from audiotools.bitstream import BitstreamRecorder from audiotools.text import ERR_FOREIGN_METADATA import os if metadata is None: return elif not isinstance(metadata, ID3v22Comment): raise ValueError(ERR_FOREIGN_METADATA) elif 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) metadata.build(id3_chunk) # generate a temporary AIFF file in which our new ID3v2.2 chunk # replaces the existing ID3v2.2 chunk new_aiff = TemporaryFile(self.filename) self.__class__.aiff_from_chunks( new_aiff, [(chunk if chunk.id != b"ID3 " else AIFF_Chunk( b"ID3 ", id3_chunk.bytes(), id3_chunk.data())) for chunk in self.chunks()]) new_aiff.close()
def update_metadata(self, metadata): """takes this track's current MetaData object as returned by get_metadata() and sets this track's metadata with any fields updated in that object raises IOError if unable to write the file """ if (metadata is None): return elif (not isinstance(metadata, ApeTag)): from audiotools.text import ERR_FOREIGN_METADATA raise ValueError(ERR_FOREIGN_METADATA) from audiotools.bitstream import BitstreamReader, BitstreamWriter from audiotools import transfer_data f = open(self.filename, "r+b") f.seek(-32, 2) (preamble, version, tag_size, item_count, read_only, item_encoding, is_header, no_footer, has_header) = BitstreamReader(f, 1).parse(ApeTag.HEADER_FORMAT) if ((preamble == 'APETAGEX') and (version == 2000)): if (has_header): old_tag_size = 32 + tag_size else: old_tag_size = tag_size if (metadata.total_size() >= old_tag_size): # metadata has grown # so append it to existing file f.seek(-old_tag_size, 2) metadata.build(BitstreamWriter(f, 1)) else: # metadata has shrunk # so rewrite file with smaller metadata from audiotools import TemporaryFile from os.path import getsize # copy everything but the last "old_tag_size" bytes # from existing file to rewritten file new_apev2 = TemporaryFile(self.filename) old_apev2 = open(self.filename, "rb") limited_transfer_data(old_apev2.read, new_apev2.write, getsize(self.filename) - old_tag_size) # append new tag to rewritten file metadata.build(BitstreamWriter(new_apev2, 1)) old_apev2.close() new_apev2.close() else: # no existing metadata, so simply append a fresh tag f = open(self.filename, "ab") metadata.build(BitstreamWriter(f, 1)) f.close()
def delete_metadata(self): """deletes the track's MetaData raises IOError if unable to write the file""" if ((self.get_replay_gain() is not None) or (self.get_cuesheet() is not None)): # non-textual metadata is present and needs preserving self.set_metadata(MetaData()) else: # no non-textual metadata, so wipe out the entire block from os import access, R_OK, W_OK from audiotools.bitstream import BitstreamReader from audiotools import transfer_data if not access(self.filename, R_OK | W_OK): raise IOError(self.filename) with open(self.filename, "rb") as f: f.seek(-32, 2) (preamble, version, tag_size, item_count, read_only, item_encoding, is_header, no_footer, has_header) = BitstreamReader(f, True).parse( ApeTag.HEADER_FORMAT) if (preamble == b'APETAGEX') and (version == 2000): from audiotools import TemporaryFile from os.path import getsize # there's existing metadata to delete # so rewrite file without trailing metadata tag if has_header: old_tag_size = 32 + tag_size else: old_tag_size = tag_size # copy everything but the last "old_tag_size" bytes # from existing file to rewritten file new_apev2 = TemporaryFile(self.filename) old_apev2 = open(self.filename, "rb") limited_transfer_data( old_apev2.read, new_apev2.write, getsize(self.filename) - old_tag_size) old_apev2.close() new_apev2.close()
def update_metadata(self, metadata): """takes this track's current MetaData object as returned by get_metadata() and sets this track's metadata with any fields updated in that object raises IOError if unable to write the file """ import os from audiotools import (TemporaryFile, LimitedFileReader, transfer_data) from audiotools.id3 import (ID3v2Comment, ID3CommentPair) from audiotools.id3v1 import ID3v1Comment from audiotools.bitstream import BitstreamWriter if (metadata is None): return elif (not (isinstance(metadata, ID3v2Comment) or isinstance(metadata, ID3CommentPair) or isinstance(metadata, ID3v1Comment))): from .text import ERR_FOREIGN_METADATA raise ValueError(ERR_FOREIGN_METADATA) elif (not os.access(self.filename, os.W_OK)): raise IOError(self.filename) new_mp3 = TemporaryFile(self.filename) #get the original MP3 data old_mp3 = open(self.filename, "rb") MP3Audio.__find_last_mp3_frame__(old_mp3) data_end = old_mp3.tell() old_mp3.seek(0, 0) MP3Audio.__find_mp3_start__(old_mp3) data_start = old_mp3.tell() old_mp3 = LimitedFileReader(old_mp3, data_end - data_start) #write id3v2 + data + id3v1 to file if (isinstance(metadata, ID3CommentPair)): metadata.id3v2.build(BitstreamWriter(new_mp3, 0)) transfer_data(old_mp3.read, new_mp3.write) metadata.id3v1.build(new_mp3) elif (isinstance(metadata, ID3v2Comment)): metadata.build(BitstreamWriter(new_mp3, 0)) transfer_data(old_mp3.read, new_mp3.write) elif (isinstance(metadata, ID3v1Comment)): transfer_data(old_mp3.read, new_mp3.write) metadata.build(new_mp3) #commit change to disk old_mp3.close() new_mp3.close()
def update_metadata(self, metadata): """takes this track's current MetaData object as returned by get_metadata() and sets this track's metadata with any fields updated in that object raises IOError if unable to write the file """ import os from audiotools import (TemporaryFile, LimitedFileReader, transfer_data) from audiotools.id3 import (ID3v2Comment, ID3CommentPair) from audiotools.id3v1 import ID3v1Comment from audiotools.bitstream import BitstreamWriter if metadata is None: return elif (not (isinstance(metadata, ID3v2Comment) or isinstance(metadata, ID3CommentPair) or isinstance(metadata, ID3v1Comment))): from audiotools.text import ERR_FOREIGN_METADATA raise ValueError(ERR_FOREIGN_METADATA) elif not os.access(self.filename, os.W_OK): raise IOError(self.filename) new_mp3 = TemporaryFile(self.filename) # get the original MP3 data old_mp3 = open(self.filename, "rb") MP3Audio.__find_last_mp3_frame__(old_mp3) data_end = old_mp3.tell() old_mp3.seek(0, 0) MP3Audio.__find_mp3_start__(old_mp3) data_start = old_mp3.tell() old_mp3 = LimitedFileReader(old_mp3, data_end - data_start) # write id3v2 + data + id3v1 to file if isinstance(metadata, ID3CommentPair): metadata.id3v2.build(BitstreamWriter(new_mp3, False)) transfer_data(old_mp3.read, new_mp3.write) metadata.id3v1.build(new_mp3) elif isinstance(metadata, ID3v2Comment): metadata.build(BitstreamWriter(new_mp3, False)) transfer_data(old_mp3.read, new_mp3.write) elif isinstance(metadata, ID3v1Comment): transfer_data(old_mp3.read, new_mp3.write) metadata.build(new_mp3) # commit change to disk old_mp3.close() new_mp3.close()
def delete_metadata(self): """deletes the track's MetaData this removes or unsets tags as necessary in order to remove all data raises IOError if unable to write the file""" import os from audiotools import transfer_data, TemporaryFile if not os.access(self.filename, os.W_OK): raise IOError(self.filename) new_aiff = TemporaryFile(self.filename) self.__class__.aiff_from_chunks( new_aiff, [c for c in self.chunks() if c.id != b"ID3 "]) new_aiff.close()
def update_metadata(self, metadata): """takes this track's current MetaData object as returned by get_metadata() and sets this track's metadata with any fields updated in that object raises IOError if unable to write the file """ from audiotools import transfer_data, TemporaryFile from audiotools.id3 import ID3v22Comment from audiotools.bitstream import BitstreamRecorder from audiotools.text import ERR_FOREIGN_METADATA import os if metadata is None: return elif not isinstance(metadata, ID3v22Comment): raise ValueError(ERR_FOREIGN_METADATA) elif 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) metadata.build(id3_chunk) # generate a temporary AIFF file in which our new ID3v2.2 chunk # replaces the existing ID3v2.2 chunk new_aiff = TemporaryFile(self.filename) self.__class__.aiff_from_chunks( new_aiff, [(chunk if chunk.id != b"ID3 " else AIFF_Chunk(b"ID3 ", id3_chunk.bytes(), id3_chunk.data())) for chunk in self.chunks()]) new_aiff.close()
def update_metadata(self, metadata): """takes this track's current MetaData object as returned by get_metadata() and sets this track's metadata with any fields updated in that object raises IOError if unable to write the file """ from audiotools.bitstream import (parse, BitstreamWriter, BitstreamReader) from audiotools import transfer_data if metadata is None: return elif not isinstance(metadata, ApeTag): from audiotools.text import ERR_FOREIGN_METADATA raise ValueError(ERR_FOREIGN_METADATA) elif len(metadata.keys()) == 0: # wipe out entire block of metadata from os import access, R_OK, W_OK if not access(self.filename, R_OK | W_OK): raise IOError(self.filename) with open(self.filename, "rb") as f: f.seek(-32, 2) (preamble, version, tag_size, item_count, read_only, item_encoding, is_header, no_footer, has_header) = BitstreamReader(f, True).parse( ApeTag.HEADER_FORMAT) if (preamble == b'APETAGEX') and (version == 2000): from audiotools import TemporaryFile, transfer_data from os.path import getsize # there's existing metadata to delete # so rewrite file without trailing metadata tag if has_header: old_tag_size = 32 + tag_size else: old_tag_size = tag_size # copy everything but the last "old_tag_size" bytes # from existing file to rewritten file new_apev2 = TemporaryFile(self.filename) old_apev2 = open(self.filename, "rb") limited_transfer_data( old_apev2.read, new_apev2.write, getsize(self.filename) - old_tag_size) old_apev2.close() new_apev2.close() else: # re-set metadata block at end of file f = open(self.filename, "r+b") f.seek(-32, 2) tag_footer = f.read(32) if len(tag_footer) < 32: # no existing ApeTag can fit, so append fresh tag f.close() with BitstreamWriter(open(self.filename, "ab"), True) as writer: metadata.build(writer) return (preamble, version, tag_size, item_count, read_only, item_encoding, is_header, no_footer, has_header) = parse(ApeTag.HEADER_FORMAT, True, tag_footer) if (preamble == b'APETAGEX') and (version == 2000): if has_header: old_tag_size = 32 + tag_size else: old_tag_size = tag_size if metadata.total_size() >= old_tag_size: # metadata has grown # so append it to existing file f.seek(-old_tag_size, 2) writer = BitstreamWriter(f, True) metadata.build(writer) writer.close() else: f.close() # metadata has shrunk # so rewrite file with smaller metadata from audiotools import TemporaryFile from os.path import getsize # copy everything but the last "old_tag_size" bytes # from existing file to rewritten file new_apev2 = TemporaryFile(self.filename) with open(self.filename, "rb") as old_apev2: limited_transfer_data( old_apev2.read, new_apev2.write, getsize(self.filename) - old_tag_size) # append new tag to rewritten file with BitstreamWriter(new_apev2, True) as writer: metadata.build(writer) # closing writer closes new_apev2 also else: # no existing metadata, so simply append a fresh tag f.close() with BitstreamWriter(open(self.filename, "ab"), True) as writer: metadata.build(writer)
def update_metadata(self, metadata): """takes this track's current MetaData object as returned by get_metadata() and sets this track's metadata with any fields updated in that object raises IOError if unable to write the file """ import os from audiotools.ape import ApeTag from audiotools.id3 import ID3v2Comment from audiotools.id3 import ID3CommentPair from audiotools.id3v1 import ID3v1Comment if (metadata is None): return elif (not os.access(self.filename, os.W_OK)): raise IOError(self.filename) # ensure metadata is APEv2, ID3v2, ID3v1, or ID3CommentPair if (((not isinstance(metadata, ApeTag)) and (not isinstance(metadata, ID3v2Comment)) and (not isinstance(metadata, ID3CommentPair)) and (not isinstance(metadata, ID3v1Comment)))): from audiotools.text import ERR_FOREIGN_METADATA raise ValueError(ERR_FOREIGN_METADATA) current_metadata = self.get_metadata() if (isinstance(metadata, ApeTag) and (current_metadata is None)): # if new metadata is APEv2 and no current metadata, # simply append APEv2 tag from audiotools.bitstream import BitstreamWriter with BitstreamWriter(open(self.filename, "ab"), True) as writer: metadata.build(writer) elif (isinstance(metadata, ApeTag) and isinstance(current_metadata, ApeTag) and (metadata.total_size() > current_metadata.total_size())): # if new metadata is APEv2, current metadata is APEv2 # and new metadata is larger, # overwrite old tag with new tag from audiotools.bitstream import BitstreamWriter with open(self.filename, "r+b") as f: f.seek(-current_metadata.total_size(), 2) metadata.build(BitstreamWriter(f, True)) else: from audiotools.bitstream import BitstreamWriter from audiotools import (transfer_data, LimitedFileReader, TemporaryFile) from audiotools.id3 import skip_id3v2_comment # otherwise, rebuild TTA with APEv2/ID3 tags in place old_tta = open(self.filename, "rb") skip_id3v2_comment(old_tta) old_tta = LimitedFileReader(old_tta, self.data_size()) new_tta = TemporaryFile(self.filename) if (isinstance(metadata, ApeTag)): transfer_data(old_tta.read, new_tta.write) metadata.build(BitstreamWriter(new_tta, True)) elif (isinstance(metadata, ID3CommentPair)): metadata.id3v2.build(BitstreamWriter(new_tta, False)) transfer_data(old_tta.read, new_tta.write) metadata.id3v1.build(new_tta) elif (isinstance(metadata, ID3v2Comment)): metadata.build(BitstreamWriter(new_tta, False)) transfer_data(old_tta.read, new_tta.write) else: # ID3v1Comment transfer_data(old_tta.read, new_tta.write) metadata.build(new_tta) old_tta.close() new_tta.close()
def update_metadata(self, metadata): """takes this track's current MetaData object as returned by get_metadata() and sets this track's metadata with any fields updated in that object raises IOError if unable to write the file """ if (metadata is None): return elif (not isinstance(metadata, ApeTag)): from audiotools.text import ERR_FOREIGN_METADATA raise ValueError(ERR_FOREIGN_METADATA) from audiotools.bitstream import BitstreamReader, BitstreamWriter from audiotools import transfer_data f = open(self.filename, "r+b") f.seek(-32, 2) (preamble, version, tag_size, item_count, read_only, item_encoding, is_header, no_footer, has_header) = BitstreamReader(f, 1).parse(ApeTag.HEADER_FORMAT) if ((preamble == 'APETAGEX') and (version == 2000)): if (has_header): old_tag_size = 32 + tag_size else: old_tag_size = tag_size if (metadata.total_size() >= old_tag_size): # metadata has grown # so append it to existing file f.seek(-old_tag_size, 2) metadata.build(BitstreamWriter(f, 1)) else: # metadata has shrunk # so rewrite file with smaller metadata from audiotools import TemporaryFile from os.path import getsize # copy everything but the last "old_tag_size" bytes # from existing file to rewritten file new_apev2 = TemporaryFile(self.filename) old_apev2 = open(self.filename, "rb") limited_transfer_data( old_apev2.read, new_apev2.write, getsize(self.filename) - old_tag_size) # append new tag to rewritten file metadata.build(BitstreamWriter(new_apev2, 1)) old_apev2.close() new_apev2.close() else: # no existing metadata, so simply append a fresh tag f = open(self.filename, "ab") metadata.build(BitstreamWriter(f, 1)) f.close()