Python video operation - python realizes reading and saving video

python read and save video

The content comes from blog + blog ~

read video

The advantage of using the cv2 library is that the return is arrary without conversion, but this method is read from beginning to end

Using the cv2 library to read the video, the code is implemented as follows:

import cv2

cap = cv2.VideoCapture('C:\\Users\\xxx\\Desktop\\sweet.mp4')

while(cap.isOpened()):
    # ret返回布尔值
    ret, frame = cap.read()
    # 展示读取到的视频矩阵
    cv2.imshow('image', frame)
    # 键盘等待
    k = cv2.waitKey(20)
    # q键退出
    if k & 0xff == ord('q'):
        break

# 释放资源
cap.release()
# 关闭窗口
cv2.destroyAllWindows()

read and save video

  1. read video
  2. Process each frame
  3. save video
import cv2

# VideoCapture方法是cv2库提供的读取视频方法
cap = cv2.VideoCapture('C:\\Users\\xxx\\Desktop\\sweet.mp4')
# 设置需要保存视频的格式“xvid”
# 该参数是MPEG-4编码类型,文件名后缀为.avi
fourcc = cv2.VideoWriter_fourcc(*'XVID')
# 设置视频帧频
fps = cap.get(cv2.CAP_PROP_FPS)
# 设置视频大小
size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
# VideoWriter方法是cv2库提供的保存视频方法
# 按照设置的格式来out输出
out = cv2.VideoWriter('C:\\Users\\xxx\\Desktop\\out.avi',fourcc ,fps, size)

# 确定视频打开并循环读取
while(cap.isOpened()):
    # 逐帧读取,ret返回布尔值
    # 参数ret为True 或者False,代表有没有读取到图片
    # frame表示截取到一帧的图片
    ret, frame = cap.read()
    if ret == True:
        # 垂直翻转矩阵
        frame = cv2.flip(frame,0)

        out.write(frame)

        cv2.imshow('frame',frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break

# 释放资源
cap.release()
out.release()
# 关闭窗口
cv2.destroyAllWindows()

cv2.waitKey() is a keyboard binding function. Its time measure is milliseconds ms. The function will wait for n milliseconds inside (n) to see if there is keyboard input. If there is keyboard input, return the ASCII value of the key. If there is no keyboard input, it returns -1. Generally set to 0, it will wirelessly wait for keyboard input.

cv2.VideoWriter_fourcc() function

fourcc means four-character codes (Four-Character Codes), the code consists of four characters, the following are some commonly used parameters of the VideoWriter_fourcc object:

Note: character order cannot be mixed

  • cv2.VideoWriter_fourcc('I','4','2','0'), the parameter is YUV encoding type, the file name suffix is ​​.avi
  • cv2.VideoWriter_fourcc('P','I','M','I'), the parameter is MPEG-1 encoding type, the file name suffix is ​​.avi
  • cv2.VideoWriter_fourcc('X','V','I','D'), the parameter is MPEG-4 encoding type, the file name suffix is ​​.avi
  • cv2.VideoWriter_fourcc('T','H','E','O'), the parameter is Ogg Vorbis, the file name suffix is ​​.ogv
  • cv2.VideoWriter_fourcc('F','L','V',1), the parameter is Flash video, the file name suffix is ​​.flv

cv2.flip() function

grammar:

cv2.flip(src, flipCode[, dst]) #src为要操作的图像

How to use flipCode:

flipCode=-1        #水平垂直翻转
flipCode= 0        #垂直翻转
flipCode= 1        #水平翻转

application:

cv2.imshow('img1',cv2.flip(img,-1))#水平垂直翻转
cv2.imshow('img2',cv2.flip(img,0))#垂直翻转
cv2.imshow('img3',cv2.flip(img,1))#水平翻转

insert image description here

First written on August 23, 2021.

Guess you like

Origin blog.csdn.net/weixin_52777510/article/details/119872183