How to read specific bytes from a binary MP3 file in Python? - python

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

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.

Related

how to change byte type in pythone

i have a small problem and it caused me a lot of trubble. basicly i want to convert an immage to bytes than store string wersion of those bytes in an txt file and than read file contents and transform it into bytes and than into image. i've goten first part of this kinda ready (it works but it's made quickly and badly) but the conversion from string to byte gives me problem.
when i read image bytes it's something like this: b'GIF89aP\x00P\x00\xe3'
but when i read it from txt by 'rb' or just transform str to byte it gives me this: b'GIF89aP\\x00P\\x00\\xe3'
and with this i can't write it to an immage.
so i've tried to read and learn anything about this but i couldn't find anything that would help.
the code is here and i know it's really messy but i just need it to work
file = open('p.gif', 'rb')
image = file.read()
str_b = str(image)
leng = len(str_b)
print(leng)
str_b = str_b[:0] + str_b[0+2:]
leng =- 1
str_b = str_b[:leng]
print(image)
#a = open('bytearray', 'w+')
#a.write(str_b)
#a.close
a = open('bytearray', 'r')
a = a.read()
temp = a.encode('utf-8')
print(temp)
#b = open('check', 'w+')
#b.write(str(string))
#print(string)
image_result = open('decoded.jpg', 'wb') # create a writable image and write the decoding result
image_result.write(temp)
basicly my goal right now is to get bytes that look like this: b'GIF89aP\x00P\x00\xe3'
Please do not use eval like suggested above, eval has serious security vulnerabilities and will execute any python code you pass within it. You could accidentally read a text file that has code to reformat the disk and it will just execute, this is just an example but you get my point its bad practice and just results in more problems see https://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html if you want some examples on why eval is bad
anyways lets try to fix your code
instead of converting your byte array to string by wrapping it in the str() method I would suggest you use .decode() and .encode()
Fixed Code:
with open('p.gif', 'rb') as file:
image = file.read() # read file bytes
str_image = image.decode("utf-8") #using decode we changed the bytes to a string
with open('image.txt', 'w') as file:
file.write(str_image) # write image as string to a text file
with open('image.txt', 'r') as file
str_from_file = file.read() # read the text file and store the string
file_bytes = str_from_file.encode("utf-8") # encode the image str back to bytes
print(type(str_from_file)) #type is str
print(type(file_bytes)) # types is bytes
I hope this fixes your issue and also doesn't include vulnerabilties in what your building

Python: Convert data file format to string

I have a file that has the following output when file command is run:
#file test.bin
#test.bin : data
#file -i test.bin
#test.bin: application/octet-stream; charset=binary
I want to read the contents of this file and forward to a python library that accepts this read-data as a string.
file = open("test.bin", "rb")
readBytes = file.read() # python type : <class 'bytes'>
output = test.process(readBytes) # process expects a string
I have tried str(readBytes), however that did not work. I see that there are also unprintable strings in the file test.bin, as the output of strings test.bin produces far lesser output than the actual bytes present in the file.
Is there a way to convert the bytes read into strings? Or am I trying to achieve something that makes no sense at all?
Try to use Bitstring. It's good package for reading bits.
# import module
from bitstring import ConstBitStream
# read file
x = ConstBitStream(filename='file.bin')
# read 5 bits
output = x.read(5)
# convert to unsigned int
int_val = output.uint
Do you mean by?
output = test.process(readBytes.decode('latin1'))

Easy way to view and save the binary of a file?

What is the easiest way to get the underlying binary code(0s and 1s) for a given file? The context for this question is that I want a python function which takes a file name, looks it up and gathers the binary code for that file before either storing it somewhere or returning it. After this I want to do some manipulations on the binary file.
The underlying code for a file is available form the .read() method of the file object. Use the b mode modifier when you open the file:
with open("input_file.bin", "rb") as input_file:
bits = input_file.read()
If you want to easily manipulate the bits after reading them in, you might want to convert them to a bitarray:
from bitarray import bitarray
with open("input_file.bin", "rb") as input_file:
chars = input_file.read()
bits = bitarray()
bits.frombytes(chars)
print bits.count(1), bits.count(0)
References:
https://docs.python.org/2/library/functions.html#open
https://pypi.python.org/pypi/bitarray/0.8.1

How to open and read a binary file in Python?

I have a binary file (link) that I would like to open and read contents of with Python. How are such binary files opened and read with Python? Any specific modules to use for such an operation.
The 'b' flag will get python to treat the file as a binary, so no modules are needed. Also you haven't provided a purpose for having python read a binary file with a question like that.
f = open('binaryfile', 'rb')
print(f.read())
Here is an Example:
with open('somefile.bin', 'rb') as f: #the second parameter "rb" is used only when reading binary files. Term "rb" stands for "read binary".
data = f.read() #we are assigning a variable which will read whatever in the file and it will be stored in the variable called data.
print(data)
Reading a file in python is trivial (as mentioned above); however, it turns out that if you want to read a binary file and decode it correctly you need to know how it was encoded in the first place.
I found a helpful example that provided some insight at https://www.devdungeon.com/content/working-binary-data-python,
# Binary to Text
binary_data = b'I am text.'
text = binary_data.decode('utf-8') #Trans form back into human-readable ASCII
print(text)
binary_data = bytes([65, 66, 67]) # ASCII values for A, B, C
text = binary_data.decode('utf-8')
print(text)
but I was still unable to decode some files that my work created because they used an unknown encoding method.
Once you know how it is encoded you can read the file bit by bit and perform the decoding with a function of three.

Save a bitstream to a file using python

I need to output an h.265 (or hevc, is the same) bit-stream onto an str file in python.
I have a bitstream file and i select some data from this file to save it to a new one. I use bitstring module to process the bitstream file.
Edit: My question is how to create a new bitstream file and insert data into.
Check out the part about Joining BitArrays (base class of BitStream) in this part of the bitstring documentation. How to join the substreams depends on how you have them in the first place.
For writing the bitstream to a file, use the method 'toFile' of the Bits class, which is a base class of BitStream.
f = open('fileToWriteTo', 'wb')
bitstreamObject.tofile(f)
If you want to write multiple substreams one after another, you can open the file in append mode the next times you write something.
f = open('fileToWriteTo', 'ab')
nextSubstream.tofile(f)
Take a look at struct
A quick example:
import struct
characters = "Hello World"
with open(filepath, 'wb') as f:
for char in characters:
# #B means to pack native (LSB or MSB) to size unsigned char (1 byte)
packed = struct.pack('#B', char)
f.write(packed)

Categories

Resources