mp3 - How to read specific bytes from a binary file in Python? -
i learn handle read , write binary data. know can open binary file with
f = open(myfile, mode='rb') fb = f.read() f.close() return fb how can access , read range $a7-$ac in mp3 file structure: lame mp3 tags
you should take @ python's struct library extracting binary data.
import struct mp3_filename = r"my_mp3_file.mp3" open(mp3_filename, 'rb') f_mp3: mp3 = f_mp3.read() entry = mp3[0xa7:0xac+1] print struct.unpack("{}b".format(len(entry)), entry) this give list of integers such as:
(49, 0, 57, 0, 57, 0) you pass format string tell python how intepret each of bytes. in example, converted bytes integers. each format specifier can have repeat count, example, format string "6b". if wanted decode words, change format specifier, there full table of options you: struct format characters
to convert these zeros, need close file , reopen writing. build new output follows:
import struct mp3_filename = r"my_mp3_file.mp3" zeros = "\0\0\0\0\0\0" open(mp3_filename, 'rb') f_mp3: mp3 = f_mp3.read() entry = mp3[0xa7:0xac+1] print struct.unpack("{}b".format(len(entry)), entry) if entry != zeros: print "non zero" open(mp3_filename, 'wb') f_mp3: f_mp3.write(mp3[:0xa7] + zeros + mp3[0xad:]) fyi: there ready made python libraries able extract tag information mp3 files. take @ id3reader package.
Comments
Post a Comment