def get_metadata(self): """returns a MetaData object, or None raises IOError if unable to read the file""" from cStringIO import StringIO from audiotools.bitstream import BitstreamReader from audiotools.ogg import PacketReader, PageReader from audiotools.vorbiscomment import VorbisComment reader = PacketReader(PageReader(open(self.filename, "rb"))) identification = reader.read_packet() comment = BitstreamReader(StringIO(reader.read_packet()), True) (packet_type, packet_header) = comment.parse("8u 6b") if ((packet_type == 3) and (packet_header == 'vorbis')): vendor_string = \ comment.read_bytes(comment.read(32)).decode('utf-8') comment_strings = [ comment.read_bytes(comment.read(32)).decode('utf-8') for i in xrange(comment.read(32))] if (comment.read(1) == 1): # framing bit return VorbisComment(comment_strings, vendor_string) else: return None else: return None
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 .vorbiscomment import VorbisComment if (metadata is not None): metadata = VorbisComment.converted(metadata) old_metadata = self.get_metadata() metadata.vendor_string = old_metadata.vendor_string #remove REPLAYGAIN_* tags from new metadata (if any) for key in [u"REPLAYGAIN_TRACK_GAIN", u"REPLAYGAIN_TRACK_PEAK", u"REPLAYGAIN_ALBUM_GAIN", u"REPLAYGAIN_ALBUM_PEAK", u"REPLAYGAIN_REFERENCE_LOUDNESS"]: try: metadata[key] = old_metadata[key] except KeyError: metadata[key] = [] self.update_metadata(metadata)
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""" if metadata is None: return self.delete_metadata() metadata = VorbisComment.converted(metadata) old_metadata = self.get_metadata() metadata.vendor_string = old_metadata.vendor_string # port REPLAYGAIN and ENCODER from old metadata to new metadata for key in [u"REPLAYGAIN_TRACK_GAIN", u"REPLAYGAIN_TRACK_PEAK", u"REPLAYGAIN_ALBUM_GAIN", u"REPLAYGAIN_ALBUM_PEAK", u"REPLAYGAIN_REFERENCE_LOUDNESS", u"ENCODER"]: try: metadata[key] = old_metadata[key] except KeyError: metadata[key] = [] self.update_metadata(metadata)
def get_metadata(self): """returns a MetaData object, or None raises IOError if unable to read the file""" from io import BytesIO from audiotools.bitstream import BitstreamReader from audiotools.ogg import PacketReader, PageReader reader = PacketReader(PageReader(open(self.filename, "rb"))) identification = reader.read_packet() comment = BitstreamReader(BytesIO(reader.read_packet()), True) if (comment.read_bytes(8) == "OpusTags"): vendor_string = \ comment.read_bytes(comment.read(32)).decode('utf-8') comment_strings = [ comment.read_bytes(comment.read(32)).decode('utf-8') for i in range(comment.read(32)) ] return VorbisComment(comment_strings, vendor_string) else: return None
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""" if (metadata is None): return self.delete_metadata() metadata = VorbisComment.converted(metadata) old_metadata = self.get_metadata() metadata.vendor_string = old_metadata.vendor_string # port REPLAYGAIN and ENCODER from old metadata to new metadata for key in [ u"REPLAYGAIN_TRACK_GAIN", u"REPLAYGAIN_TRACK_PEAK", u"REPLAYGAIN_ALBUM_GAIN", u"REPLAYGAIN_ALBUM_PEAK", u"REPLAYGAIN_REFERENCE_LOUDNESS", u"ENCODER" ]: try: metadata[key] = old_metadata[key] except KeyError: metadata[key] = [] self.update_metadata(metadata)
def set_replay_gain(self, replaygain): """given a ReplayGain object, sets the track's gain to those values may raise IOError if unable to modify the file""" if replaygain is None: return self.delete_replay_gain() vorbis_comment = self.get_metadata() if vorbis_comment is None: from audiotools.vorbiscomment import VorbisComment from audiotools import VERSION vorbis_comment = VorbisComment([], u"Python Audio Tools %s" % (VERSION)) vorbis_comment[u"REPLAYGAIN_TRACK_GAIN"] = [ u"%1.2f dB" % (replaygain.track_gain) ] vorbis_comment[u"REPLAYGAIN_TRACK_PEAK"] = [ u"%1.8f" % (replaygain.track_peak) ] vorbis_comment[u"REPLAYGAIN_ALBUM_GAIN"] = [ u"%1.2f dB" % (replaygain.album_gain) ] vorbis_comment[u"REPLAYGAIN_ALBUM_PEAK"] = [ u"%1.8f" % (replaygain.album_peak) ] vorbis_comment[u"REPLAYGAIN_REFERENCE_LOUDNESS"] = [u"89.0 dB"] self.update_metadata(vorbis_comment)