简体   繁体   中英

read a .h264 file

I'll be happy for your help with some problem that I have.

Goal: To read a .h264 file (I extracted the raw bitstream to a file using ffmpeg) using python, and save it in some data structure (probably a list, I'll be happy for suggestions).

I want to read the data as hexa, for example I'll show how the data looks like: 在此处输入图片说明

What I want is to feed each byte(2 hexa digits), into a list, or some other data structure. But any step forward will help me.

My Attempts: First I tried to read the way I know:

with open(path, 'r') as fp:
     data = fp.read()

Didn't work, got just ".

After a lot of changes, I tried something else, I saw online:

    with open(path, 'r') as fp:
    hex_list = ["{:02}".format(ord(c)) for c in fp.read()]

Still got an empty list.

I'll be happy for you help. Thanks a lot.

EDIT : Thanks to the comment below, I tried to open using 'rb', but still with no luck.

If you have an h264 mp4 file, you can open it and get a hexadecimal string representation like this using binascii.hexlify() :

import binascii
with open('test.mp4', 'rb') as fin:
    hexa = binascii.hexlify(fin.read())
    print(hexa[0:1000])

hexa will be a python bytes object, and you can easily get back the binary representation by doing binascii.unhexlify(hexa) . This will be much more efficient than storing the hex representation as strings in a list() , both in terms of space and time. You can access the bytes array with indices/slices, so whatever you were intending to do with the list will probably work fine with this (it will just be much faster and use a lot less memory).

One thing to keep in mind though is to get the the first hexadecimal digit from a bytes object, you don't do hexa[0] , but rather hexa[0:1] . To get the first pair of hexadecimal digits (byte), you do: hexa[0:2] . The second byte is hexa[2:4] etc. As explained in the docs for hex() :

Since bytes objects are sequences of integers (akin to a tuple), for a bytes object b, b[0] will be an integer, while b[0:1] will be a bytes object of length 1. (This contrasts with text strings, where both indexing and slicing will produce a string of length 1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM