Opencv uses cv2 to change video resolution and size

Change video resolution and size with CV2. code show as below:



​import cv2
def video():
    videoCapture = cv2.VideoCapture('.mp4')

    fps = 30           # 保存视频的帧率,可改变
    size = (1920, 1080)  # 保存视频大小

    videoWriter = cv2.VideoWriter('/Users/stella/Desktop/Meidapipe/Mediapipe_new.mp4',
                                 cv2.VideoWriter_fourcc('X','V','I','D'), fps, size)

    while True:
        success, frame = videoCapture.read()
        if success:
            img = cv2.resize(frame, size)
            videoWriter.write(img)
        else:
            print('break')
            break

    #释放对象,不然可能无法在外部打开
    videoWriter.release()


if __name__ == '__main__':
    video()
    print("end!")

in

 VideoWriter(filename, fourcc, fps, frameSize[, isColor])

  filename: is the path of the file to be saved
  fourcc: specifies the encoder, generally there are "DIVX", "MJPG", "XVID", "X264", which encoders are installed according to your computer environment.
  fps: the frame rate of the video to be saved
  frameSize: the screen size of the file to be saved isColor   : indicates whether it is
  black and white or color
) function size, otherwise the video will fail to be stored.

  

cv2.resize(src, size, interpolation)

  src - original image

  The size of the video after size-resize, same as in VideoWriter

  interpolation - the interpolation method. There are 5 types:

  INTER_NEAREST - nearest neighbor interpolation

  INTER_LINEAR - bilinear interpolation (default)

  INTER_AREA - resampling using pixel area relation. For image decimation, this might be a better approach. But if the image is enlarged, it has a similar effect to the nearest neighbor method.

  INTER_CUBIC - Cubic interpolation based on 4x4 pixel neighborhoods

  INTER_LANCZOS4 - Lanczos interpolation based on 8x8 pixel neighborhoods

  Usually, use cv.INTER_AREA for zooming out, and cv.INTER_CUBIC (slower) and cv.INTER_LINEAR (faster for better results) for scaling. By default, all scaling uses cv.INTER_LINEAR.

Guess you like

Origin blog.csdn.net/stellao_o/article/details/122969981