【python实现视频解帧并保存文件夹】

  • 1、功能说明
  • 2、代码实现
  • 3、函数方法解析
  • ① os.getcwd()
  • ② cv2.VideoCapture().get方法获取视频的相关参数
  • ③ cv2.VideoCapture().read()方法
  • ④ cv2.waitKey()方法
  • Reference


1、功能说明

利用python代码实现解帧视频,并保存到创建的文件夹,方便后续操作

一起来嗨皮呀@_@

python png解码 python解码视频_python

2、代码实现

# #!/usr/bin/env python
# # -*- coding: utf-8 -*-
# # @Time    : 2021/10/24
# # @Author  : Wupke
# # Purpose:  解帧视频,保存到创建的文件夹

import cv2
import os

def get_frame_from_video(video_path, interval):
    """
           video_name:输入视频路径(除了视频名称之外,读入视频的绝对路径中文件夹一定不要出现中文字符,不然不能保存图片)
           interval: 保存图片的帧率间隔
       
    """
    # 创建存帧文件夹
    path = os.getcwd() + '\\frame\\'
    # pwd = os.getcwd() + '\\frame\\' + "\\video1\\"
    if not os.path.exists(path):
        os.mkdir(path)
        print("directory made")
    # elif not os.path.exists(pwd):
    #     os.mkdir(pwd)
    else:
        print("directory existed")

    video_capture = cv2.VideoCapture(video_path)

    frame_nums = video_capture.get(7)   # 获取视频总帧数
    print("视频的总帧数为:",int(frame_nums))   
    frame_rete = video_capture.get(5)   # 获取视频帧率
    print("视频的帧率为:",int(frame_rete))   

    i = 0   # i 从 0 开始计数的帧数
    j = 0   # j 从 1 开始,记录第几次间隔
    
    while True:
        success, frame = video_capture.read() # 一直在读入视频画面帧
        i += 1
        # 判断帧率间隔保存帧
        if i % interval == 0:
            j += 1
            save_name = path + str(i) + '.jpg'
            cv2.imwrite(save_name, frame)
            print('image of %s is saved' % (save_name))
        if not success:
            print('%s frames saved from the video'%j)
            break

if __name__ == '__main__':
    # 视频路径:(除了视频名称之外,读入视频的绝对路径中文件夹一定不要出现中文字符,不然不能保存图片)
    video_path = r'E:\\Desktop//003_Lab_work_space/num_4.mp4'
    interval = 10
    get_frame_from_video(video_path, interval)

3、函数方法解析

① os.getcwd()

该函数不需要传递参数,它返回当前所在的目录。
当前目录并不是指脚本所在的目录,而是所运行脚本的目录。

② cv2.VideoCapture().get方法获取视频的相关参数
import cv2

video_name = r'E:\Projects\video\shichang1.mp4'
video_capture = cv2.VideoCapture(video_name)
frame_nums = video_capture.get(7)   # 获取视频总帧数
print(frame_nums)

cv2.VideoCapture().get方法按预定的顺序获取视频的相关信息
从0开始编号,opencv设定的参数列表如下:

0 CV_CAP_PROP_POS_MSEC Current position of the video file in milliseconds or video capture timestamp.
1 CV_CAP_PROP_POS_FRAMES 0-based index of the frame to be decoded/captured next.
2 CV_CAP_PROP_POS_AVI_RATIO Relative position of the video file: 0 - start of the film, 1 - end of the film.
3 CV_CAP_PROP_FRAME_WIDTH    #视频帧宽度
4 CV_CAP_PROP_FRAME_HEIGHT    #视频帧高度
5 CV_CAP_PROP_FPS            #视频帧速率
6 CV_CAP_PROP_FOURCC 4-character code of codec.
7 CV_CAP_PROP_FRAME_COUNT  #视频总帧数
8 CV_CAP_PROP_FORMAT Format of the Mat objects returned by retrieve() .
9 CV_CAP_PROP_MODE Backend-specific value indicating the current capture mode.
10 CV_CAP_PROP_BRIGHTNESS Brightness of the image (only for cameras).
11 CV_CAP_PROP_CONTRAST Contrast of the image (only for cameras).
12 CV_CAP_PROP_SATURATION Saturation of the image (only for cameras).
13 CV_CAP_PROP_HUE Hue of the image (only for cameras).
14 CV_CAP_PROP_GAIN Gain of the image (only for cameras).
15 CV_CAP_PROP_EXPOSURE Exposure (only for cameras).
16 CV_CAP_PROP_CONVERT_RGB Boolean flags indicating whether images should be converted to RGB.
17 CV_CAP_PROP_WHITE_BALANCE_U The U value of the whitebalance setting (note: only supported by DC1394 v 2.x backend currently)
18 CV_CAP_PROP_WHITE_BALANCE_V The V value of the whitebalance setting (note: only supported by DC1394 v 2.x backend currently)
19 CV_CAP_PROP_RECTIFICATION Rectification flag for stereo cameras (note: only supported by DC1394 v 2.x backend currently)
20 CV_CAP_PROP_ISO_SPEED The ISO speed of the camera (note: only supported by DC1394 v 2.x backend currently)
21 CV_CAP_PROP_BUFFERSIZE Amount of frames stored in internal buffer memory (note: only supported by DC1394 v 2.x backend currently)
③ cv2.VideoCapture().read()方法
import cv2

cap = cv2.VideoCapture(path)
# 参数path可以写本地路径或者打开设备摄像头
ret, frame = cap.read()

''' cap.read()按帧读取视频,ret,frame是获cap.read()方法的两个返回值。
其中ret是布尔值,如果读取帧是正确的则返回True,
如果文件读取到结尾,它的返回值就为False。frame就是每一帧的图像,是个三维矩阵。
'''
# 如:
while True:
success, frame = video_capture.read() # 一直在读入视频画面帧
④ cv2.waitKey()方法

waitKey()方法本身表示等待键盘输入
cv2.waitKey(1)
参数是1,表示延时1ms切换到下一帧图像,对于视频而言;
参数为0,如cv2.waitKey(0)只显示当前帧图像,相当于视频暂停,;