In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Python how to convert video into pictures, this article introduces the corresponding analysis and answers in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.
Preface
The code is bald, but I like the logic and problem-solving power of the code.
(I'm going to modify the picture of this project to zoom out to see that it looks a bit like QAQ.)
Code presentation
This code can realize the conversion of multiple video clips in multiple folders. The specific meaning of the code can be referred to the comments.
The specific action is to copy and paste this code into the created main.py file, configure the source file path on line 63, and then run it.
# convert multiple video files in four folders into pictures import cv2import osnumber_in_dir = [] # you can remove def video2image (video_path, image_save_dir): # 1. Add the folder name of the source file to the storage path of the picture, such as' Coffee_room_01', etc. If os.path.exists (video_path): # determine whether the source path is correct print (video_path +'\ t ok') if video_path.split ('\\') [- 2]! = 'Videos': # this image_save_dir + = video_path.split ('\\') [- 2] +'\\'is added according to my file path. Else: image_save_dir + = video_path.split ('\\') [- 3] +'\\ 'number_in_dir.append (len (os.listdir (video_path) # can remove else: print (video_path +' 033 [0 37% 41merror\ 033 [0m') return # 2. Read the files in the source file in turn, and if the suffix is' avi' or 'MP3', create a subfolder about the file name files_in_video_path_list = os.listdir (video_path) print (len (files_in_video_path_list)) # to get the number of files under the current folder, which is used to determine whether there are other types of files file_count = 0 # for counting Verify that they are all video files Will be compared with len (files_in_video_path_list) for file_in_video_path in files_in_video_path_list: file_name = os.path.basename (file_in_video_path) if file_name.split ('.) [- 1] = = 'avi' or file_name.split ('.) [- 1] = 'mp3': file_count + = 1 # video Number of files + 1 every_video_save_dir = file_name.split ('.') [0] +'\\'if not os.path.exists (image_save_dir + every_video_save_dir): # create a storage path that belongs to the corresponding folder os.makedirs (image_save_dir + every_video_save_dir) else: print ('\ 033 [0) 37% 41merror\ 033 [0m') break # 3. Print the sequence number of the file being processed and his file name, and start converting print (str (file_count) +':'+ file_name) cap = cv2.VideoCapture (video_path + file_name) flag = cap.isOpened () if not flag: print ("open" + video_path + file_name + "error!") Frame_count = 0 # label each frame while True: frame_count + = 1 flag Frame = cap.read () if not flag: # exit break if os.path.exists (image_save_dir + every_video_save_dir + str (frame_count) + '.jpg') if you have read the last frame: # if the source video remains unchanged If you have already created Skip break cv2.imwrite (image_save_dir + every_video_save_dir + str (frame_count) + '.jpg' Frame) cap.release () print (file_name + 'save to' + image_save_dir + every_video_save_dir + 'finished') # indicates that a video clip has been converted if file_count! = len (files_in_video_path_list): print ('\ 033 [0) 37 41Maybe loss something\ 033 [0m') if _ _ name__ ='_ _ main__': # list of file paths to be converted (custom modification) video_path_list = ['E:\\ Datasets_FallDetection\\ 2 LE2I\ Coffee_room_01\\ Coffee_room_01\\ Videos\','E:\\ Datasets_FallDetection\\ 2 LE2I\ Home_01\\ Home_01\ Videos\\' 'e:\\ Datasets_FallDetection\\ 2 LE2I\ Lecture_room\\ Lecture room\','E:\\ Datasets_FallDetection\\ 2 LE2I\\ Office\\ Office\'] # the home folder where it is expected to be stored That is, 'result' folder image_save_dir ='.\\ result\\ # to convert for video_path in video_path_list: video2image (video_path, image_save_dir) result display
The 'result' main folder and subfolders will be created
Under each subfolder, a subfolder corresponding to the video name is generated and converted into jpg pictures in the order of the number of video frames.
The terminal will prompt the path of the file being processed, which video is being converted and the conversion completed.
This is the answer to the question about how Python converts video into pictures. I hope the above content can be of some help to you. If you still have a lot of questions to solve, you can follow the industry information channel to learn more about it.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.