Download dependencies
pip install opencv-python==4.0.0.21
accomplish
Method 1
def video_to_frames(video_path, outPutDirName): """ Extract video frames and save them as pictures :param video_path: :param outPutDirName: :return: """ times = 0 # Extract the frequency of the video, extract one for every 1 frame frame_frequency = 1 # Create a directory if the file directory does not exist if not (outPutDirName): (outPutDirName) # Read video frames camera = (video_path) while True: times = times + 1 res, image = () if not res: print('not res , not image') break #Storage video frames according to the set interval if times % frame_frequency == 0: create_path = (outPutDirName, f"{str(times)}.jpg") (create_path, image) ('Picture extraction ends') # Release the camera device () def image_to_video(image_path, media_path, fps): ''' Image synthesis video function :param image_path: image path :param media_path: Synthesized video save path :return: ''' # Get all the pictures names below the image path image_names = (image_path) # Sort the extracted image names image_names.sort(key=lambda n: int(n[:-4])) # Set the write format fourcc = cv2.VideoWriter_fourcc('M', 'P', '4', 'V') # Set the number of frames per second fps = fps # Read the first image to get the size, because the size of the image you need to convert to the video is the same image = ((image_path, image_names[0])) # Initialize the media write object media_writer = (media_path, fourcc, fps, ) # traverse the pictures and add each picture to the video for image_name in image_names: im = ((image_path, image_name)) media_writer.write(im) # Release media write object media_writer.release() ('Silent video writing is completed! ')
Method 2
import numpy as np import cv2 import os import sys def cut(video_file, target_dir): cap = (video_file) # Get a video isOpened = # Determine whether it is turned on # Create a folder with the so-called file name for a single video temp = (video_file)[-1] dir_name = ('.')[0] single_pic_store_dir = (target_dir, dir_name) if not (single_pic_store_dir): (single_pic_store_dir) i = 0 while isOpened: i += 1 (flag, frame) = () # Read an image fileName = 'image' + str(i) + ".jpg" if (flag == True): # The following three lines perform rotation #frame = np.rot90(frame, -1) #print(fileName) # Set the save path save_path = (single_pic_store_dir, fileName) #print(save_path) res = (save_path, frame, [cv2.IMWRITE_JPEG_QUALITY, 70]) #print(res) else: break return single_pic_store_dir if __name__ == '__main__': video_file = 'I:/' cut(video_file, 'I:/data/')
Method 3
#!/usr/bin/env python import cv2 numer = 18 cap=("/home/linux/work/python/video/"+str(numer)+".mp4") if (): ret,frame=() else: ret = False n=0 i=0 timeF = 40 path='/home/linux/work/python/video/'+str(numer)+'/{}' while ret: n = n + 1 ret,frame=() if (n%timeF == 0) : i = i+1 print(i) filename=str(numer)+"_"+str(i)+".jpg" ((filename),frame) (1) ()
Replenish
In addition to video frame segmentation, Python can also synthesize frames into video streams. The following is the implementation code.
#!/usr/bin/env python import cv2 img = ('/home/linux/work/python/img/1_475.jpg') imginfo = size = (imginfo[1],imginfo[0]) print(size) fourcc = cv2.VideoWriter_fourcc(*"mp4v") videoWrite = ('/home/linux/work/python/2.mp4',fourcc,10,size) for i in range(475,2208): filename = '/home/linux/work/python/img/1_'+str(i)+'.jpg' img = (filename,1) (img) print(i) () print('end')
This is the article about how to implement video frame-breaking in Python. For more related Python video frame-breaking content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!