Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to decompose video into pictures and composite video with Python

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces Python how to achieve video decomposition into pictures and picture synthesis video related knowledge, the content is detailed and easy to understand, simple and fast operation, with a certain reference value, I believe that after reading this Python how to achieve video decomposition into pictures and picture synthesis video articles will have a harvest, let's take a look.

First, python video split + picture synthesis (source code 1)

1.python video split import cv2 def video2frame (videos_path,frames_save_path,time_interval):'': param videos_path: video storage path: param frames_save_path: after the video is split into frames, the image is saved: param time_interval: save interval: return:''vidcap = cv2.VideoCapture (videos_path) success, image = vidcap.read () count = 0 while success: success Image = vidcap.read () count + = 1 if count% time_interval = 0: cv2.imencode ('.jpg' Image) [1] .tofile (frames_save_path + "/ frame%d.jpg"% count) # if count = = 20: # break print (count) if _ _ name__ ='_ _ main__': videos_path = renders:\ py\ python3.7\ test\ test98youhuashiping\ shipingchaifen\ 1.mp4' frames_save_path = renders:\ py\ python3.7\ test\ test98youhuashiping\ shipingchaifen' time_interval = save video2frame every other frame (videos_path Frames_save_path, time_interval) 2.python picture composition import cv2import osimport numpy as npfrom PIL import Image def frame2video (im_dir,video_dir,fps): im_list = os.listdir (im_dir) im_list.sort (key=lambda x: int (x.replace ("frame", "). Split ('.') [0]) # it's best to see if img = Image.open (os.path.join (im_dir)) Im_ list [0]) img_size = img.size # to get the image resolution The resolution of the images under the im_dir folder needs to be consistent # fourcc = cv2.cv.CV_FOURCC ('Masters, cv2.VideoWriter_fourcc, fourcc, fps) # opencv version is 2 fourcc = cv2.VideoWriter_fourcc (*' XVID') # opencv version is 3 videoWriter = cv2.VideoWriter (video_dir, fourcc, fps) Img_size) # count = 1 for i in im_list: im_name = os.path.join (im_dir+i) frame = cv2.imdecode (np.fromfile (im_name, dtype=np.uint8) -1) videoWriter.write (frame) # count+=1 # if (count = = 200): # print (im_name) # break videoWriter.release () print ('finish') if _ name__ =' _ _ main__': im_dir = ritual:\ py\ python3.7\ test\ shipingchaifen\ pho/'# frame storage path video_dir = Renee:\ py\ python3.7\ test\ test98youhuashiping\ shipingchaifen/test.mp4' # the path to the storage of the composite video fps = 30 # frame rate The more frames per second, the smoother the movements displayed. Frame2video (im_dir, video_dir, fps)

Tip: do not appear Chinese and special characters in the path, and the writing should be standardized!

Second, python video split + picture synthesis (source code 2)

Import cv2import numpy as npimport osos.chdir (ringing E:\ py\ python3.7\ test\ test98youhuashiping\ chaifen') # # reads the video and decomposes it frame by frame into pictures cap = cv2.VideoCapture ('1.mp4') # Open a video isOpened = cap.isOpened () # to determine whether to open print (isOpened) # to get information about the video. The width of each picture of the video is the same fps = cap.get (cv2.CAP_PROP_FPS) # frame rate That is, how many pictures per second make up width = int (cap.get (cv2.CAP_PROP_FRAME_WIDTH)) # get the width height = int (cap.get (cv2.CAP_PROP_FRAME_HEIGHT)) # get the height print (fps,width,height) # output related information I = 0 while (isOpened): # read the image of the first two seconds of the video Total 2*int (fps) if I = = int (fps) * 2: break else: I = iTun1 (flag,frame) = cap.read () # read each flag frame filename = 'image'+str (I) +' .jpg'# write the read picture to the file, if flag = = True: cv2.imwrite (filename,frame, [cv2.IMWRITE_JPEG_QUALITY,100]) # determine the picture quality 100 is a high print ('endgame') # # read scattered pictures (the decomposed pictures above) and synthesize them into video img = cv2.imread ('image1.jpg') imginfo = img.shapesize = (imginfo [1], imginfo [0]) # different from the default, opencv uses height before width, all need to reorder print (size) # to create write objects, including the new video name How many frames per second (10 pictures), size size # the lowest resolution for the average person's eye is 19 frames per second videoWrite = cv2.VideoWriter ('2.mp4) for i in range (1) for i in range (1): filename =' image'+str (I) + '.jpg' img = cv2.imread (filename,1) # 1 represents a color picture 0 means grayscale image # directly write the data corresponding to the picture videoWrite.write (img) videoWrite.release () # turn off the writing object print ('end') 3, python video split (source code 3)

Import cv2 # Import opencv module import osimport time def video_split (video_path,save_path):''cut the video file into frames' @ param video_path: video path @ param save_path: save the path of the segmented frame 'vc=cv2.VideoCapture (video_path) # how many frames are needed for frame-by-frame if vc.isOpened (): rval Frame=vc.read () else: rval=False while rval: rval,frame=vc.read () # extract 2 pictures per second if% 2 = = 0: cv2.imwrite (save_path + "/" + str ('dumped% c) +' .jpg' Frame) cv2.waitKey (1) c=c+1 DATA_DIR = r "E:\ py\ python3.7\ test\ test98youhuashiping\ ceshi\ mp4" # Video data home directory SAVE_DIR = r "E:\ py\ python3.7\ test\ test98youhuashiping\ ceshi\ pho2" # frame file save directory start_time = time.time () for parents,dirs Filenames in os.walk (DATA_DIR): # if parents = = DATA_DIR: # continue print ("processing folder", parents) path = parents.replace ("\\" "/ /") f = parents.split ("\\") [1] save_path = SAVE_DIR + "/ /" + f # traverse each video data for file in filenames: file_name = file.split (".") [0] save_path_ = save_path + "/" + file_name if not os.path.isdir (save_path_): Os.makedirs (save_path_) video_path = path + "/" + file video_split (video_path Save_path_) end_time = time.time () print ("Cost time", start_time-end_time) on "how to decompose video into pictures and compositing videos with Python" ends here Thank you for reading! I believe you all have a certain understanding of "how to decompose Python into pictures and picture synthesis videos". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report