python实现视频分帧

简介import cv2 def video2frame(videos_path, frames_save_path, time_interval): ''' :param videos_path: 视频的存放路径 :param frames_save_path: 视频切分成帧之后图片的保存路径 :param time_interval: 保存间隔 :return: ''' cap = cv2.VideoCapture(videos_


import cv2

def video2frame(videos_path, frames_save_path, time_interval):
    '''
    :param videos_path: 视频的存放路径
    :param frames_save_path: 视频切分成帧之后图片的保存路径
    :param time_interval: 保存间隔
    :return:
    '''
    cap = cv2.VideoCapture(videos_path)
    frame_index, frame_count = 0, 0

    # 视频是否加载成功
    if cap.isOpened():
        open_success = True
    else:
        open_success = False
        print("视频加载失败")

    try:
        # 读取视频帧并写入
        while open_success:
            read_success, frame = cap.read()
            # print("------>正在读取第%d帧" % frame_index, read_success)

            if frame_index % time_interval == 0:
                # resize_frame = cv2.resize(frame, (frame_width, frame_height), interpolation=cv2.INTER_AREA)  # 定义帧的格式
                frame_name = "%d.jpg" % frame_count
                each_video_frame_path_name = os.path.join(frames_save_path, frame_name)  # 每一帧保存路径与帧命名
                cv2.imwrite(each_video_frame_path_name, frame)
                # print("------>写入第%d帧" % frame_count)
                frame_count += 1

            frame_index += 1
    except cv2.error:  # 异常处理,每个视频处理结束后会出异常
        print(video_name, "处理完成")

    cap.release()

调用之前需要确保写入图片的路径存在:http://comonly.cn/l/p/1887.html

本文转自:https://www.cnblogs.com/welan/p/13743305.html
新加评论 评论标题: