title: How to use OpenCV VideoCapture inside for loop?
body: <p>I am trying to convert videos into frames using VideoCapture. Inside my directory 'startpath', I have multiple .mp4 files. I want to run a for loop using which all those files will convert to .png frames and will store in a separate folder for each video. </p>
  
  <p>But I am not sure how to make it work for multiple files using for loop.</p>
  
  <p>I am able to get the code working if there is only 1 file. Below is the code-:</p>
  
  <pre><code>import cv2
  import os, fnmatch
  
  startpath='C:/Users/work/Documents/data'
  listOfFiles = os.listdir(startpath)
  
  print(listOfFiles)
  pattern = "*.mp4"
  names = []
  for entry in listOfFiles:  
      if fnmatch.fnmatch(entry, pattern):
          names.append(entry)
  print (names) #this list has all the .mp4 files
  vidcap=cv2.VideoCapture(C:/Users/work/Downloads/SampleVideo_1280x720_30mb.mp4)
  success,image = vidcap.read()
  count = 0
  hello = os.path.join(startpath, 'output')
  output = os.mkdir(hello)
  os.chdir(hello)
  while success:
     cv2.imwrite("frame%d.jpg" % count, image) 
     # save frame as JPEG file
     success,image = vidcap.read()
     print('Read a new frame: ', success)
     count += 1
  </code></pre>
  
  <p>Any help in this will be highly appreciated. </p>
  
  <p>Thank You</p>