take screenshot of video python

Solutions on MaxInterview for take screenshot of video python by the best coders in the world

showing results for - "take screenshot of video python"
Samuel
28 Sep 2016
1import cv2
2import os
3import time
4
5def get_frames(inputFile,outputFolder,step,count):
6
7  '''
8  Input:
9    inputFile - name of the input file with directoy
10    outputFolder - name and path of the folder to save the results
11    step - time lapse between each step (in seconds)
12    count - number of screenshots
13  Output:
14    'count' number of screenshots that are 'step' seconds apart created from video 'inputFile' and stored in folder 'outputFolder'
15  Function Call:
16    get_frames("test.mp4", 'data', 10, 10)
17  '''
18
19  #initializing local variables
20  step = step
21  frames_count = count
22
23  currentframe = 0
24  frames_captured = 0
25
26  #creating a folder
27  try:  
28      # creating a folder named data 
29      if not os.path.exists(outputFolder): 
30          os.makedirs(outputFolder) 
31    
32  #if not created then raise error 
33  except OSError: 
34      print ('Error! Could not create a directory') 
35  
36  #reading the video from specified path 
37  cam = cv2.VideoCapture(inputFile) 
38
39  #reading the number of frames at that particular second
40  frame_per_second = cam.get(cv2.CAP_PROP_FPS)
41
42  while (True):
43      ret, frame = cam.read()
44      if ret:
45          if currentframe > (step*frame_per_second):  
46              currentframe = 0
47              #saving the frames (screenshots)
48              name = './data/frame' + str(frames_captured) + '.jpg'
49              print ('Creating...' + name) 
50              
51              cv2.imwrite(name, frame)       
52              frames_captured+=1
53              
54              #breaking the loop when count achieved
55              if frames_captured > frames_count-1:
56                ret = False
57          currentframe += 1           
58      if ret == False:
59          break
60  
61  #Releasing all space and windows once done
62  cam.release()
63  cv2.destroyAllWindows()
64