Video operation_01 Video read and write: video read and write + read video + save video

1 Read video from file and play


To get a video in OpenCV, we need to create a VideoCapture object and specify the video file you want to read:

1. Create an object to read the video

cap = cv.VideoCapture(filepath)

parameter:

        filepath: video file path


2. Video attribute information

2.1. Get some properties of the video,

retval = cap.get(propId)

parameter:

  • propId: a number from 0 to 18, each number represents a property of the video

Common properties are:

2.2 Modify the attribute information of the video

cap.set(propId,value)

parameter:

  • proid: the index of the property, corresponding to the table above
  • value: the modified attribute value

Determine whether the image is successfully read

 isornot = cap.isOpened()

Returns true if the read is successful, otherwise returns False
to get a frame of the video

ret, frame = cap.read()

parameter:

  • ret: If the acquisition is successful, return True, if the acquisition fails, return False
  • Frame: Get the image of a certain frame

Call cv.imshow() to display the image, and use cv.waitkey() to set the appropriate duration when displaying the image. If it is too low, the video will play very fast, and if it is too high, it will play very slowly. Usually, we set 25ms is fine.
Finally, call cap.realease() to release the video

Example:

import numpy as np
import cv2 as cv
# 1.获取视频对象
cap = cv.VideoCapture('img/01.mp4')
# 2.判断是否读取成功
while(cap.isOpened()):
    # 3.获取每一帧图像
    #ret: 若获取成功返回True,获取失败,返回False 
    #Frame: 获取到的某一帧的图像
    ret, frame = cap.read() 
    # 4. 获取成功显示图像
    if ret == True:
        cv.imshow('frame',frame)
    # 5.每一帧间隔为25ms
    if cv.waitKey(25) & 0xFF == ord('q'):
        break
# 6.释放视频对象
cap.release()
cv.destroyAllwindows()


2 Save the video


In OpenCV we save the video using the VedioWriter object, in which we specify the name of the output file, as follows:

Create an object for video writing

out = cv2.VideoWriter(filename,fourcc, fps, frameSize)

parameter:

  • filename: the location where the video is saved
  • fourcc: 4-byte code specifying the video codec
  • fps: frame rate
  • frameSize: frame size

Set the codec of the video as shown below,

retval = cv2.VideoWriter_fourcc( c1, c2, c3, c4 )

parameter:

  • c1,c2,c3,c4: is the 4-byte code of the video codec, find the list of available codes in fourcc.org, closely related to the platform, commonly used are:

                        In Windows: DIVX (.avi)

                        In OS: MJPG (.mp4), DIVX (.avi), X264 (.mkv).

  • Use cap.read() to get each frame of the video, and use out.write() to write a frame of image into the video.
  • Use cap.release() and out.release() to release resources.

Example:

import cv2 as cv

# 1. 读取视频
cap = cv.VideoCapture("img/01.mp4")

# 2. 获取图像的属性(宽和高),并将其转换为整数
frame_width = int(cap.get(3))
frame_height = int(cap.get(4))

# 3. 创建保存视频的对象,设置编码格式,帧率,图像的宽高等
out = cv.VideoWriter('outpy.avi', cv.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (frame_width, frame_height))
while (True):
    # 4.获取视频中的每一帧图像
    ret, frame = cap.read()
    if ret == True:
        # 5.将每一帧图像写入到输出文件中
        out.write(frame)
    else:
        break

# 6.释放资源
cap.release()
out.release()
cv.destroyAllWindows()

Summarize

Read the video:

  •         Read video: cap = cv.VideoCapture()
  •         Judging read success: cap.isOpened()
  •         Read each frame of image: ret, frame = cap.read()
  •         Get property: cap.get(proid)
  •         Set properties: cap.set(proid,value)
  •         Resource release: cap.release()

save video

  •         Save the video: out = cv.VideoWrite()
  •         Video writing: out.write()
  •         Resource release: out.release()

Guess you like

Origin blog.csdn.net/qq_39237205/article/details/122083255#comments_20524087