mp4视频提取时间戳

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()
    

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值