def l_tag_value(cls, file, tag): subtype = {1: Con.Byte("data"), 2: Con.CString("data"), 3: Con.ULInt16("data"), 4: Con.ULInt32("data"), 5: Con.Struct("data", Con.ULInt32("high"), Con.ULInt32("low"))}[tag.type] data = Con.StrictRepeater(tag.count, subtype) if ((tag.type != 2) and (data.sizeof() <= 4)): return tag.offset else: file.seek(tag.offset, 0) return data.parse_stream(file)
Con.UBInt32("geometry_matrix_v"), Con.UBInt32("geometry_matrix_x"), Con.UBInt32("geometry_matrix_y"), Con.UBInt32("geometry_matrix_w")), Con.UBInt64("quicktime_preview"), Con.UBInt32("quicktime_still_poster"), Con.UBInt64("quicktime_selection_time"), Con.UBInt32("quicktime_current_time"), Con.UBInt32("next_track_id")) ATOM_IODS = Con.Struct( "iods", Con.Byte("version"), Con.String("flags", 3), Con.Byte("type_tag"), Con.Switch( "descriptor", lambda ctx: ctx.type_tag, { 0x10: Con.Struct( None, Con.StrictRepeater(3, Con.Byte("extended_descriptor_type")), Con.Byte("descriptor_type_length"), Con.UBInt16("OD_ID"), Con.Byte("OD_profile"), Con.Byte("scene_profile"), Con.Byte("audio_profile"), Con.Byte("video_profile"), Con.Byte("graphics_profile")), 0x0E: Con.Struct( None, Con.StrictRepeater(3, Con.Byte("extended_descriptor_type")), Con.Byte("descriptor_type_length"), Con.String("track_id", 4)) })) ATOM_TKHD = Con.Struct( "tkhd", Con.Byte("version"), Con.BitStruct("flags", Con.Padding(20), Con.Flag("TrackInPoster"), Con.Flag("TrackInPreview"), Con.Flag("TrackInMovie"),
class MP3Audio(AudioFile): """An MP3 audio file.""" SUFFIX = "mp3" NAME = SUFFIX DEFAULT_COMPRESSION = "2" #0 is better quality/lower compression #9 is worse quality/higher compression COMPRESSION_MODES = ("0", "1", "2", "3", "4", "5", "6", "7", "8", "9") BINARIES = ("lame",) REPLAYGAIN_BINARIES = ("mp3gain", ) #MPEG1, Layer 1 #MPEG1, Layer 2, #MPEG1, Layer 3, #MPEG2, Layer 1, #MPEG2, Layer 2, #MPEG2, Layer 3 MP3_BITRATE = ((None, None, None, None, None, None), (32, 32, 32, 32, 8, 8), (64, 48, 40, 48, 16, 16), (96, 56, 48, 56, 24, 24), (128, 64, 56, 64, 32, 32), (160, 80, 64, 80, 40, 40), (192, 96, 80, 96, 48, 48), (224, 112, 96, 112, 56, 56), (256, 128, 112, 128, 64, 64), (288, 160, 128, 144, 80, 80), (320, 192, 160, 160, 96, 96), (352, 224, 192, 176, 112, 112), (384, 256, 224, 192, 128, 128), (416, 320, 256, 224, 144, 144), (448, 384, 320, 256, 160, 160)) #MPEG1, MPEG2, MPEG2.5 MP3_SAMPLERATE = ((44100, 22050, 11025), (48000, 24000, 12000), (32000, 16000, 8000)) MP3_FRAME_HEADER = Con.BitStruct("mp3_header", Con.Const(Con.Bits("sync", 11), 0x7FF), Con.Bits("mpeg_version", 2), Con.Bits("layer", 2), Con.Flag("protection", 1), Con.Bits("bitrate", 4), Con.Bits("sampling_rate", 2), Con.Bits("padding", 1), Con.Bits("private", 1), Con.Bits("channel", 2), Con.Bits("mode_extension", 2), Con.Flag("copyright", 1), Con.Flag("original", 1), Con.Bits("emphasis", 2)) XING_HEADER = Con.Struct("xing_header", Con.Bytes("header_id", 4), Con.Bytes("flags", 4), Con.UBInt32("num_frames"), Con.UBInt32("bytes"), Con.StrictRepeater(100, Con.Byte("toc_entries")), Con.UBInt32("quality")) def __init__(self, filename): """filename is a plain string.""" AudioFile.__init__(self, filename) try: mp3file = file(filename, "rb") except IOError, msg: raise InvalidMP3(str(msg)) try: try: MP3Audio.__find_next_mp3_frame__(mp3file) except ValueError: raise InvalidMP3(_(u"MP3 frame not found")) fr = MP3Audio.MP3_FRAME_HEADER.parse(mp3file.read(4)) self.__samplerate__ = MP3Audio.__get_mp3_frame_sample_rate__(fr) self.__channels__ = MP3Audio.__get_mp3_frame_channels__(fr) self.__framelength__ = self.__length__() finally: mp3file.close()