If you have a lot of videos in a folder and there are folders in the folder and folders in the folder also have videos, how can you read and save them one by one. So I wrote a code to use os,walk, this can traverse all the files and folders in the folder
import os import cv2 cut_frame = 250 # How many frames to cut, just set it yourself save_path = "C:\ Literature and Information\Handheld Infrared\Pictures" for root, dirs, files in (r"C:\Documentation and Information\Handheld Infrared"): # Just fill in the folder directory here for file in files: # Get file path if ('.mp4' in file): path = (root, file) video = (path) video_fps = int((cv2.CAP_PROP_FPS)) print(video_fps) current_frame = 0 while (True): ret, image = () current_frame = current_frame + 1 if ret is False: () break if current_frame % cut_frame == 0: # (save_path + '/' + file[:-4] + str(current_frame) + '.jpg', # image) # file[:-4] is to remove the ".mp4" suffix, here my naming format is, the video file name + the current frame number + .jpg, using imwrite can not have the Chinese path and Chinese file name ('.jpg', image)[1].tofile(save_path + '/' + file[:-4] + str(current_frame) + '.jpg') # Use imencode to include Chinese in the entire path, and the filename can also be Chinese print('Saving' + file + save_path + '/' + file[:-4] + str(current_frame))
ps: see below python traversing folders
import os # Traversing folders def walkFile(file): for root, dirs, files in (file): # root indicates the path to the folder currently being accessed # dirs Indicates the list of subdirectories under this folder. # files Indicates a list of files in the folder # Traversing the document for f in files: print((root, f)) # Iterate through all the folders for d in dirs: print((root, d)) def main(): walkFile("f:/ostest/") if __name__ == '__main__': main()
summarize
The above is a small introduction to python using openCV to traverse all the video files in the folder and save it as an image, I hope it will help you, if you have any questions please leave me a message, I will reply to you in time. I would also like to thank you very much for your support of my website!
If you find this article helpful, please feel free to reprint it, and please note the source, thank you!