I am trying to get the i-frames of a video using opencv. I have read that the header of a keyframe should look like this: b'\x00\x00\x01\xb0', but the problem I am facing is that I can not seem to find the header of the frame if I just do something like this:
while True:
ret, frame = vid.read()
if ret == False:
break
print(bytes(frame))
I did not find anything that can locate keyframes only ffprobe and I am trying to find something else to use, if there is a library of something please let me know!
Got back something that starts like this:
b'YK<YK<YK<YK<YK<YK<YK<YK<YK<YK<YK<YK<YK<YK<YK<YK<YL:YL:YM9YM9YM9YM9YM9YM9YM9YM9YM9YM9YM9YM9ZN:ZN:ZN:[O;[O;[O;[O;[O;[N<[N<[M>[M>[L?[L?[L?[L?[L?[L?YN>YN>VN;VN;UM:UM:TJ<TJ<TJ<TJ<SH?SH?SH?QF=QF=QF=PE:OD9ND6MC5LB4I?1E:1C8/B42B42@14?03?03?03@14?03<02<026,.4*,3)+0&(-#%,"$)\x1f!\'\x1d\x1f%\x1b\x1d#\x19\x1b"\x18\x1a#\x19\x1b!\x19\x1b$\x1c\x1e% \x1f&! \x1e\x1a\x1d\x1b\x17\x1a\x18\x14\x16\x14\x10\x0f\r\t\x0b\t\x05\x07\x05\x01\x07\x05\x01\x06\x04\x00\x05\x03\x00\x04\x02\x00\x03\x01\x00\x03\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0 ......
I Like pyav for more flexible things like U want.
you got raw frame data because its already Decoded, so there are no nal unit Headers anymore.
import av
container = av.open(path_to_video)
for frame in container.decode(video=0):
if frame.key_frame:
frame_bytes = bytes(frame)