title: More efficient method to save 10 frames per second from a video between given timecode - Python
body: <p>Ive got a set of videos, and a CSV file that with heading(start, end, category, season, episode) with the task to save 10 images per second between the start and end points in each row of the file. The method I devised (see below) first reads the CSV file and creates a list of integers (every 100 ms between the start and end points). Then using cv2 I load the video, go to the frame given in the list and progressively save each one. </p>
  
  <p>My question in wether there is a quicker way to do this, whilst I havent done any speed tests or anything like that, the method just seems a bit slow and clunky. Any help appreciated.</p>
  
  <pre><code>import csv
  import cv2
  
  def readCSV():
      times = []
      reader = csv.DictReader(open("timecode.csv"))
      for i in reader:       
          l = range( ((int(str(i["start"])[0:2]))*60000) + 
                     ((int(str(i["start"])[2:4]))*1000), 
  
                     ((int(str(i["end"])[0:2]))*60000) + 
                     ((int(str(i["end"])[2:4]))*1000) )
          for j in l[::100]:
              times.append([i["season"], i["episode"], j, i["interaction"]])
  
      return times
  
  def vid_to_frame(season, episode, timecode, category):
      vid = cv2.VideoCapture("s{}_e{}.mp4".format(str(season).zfill(2),
                                                  str(episode).zfill(2)))
      vid.set(cv2.CAP_PROP_POS_MSEC, float(timecode))
      success, image = vid.read()
      if success:
          cv2.imwrite("E:/Hammond/readingVideos/imageSamples/{}/{}.jpg"
                      .format(str(category), str(timecode)), image)
  
  for n in readCSV():
      vid_to_frame(n[0], n[1], n[2], n[3])
  </code></pre>