import struct
import json
import time
import cv2
import argparse
import os
#python3 newvideo2image.py --video_file 1130
parser = argparse.ArgumentParser()
parser.add_argument('--video_file', type=str,
opt = parser.parse_args()
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)
def mkdir_file(name):
#delete space
name = name.strip()
#delete '\\'
name = name.rstrip("\\")
# whether dir is exist
dir = name.split(".", 1)[0]
file_path = 'output_jpg/' + dir
isExist = os.path.exists(file_path)
if not isExist:
os.makedirs(file_path)
print ('mkdir ' + file_path + ' success')
return file_path
else:
print (file_path + ' is exist!')
return file_path
def file_name(file_dir):
mp4_file = []
h264_file = []
for root, dirs, files in os.walk(file_dir):
for file in files:
#print(file)
file_type = file.split(".", 1)[1]
#print(file_type)
if file_type == 'mp4':
print(file)
mp4_file.append(os.path.join(root, file))
else:
h264_file.append(os.path.join(root, file))
return mp4_file, h264_file
if __name__ == '__main__':
info = VideoInfo()
mp4_file_path, h264_file_path = file_name(opt.video_file)
for file_mp4 in mp4_file_path:
videoCapture = cv2.VideoCapture()
videoCapture.open(file_mp4)
file_path = file_mp4.rsplit("/", 1)[0]
file_mp4_name = file_mp4.rsplit("/", 1)[1]
file_264 = file_path + '/tmp'+ file_mp4_name +'.h264'
print (file_264)
dir = mkdir_file(file_mp4_name)
with open(file_264, 'rb') as reader:
key = 0
count = 0
key_found = False
frame = None
data = reader.read()
print("data length :", 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(dir),'w')
for i in range(int(frames)):
ret, frame = videoCapture.read()
if ret is True:
#timestamps = videoCapture.get(cv2.CAP_PROP_POS_MSEC)
t = time.time()
curr_t = int(round(t * 1000000))
print(curr_t)
cv2.imwrite("{}/{}.jpg".format(dir, curr_t), frame)
f1.writelines("{}\n".format(curr_t))
videoCapture.release()
f1.close()
mp4视频提取时间戳
最新推荐文章于 2025-04-02 10:23:21 发布