import struct
import json
import cv2
import argparse
import os
parser = argparse.ArgumentParser()
parser.add_argument('--source_video_mp4', type=str, default='/home/momenta/Downloads/dvr/202107081536.mp4')
parser.add_argument('--source_video_h264', type=str, default='/home/momenta/Downloads/dvr/tmp.h264')
parser.add_argument('--output_dir', type=str, default='output')
opt = parser.parse_args()
videoCapture = cv2.VideoCapture()
videoCapture.open(opt.source_video_mp4)
class Frame:
def __init__(self, frame_type, start_index):
self.frame_type = frame_type
self.data = []
class VideoInfo:
def __init__(self):
self.timestamps = []
def parse_frame(frame, info):
if frame.data[0] == 0x06 and frame.data[1] == 0x5 and frame.data[2] == 0x20:
# for b in frame.data:
# logger.info(hex(b), end='')
# logger.info()
uuid, timestamp = struct.unpack('16sQ', bytes(frame.data[3:27]))
print(frame.data[3:27])
info.timestamps.append(timestamp)
if __name__ == '__main__':
info = VideoInfo()
with open(opt.source_video_h264, 'rb') as reader:
key = 0
count = 0
key_found = False
frame = None
data = reader.read()
print(len(data))
for i in range(0, len(data) - 1):
b = data[i]
if key_found:
key_found = False
frame_type = b & 0x1F
frame = Frame(frame_type, i)
frame.data.append(b)
else:
if frame:
frame.data.append(b)
key = (key << 8) | b
key &= 0xFFFFFFFF
if key & 0xFFFFFF == 0x000001:
count += 1
key_found = True
if frame:
if key == 0x000000001:
frame.data = frame.data[:-4]
else:
frame.data = frame.data[:-3]
parse_frame(frame, info)
print(info.timestamps)
print(len(info.timestamps))
ind = [i for i in range(len(info.timestamps))]
to_save = dict(zip(ind, info.timestamps))
fps = videoCapture.get(cv2.CAP_PROP_FPS)
frames = videoCapture.get(cv2.CAP_PROP_FRAME_COUNT)
print("fps=",fps,"frames=",frames)
print(int(frames))
f1 = open("{}/timestamp.txt".format(opt.output_dir),'w')
for i in range(int(frames)):
ret,frame = videoCapture.read()
if ret is True:
timestamps = videoCapture.get(cv2.CAP_PROP_POS_MSEC)
cv2.imwrite("{}/{}.jpg".format(opt.output_dir, to_save[i]), frame)
f1.writelines("{}".format(timestamps))
f1.close()
【python】video2image
最新推荐文章于 2024-03-20 09:38:34 发布