Read and save videos in python
Read video
The advantage of using the cv2 library is that the return is array, without conversion, but this method is to read from beginning to end
Use the cv2 library to read videos, the code implementation is as follows:
import cv2 cap = ('C:\\Users\\xxx\\Desktop\\sweet.mp4') while(()): # ret returns boolean ret, frame = () # Display the read video matrix ('image', frame) # keyboard waiting k = (20) # q key exit if k & 0xff == ord('q'): break # Free up resources() # Close the window()
Read and save videos
- Read video
- Process each frame
- Save the video
import cv2 # VideoCapture method is a method for reading video provided by cv2 librarycap = ('C:\\Users\\xxx\\Desktop\\sweet.mp4') # Set the format "xvid" that needs to be saved# This parameter is MPEG-4 encoding type, with the file name suffixed with .avifourcc = cv2.VideoWriter_fourcc(*'XVID') # Set video frame ratefps = (cv2.CAP_PROP_FPS) # Set the video sizesize = (int((cv2.CAP_PROP_FRAME_WIDTH)),int((cv2.CAP_PROP_FRAME_HEIGHT))) # VideoWriter method is a method to save video provided by cv2 library# Output output according to the set formatout = ('C:\\Users\\xxx\\Desktop\\',fourcc ,fps, size) # Make sure the video is turned on and loop to readwhile(()): # Read frame by frame, ret returns a boolean value # The parameter ret is True or False, which means whether the image has been read #frame means an image that has been captured in a frame ret, frame = () if ret == True: # Vertical flip matrix frame = (frame,0) (frame) ('frame',frame) if (1) & 0xFF == ord('q'): break else: break # Free up resources() () # Close the window()
() is a keyboard binding function. Its time measurement is milliseconds ms. The function will wait for n milliseconds in (n) to see if there is keyboard input. If there is keyboard input, the ASCII value of the key is returned. Without keyboard input, return -1. Generally set to 0, it will wait for keyboard input wirelessly.
cv2.VideoWriter_fourcc() function
fourcc means four character codes (Four-Character Codes), which isFourComponent of 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'), this parameter is YUV encoding type, and the file name is suffixed with .avi
- cv2.VideoWriter_fourcc('P','I','M','I'), this parameter is the MPEG-1 encoding type, and the file name is suffixed.avi
- cv2.VideoWriter_fourcc('X','V','I','D'), this parameter is the MPEG-4 encoding type, with the file name suffixed.avi
- cv2.VideoWriter_fourcc('T','H','E','O'), this parameter is Ogg Vorbis, and the file name is suffixed.ogv
- cv2.VideoWriter_fourcc('F','L','V',1), this parameter is Flash video, the file name is suffixed.flv
()function
grammar:
(src, flipCode[, dst]) #src is the image to be operated
How to use flipCode:
flipCode=-1 #Horizontal and vertical flipflipCode= 0 #Flip verticallyflipCode= 1 #Horizontal Flip
application:
('img1',(img,-1))#Horizontal and vertical flip('img2',(img,0))#Flip vertically('img3',(img,1))#Horizontal Flip
This is the article about the operation steps of python using the cv2 library to read and save videos. For more related python reading and saving video content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!