title: Recording a h264 video in python on a Raspberry Pi in a circular buffer and saving a specific number of seconds
body: <p>I'm using the picamera library to create a PiCamerIcrcularIO stream to write the recording data to. I can't seem to get a reliable method to always create a video file with a 6 second length to it. Depending on when I copy the video data to disk I can end up with anywhere between 3 to 6 seconds of video. Here is the code I am using to test this with:</p>
  
  <pre><code>"""Testing the video recording settings"""
  import datetime as dt
  import os
  from random import randint
  import subprocess
  import sys
  
  import picamera
  
  
  class Camera(object):
      """Camera device"""
      def __init__(self):
          self.device = None  # type: picamera.PiCamera
          self.stream = None  # type: picamera.PiCameraCircularIO
  
      def initialize_camera(self):
          """Initializes the camera to 1280x720 and start recording video to a circular buffer"""
          print "initializing camera"
          self.device = picamera.PiCamera()
          # set the camera's resolution (1280, 720)
          self.device.resolution = (1280, 720)
          # set the camera's framerate
          self.device.framerate = 24
          self.stream = picamera.PiCameraCircularIO(self.device, seconds=8)
          self.device.start_recording(self.stream, format='h264')
          return
  
      def stop_camera(self):
          """Stop the recording and turn the camera off"""
          if self.device is not None:
              if self.device.recording:
                  self.device.stop_recording()
              self.device.close()
          if self.stream is not None:
              self.stream = None
          return
  
      def capture_video(self):
          """Store the video from the circular buffer to disk"""
          try:
              if self.device is None:
                  # get the camera set up
                  self.initialize_camera()
              video_name = dt.datetime.now().strftime(
                  '%m-%d-%Y_%H:%M:%S').replace(':', '-') + '.h264'
              video_path = "/home/pi"
              # copy the buffer to disk
              print os.path.join(video_path, video_name)
              self.stream.copy_to(os.path.join(video_path, video_name), seconds=6)
              # add the MP4 wrapper to the h264 file using the MP4Box program
              mp4_command = "MP4Box -add '{0}' '{1}.mp4' -fps 24".format(
                  os.path.join(video_path, video_name),
                  os.path.join(video_path, os.path.splitext(os.path.basename(video_name))[0]))
              subprocess.call([mp4_command], shell=True)
              # remove the original h264 file
              os.remove(os.path.join(video_path, video_name))
          except BaseException as err:
              print 'error: {0} on line {1}\r'.format(err, sys.exc_info()[-1].tb_lineno)
          return
  
  
  def main():
      """Main program"""
      camera = Camera()
      camera.initialize_camera()
      camera.device.wait_recording(timeout=randint(9, 20))
      camera.capture_video()
      camera.stop_camera()
      return
  
  
  if __name__ == '__main__':
      main()
      sys.exit(0)
  </code></pre>
  
  <p>I'm using the random integer to simulate different potential points that I could be attempting to capture the recorded data to disk. I have also tried extending/reducing the seconds parameter on the <code>PiCameraCircularIO</code> object and specifically adding the <code>intra_period</code>, <code>quality</code>, or <code>bitrate</code> parameters to the <code>start_recording</code> function without seeming to affect the overall result. Is there something silly I'm overlooking?</p>