Reputation: 43
I would learn to handle read and write binary data. I know that I can open a binary file with
f = open(myfile, mode='rb')
fb = f.read()
f.close()
return fb
How can I access and read the range $A7-$AC in a mp3 file with this structure: Lame mp3 Tags
Upvotes: 1
Views: 3040
Reputation: 46759
You should take a look at Python's struct
library for help with extracting binary data.
import struct
mp3_filename = r"my_mp3_file.mp3"
with open(mp3_filename, 'rb') as f_mp3:
mp3 = f_mp3.read()
entry = mp3[0xA7:0xAC+1]
print struct.unpack("{}b".format(len(entry)), entry)
This would give you a list of integers such as:
(49, 0, 57, 0, 57, 0)
You pass a format string to tell Python how to intepret each of the bytes. In this example, they are all simply converted from bytes into integers. Each format specifier can have a repeat count, so for your example, the format string would be "6b"
. If you wanted to decode this as words, you would simply change the format specifier, there is a full table of options to help you: Struct format characters
To convert these to zeros, you would need to close the file and reopen it for writing. Build a new output as follows:
import struct
mp3_filename = r"my_mp3_file.mp3"
zeros = "\0\0\0\0\0\0"
with open(mp3_filename, 'rb') as 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"
with open(mp3_filename, 'wb') as f_mp3:
f_mp3.write(mp3[:0xA7] + zeros + mp3[0xAD:])
FYI: There are ready made Python libraries that are able to extract tag information from MP3 files. Take a look at something like the id3reader
package.
Upvotes: 1