Recording multiple videos to differing file names with Raspberry Pi Camera triggered by IR Sensor
Question: I am currently working on setting up my Raspberry Pi to (when triggered by an
IR sensor) record twenty second videos, each to a new file.

Currently it is returning this error when movement is detected:

"_Traceback (most recent call last): File "pir_2filenametest2.py", line 57, in
for filename in camera.start_recording('pivid{counter:03}.h264'): TypeError:
'NoneType' object is not iterable_ "

I am pretty much brand new to python, so any help would be wonderful.

Here is the code:

    
    
    # Author : Matt Hawkins
    # Date   : 21/01/2013
    # Import required Python libraries
    import RPi.GPIO as GPIO
    import time
    import picamera
    # Use BCM GPIO references
    # instead of physical pin numbers
    GPIO.setmode(GPIO.BCM)
    # Define GPIO to use on Pi
    GPIO_PIR = 7
    print "Wilbur Cam! (CTRL-C to exit)"
    # Set pin as input
    GPIO.setup(GPIO_PIR,GPIO.IN)      # Echo
    Current_State  = 0
    Previous_State = 0
    camera = picamera.PiCamera()
    try:
      print "Waiting for PIR to settle ..."
      # Loop until PIR output is 0
      while GPIO.input(GPIO_PIR)==1:
        Current_State  = 0    
      print "  Ready"     
    
      # Loop until users quits with CTRL-C
      while True :
    
        # Read PIR state
        Current_State = GPIO.input(GPIO_PIR)
    
        if Current_State==1 and Previous_State==0:
          # PIR is triggered
          print "  Motion detected!"
          # Record previous state
          Previous_State=1
          # Camera begins to record
          camera.resolution = (1360, 768)
          for filename in camera.start_recording('pivid{counter:03}.h264'):
            print('Captured %s' % filename)
            time.sleep(20)
            camera.stop_recording()
        elif Current_State==0 and Previous_State==1:
          # PIR has returned to ready state
          print "  Ready"
          Previous_State=0
    
        # Wait for 10 milliseconds
        time.sleep(0.01)
    
    except KeyboardInterrupt:
      print "  Quit" 
      # Reset GPIO settings
      GPIO.cleanup()
    


Answer: [camera.start_recording](http://picamera.readthedocs.org/en/latest/api.html#picamera.PiCamera.start_recording)
does not return a value, it starts the recording, the first arg `output` is
what the video is written to which you could pass as a string or a file object
but there is nothing to iterate over:

**start_recording(output, format=None, resize=None, splitter_port=1,
**options)**

`Start recording video from the camera, storing it in output. If output is a
string, it will be treated as a filename for a new file which the video will
be written to. Otherwise, output is assumed to be a file-like object and the
video data is appended to it (the implementation only assumes the object has a
write() method - no other methods will be called).`

So in your case the filename is `'pivid{counter:03}.h264')`

If you want different filenames you could use something like the following:

    
    
    i = 0  # set i to 0 outside the while
    while True :    
        # Read PIR state
        Current_State = GPIO.input(GPIO_PIR)    
        if Current_State==1 and Previous_State==0:
          # PIR is triggered
          print "  Motion detected!"
          # Record previous state
          Previous_State=1
          # Camera begins to record
          camera.resolution = (1360, 768)
          camera.start_recording('pivid{}.h264'.format(i)) # pass i to str.format
          print('Captured pivid{}.264'.format(i))
          i += 1 # increase i
          time.sleep(20)
          camera.stop_recording()
    

