Skip to content

动态画面帧提取

更新: 2025/2/24 字数: 0 字 时长: 0 分钟

python
import cv2
import os

def video_to_frames(video_path, outPutDirName):
    times = 0
    # 提取视频的频率,每1帧提取一个
    frame_frequency = 1
    # 如果文件目录不存在则创建目录
    if not os.path.exists(outPutDirName):
        os.makedirs(outPutDirName)
    # 读取视频帧
    camera = cv2.VideoCapture(video_path)
    # 循环提取视频帧
    while True:
        times = times + 1
        res, image = camera.read()
        if not res:
            print('图片提取结束')
            break
        # 按照设置间隔存储视频帧
        if times % frame_frequency == 0:
            cv2.imwrite(outPutDirName + '\\' + str(times) + '.jpg', image)
    # 释放摄像头设备
    camera.release()


if __name__ == '__main__':
    video_to_frames(r'视频路径', r'画面帧保存路径')