Module: FormatParser::MP3Parser::ID3V2
Instance Method Summary collapse
- #attempt_id3_v2_extraction(io) ⇒ Object
-
#decode_syncsafe_int(bytes) ⇒ Object
ID3v2 uses “unsynchronized integers”, which are unsigned integers smeared over multiple bytes in such a manner that the first bit is always 0 (unset).
- #parse_id3_v2_frame(io) ⇒ Object
- #parse_id3_v2_header(byte_str) ⇒ Object
Instance Method Details
#attempt_id3_v2_extraction(io) ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/parsers/mp3_parser/id3_v2.rb', line 2 def attempt_id3_v2_extraction(io) io.seek(0) # Only support header ID3v2 header_bytes = io.read(10) return unless header_bytes header = parse_id3_v2_header(header_bytes) return unless header[:tag] == 'ID3' return unless header[:size] > 0 header_tag_payload = io.read(header[:size]) header_tag_payload = StringIO.new(header_tag_payload) return unless header_tag_payload.size == header[:size] frames = [] loop do break if header_tag_payload.eof? frame = parse_id3_v2_frame(header_tag_payload) # Some files include padding, which is there so that when you edit ID3v2 # you do not have to overwrite the entire file - you can use this padding to # add some more tags or to grow the existing ones. In practice if we hit # something with a type of "0x00000000" we have entered the padding zone and # there is no point in parsing further if frame[:id] == "\x00\x00\x00\x00".b break else frames << frame end end frames end |
#decode_syncsafe_int(bytes) ⇒ Object
ID3v2 uses “unsynchronized integers”, which are unsigned integers smeared over multiple bytes in such a manner that the first bit is always 0 (unset). This is done so that ID3v2 incompatible decoders will not by accident see the 0xFF0xFF0xFF0xFF sequence anywhere that can be mistaken for the MPEG frame synchronisation header. Effectively it is a 7 bit big-endian unsigned integer encoding.
8 bit 255 (0xFF) encoded in this mannner takes 16 bits instead, and looks like this: ‘0b00000001 01111111`. Note how it avoids having the first bit of the second byte be 1. This method decodes an unsigned integer packed in this fashion
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/parsers/mp3_parser/id3_v2.rb', line 71 def decode_syncsafe_int(bytes) size = 0 j = 0 i = bytes.bytesize - 1 while i >= 0 size += 128**i * (bytes.getbyte(j) & 0x7f) j += 1 i -= 1 end size end |
#parse_id3_v2_frame(io) ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'lib/parsers/mp3_parser/id3_v2.rb', line 51 def parse_id3_v2_frame(io) id, syncsafe_size, flags = io.read(10).unpack('a4a4a2') size = decode_syncsafe_int(syncsafe_size) content = io.read(size) # It might so happen in sutations of terrible invalidity that we end up # with less data than advertised by the syncsafe size. We will just truck on. {id: id, size: size, flags: flags, content: content} end |
#parse_id3_v2_header(byte_str) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/parsers/mp3_parser/id3_v2.rb', line 34 def parse_id3_v2_header(byte_str) packspec = [ :tag, :a3, :version, :a2, :flags, :C1, :size, :a4, ] keys, values = packspec.partition.with_index { |_, i| i.even? } unpacked_values = byte_str.unpack(values.join) header_data = Hash[keys.zip(unpacked_values)] header_data[:version] = header_data[:version].unpack('C2') header_data[:size] = decode_syncsafe_int(header_data[:size]) header_data end |