For a single video conversion picture see:

Python OpencV turns videos into images

 

For multiple videos, multithreading is used

Video_path is the path of the video to be converted, and PIC_path is the path of the output image. The output image is saved in a folder with the same name as the corresponding video under PIC_path.

The code is as follows:

# coding=utf-8 import cv2 import os import threading from threading import Lock, Thread video_path = "./video/" pic_path = "./pic/" filelist = os.listdir(video_path) def video2pic(filename): # print(filename) cnt = 0 dnt = 0 if os.path.exists(pic_path + str(filename)): pass else: Os.mkdir (pic_path + STR (filename)) cap = cv2.videocapture (video_path + STR (filename)) # get a frame ret, image = cap.read() if image is None: break # show a frame w = image.shape[1] h = image.shape[0] if (cnt % 20) == 0: cv2.imencode('.jpg', image)[1].tofile(pic_path + str(filename) + '/' + str(dnt) + '.jpg') print(pic_path + str(filename) + '/' + str(dnt) + '.jpg') dnt = dnt + 1 cnt = cnt + 1 if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() if __name__ == '__main__': for filename in filelist: threading.Thread(target=video2pic, args=(filename, )).start()Copy the code