Seppukki
Seppukki

Reputation: 573

Reading a .mp3 file as binary (NOT bytes)

I'm trying to get a binary representation of a music (.mp) file in python. I'm currently only capable to get it represented in bytes. When I try to convert those bytes to binary, I keep getting errors. This is my current program:

folder=os.listdir(os.getcwd())
for files in folder:
    if files.endswith(".mp3"):
        file = open(files, 'rb')
        stream = str(file.read()).split("\\")
        print(stream)
        for bit in range(1,len(stream)):
            """
            print(stream[bit])
            newbit = f"0{stream[bit]}"
            c = BitArray(hex=newbit)
            #print(c.bin)
            """
            print(stream[bit][1:])
            print(bin(int(stream[bit][1:], base=16)))

I keep getting the same error:

line 39, in <module>
print(bin(int(stream[bit][1:], base=16)))
ValueError: invalid literal for int() with base 16: '00Info'

When I go and check the bytes from the statement print(stream) the byte x00Info doesn't show up. I've never worked with bytes before so I have no idea what's going on.

Upvotes: 0

Views: 1714

Answers (1)

Macattack
Macattack

Reputation: 1990

import sys
with open(sys.argv[1], 'rb') as f:
    for c in f.read():
        print(bin(c)[2:])

You don't specify what you're expecting as output, but, this will print the binary.

Upvotes: 1

Related Questions