title: Recording video in specified time intervals and then saving them into file OpenCv Python
body: <p>Here are my goals.</p>
  
  <ol>
  <li>Capture video continuously until 'q; is pressed</li>
  <li>Every ten seconds save the video in created directory file</li>
  <li>Continue step two until 'q' is pressed</li>
  </ol>
  
  <p>I am executing the following code. But when creating files it's creating 6kb files and saying cannot play. I am fairly new to opencv and python. Not sure what I am missing. Running this code on pycharm with Python 3.6. Also the </p>
  
  <p>cv2.imshow('frame',frame)</p>
  
  <p>stops after ten seconds but recording is happening in background and files are created.</p>
  
  <pre><code>import numpy as np
  import cv2
  import time
  import os
  import random
  import sys
  
  
  fps=24
  width=864
  height=640
  video_codec=cv2.VideoWriter_fourcc('D','I','V','X')
  
  name = random.randint(0,1000)
  print (name)
  if (os.path.isdir(str(name)) is False):
      name = random.randint(0,1000)
      name=str(name)
  
  name = os.path.join(os.getcwd(), str(name))
  print('ALl logs saved in dir:', name)
  os.mkdir(name)
  
  
  cap = cv2.VideoCapture(0)
  ret=cap.set(3, 864)
  ret=cap.set(4, 480)
  cur_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
  
  
  start=time.time()
  video_file_count = 1
  video_file = os.path.join(name, str(video_file_count) + ".avi")
  print('Capture video saved location : {}'.format(video_file))
  
  
  while(cap.isOpened()):
      start_time = time.time()
      ret, frame = cap.read()
      if ret==True:
          cv2.imshow('frame',frame)
          if (time.time() - start &gt; 10):
              start = time.time()
              video_file_count += 1
              video_file = os.path.join(name, str(video_file_count) + ".avi")
              video_writer = cv2.VideoWriter(video_file,video_codec, fps,(int(cap.get(3)),int(cap.get(4))))
              time.sleep(10)
          if cv2.waitKey(1) &amp; 0xFF == ord('q'):
              break
      else:
          break
  cap.release()
  cv2.destroyAllWindows()
  </code></pre>
  
  <p>I want files with the recorded videos. Files are generated but size 6kb and nothing is being recorded.</p>