This is the third day of my participation in Gwen Challenge

OpenCV is a C++ library, currently popular computer vision programming library, for real-time processing of computer vision problems, it covers a lot of modules in the field of computer vision. In Python, the OpenCV library is often used for image processing.

This article explains how to read and write video files using OpenCV in Python3:

OpenCV provides VideoCapture class and VideoWriter class to support various formats of video file read and write, in different systems may support the format will be different, but in general is to support AVI format video.

Before the video ends, each frame can be read using the read() method in the VideoCapture class. Each frame is a BGR image.

You can use the Write () method in the VideoWriter class to save image information to a file pointed to by the VideoWriter class.

The code looks like this:

import cv2
videoCapture = cv2.VideoCapture("E:\\demo\\0603\\1.mp4"FPS = videoCapture. Get (cv2.cap_prop_fps) size = (int(videoCapture. Get (cv2.cap_prop_frame_width)), Int (videoCapture. Get (cv2.cap_prop_frame_height)) # Set the output video information (video file name, codec, frame rate, size) videoWriter = cv2.videoWriter ("E:\\demo\\0603\\1.avi",cv2.VideoWriter_fourcc('I'.'4'.'2'.'0'), FPS,size) # Read the video file, if the video to read has not finished, then success will receive True, each frame of the picture information saved in the frame, Write to specified file success,frame = videoCapture.read()while success:
    videoWriter.write(frame)
    success, frame = videoCapture.read()
Copy the code

The result output is:

Results:

This code reads the original video file 1.mp4 frame, and uses YUA color coding to write it into another video frame file 1.avi

Note that:

The Constructor of the VideoWriter class must specify a file name for the video, overwriting it if it exists. The video codec must also be specified. The viability of codecs varies from system to system. You need to specify the frame rate and size, which can be obtained using the get() method.

Common codecs:

  • Cv2. VideoWriter_fourcc (' I ', '4', '2', '0'). This option is uncompressedYUVColor coded, yes4:2:0Chrominance subsampling, this encoding method has good compatibility, but will produce a relatively large file, file extension .avi.
  • Cv2. VideoWriter_fourcc (' P ', 'I', 'M', '1'). This option isMPEG-1Encoding type, file extension .avi.
  • Cv2. VideoWriter_fourcc (' X ', 'V', 'I', 'D'). This option isMPEG-4If you want to get an average video size, use this option. The file name extension is .avi.
  • Cv2. VideoWriter_fourcc (' T ', 'H', 'E', 'O'). This option isOgg Vorbis, the file extension should be .ogv.
  • Cv2. VideoWriter_fourcc (' F ', 'L', 'V', '1'). This option isFlashVideo, file extension should be .flv.

The frame rate and frame size must also be specified because the video frame needs to be copied from another video file. These properties can be obtained via the Get () function of the VideoCapture class.

A series of articles will follow this month,

Wonderful article, please pay attention.