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)
Related
python
I have a text file (.txt) and I need to print to the last byte of the txt.
how I do that ?
When I do not know the size of the document.
The documenation provides an API, that can be used to solve that problem. You will need to do the following things in order:
Open the file in text mode like in the example here.
Change the file pointer to the last byte. This can be achieved using the seek memeber function of the file object. Use the SEEK_END token with an offset of -1 to get one byte before the end of the file
Read one byte with the read function.
Print that byte.
If you did not use a context manager (with keyword) while opening the file, you should use close to close the file before exiting the program
The trick here is to use the seek method, that can be used to specify an offset relative to the end of the file.
The following should work:
with open("text.txt") as file:
text = outfile.read()
byte_array = bytearray(text, "utf8")
print(byte_array[-1:])
If you need the binary representation
with open("text.txt") as file:
text = outfile.read()
byte_array = bytearray(text, "utf8")
binary_byte_list = []
for byte in byte_array:
binary_representation = bin(byte)
binary_byte_list.append(binary_representation)
print(binary_byte_list[-1:])
You could do it like this using seek which obviates the need to read the entire file into memory:
import os
with open('foo.txt', 'rb') as foo:
foo.seek(-1, os.SEEK_END)
b = foo.read()
print(b)
In this case the last character is newline and therefore:
Output:
b'\n'
Note:
File opened in binary mode
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.
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
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.
I wrote a program that uses bitarray 0.8.0 to write bits to a binary file. I would like to add a header to this binary file to describe what's inside the file.
My problem is that I think the method "fromfile" of bitarray necessarily starts reading the file from the beginning. I could make a workaround so that the reading program gets the header and then rewrite a temporary file containing only the binary portion (bitarray tofile), but it doesn't sound too efficient of an idea.
Is there any way to do this properly?
My file could look something like the following where clear text is the header and binary data is the bitarray information:
...{(0, 0): '0'}{(0, 0): '0'}{(0, 0): '0'}���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������...
Edit:
I tried the following after reading the response:
bits = ""
b = bitarray()
with open(Filename, 'rb') as file:
#Get header
byte = file.read(1)
while byte != "":
# read header
byte = file.read(1)
b.fromfile(file)
print b.to01()
print "len(b.to01())", len(b.to01())
The length is 0 and the print of "to01()" is empty.
However, the print of the header is fine.
My problem is that I think the method "fromfile" of bitarray necessarily starts reading the file from the beginning.
This is likely false; it, like most other file read routines, probably starts at the current position within the file, and stops at EOF.
EDIT:
From the documentation:
fromfile(f, [n])
Read n bytes from the file object f and append them to the bitarray interpreted as machine values. When n is omitted, as many bytes are read until EOF is reached.