0

I have the following code which I can run normally to record video, using python3 button_led_video.py, however when I put it into /etc/rc.local the camera no longer records video. Here is the python3 script:

#!/usr/bin/python3

from gpiozero import Button
from gpiozero import LED
from picamera import PiCamera
from datetime import datetime
from time import sleep

# setup camera and button
camera = PiCamera()
button = Button(4)
led = LED(17)

# recording flag
is_recording = 0

try:
    # calibrate the camera
    camera.start_preview()
    print("CALIBRATING...")
    sleep(2)
    print("READY TO RECORD!")
    print()

    while True:
        if (button.value == True) and (is_recording == 0):
            print("RECORDING")
            x = datetime.now().strftime('%Y-%m-%d-%H-%m-%s')
            camera.start_recording('video-' + x + '.h264')
            is_recording = 1
            led.on()

        elif (button.value == False) and (is_recording == 1):
            print("DONE RECORDING!")
            camera.stop_recording()
            is_recording = 0
            led.off()

except KeyboardInterrupt:
    print("INTERRUPTED!")
    button.close()
    camera.close()
    led.close()

Here is how I have it in /etc/rc.local: sudo python3 /path/to/file.py &

When I make file.py executable and add the above line to /etc/rc.local, following a reboot (when I would expect the script to run on its own) I am able to get the LED to light up but nothing happens with the camera (no new video files appear after pressing, holding, and then releasing the button). If I try to run rc.local manually at this point by simply running /etc/rc.local I get an error that ends with: picamera.exc.PiCameraMMALError: Failed to enable connection: Out of resources

I am totally stuck on this, as it works outside of /etc/rc.local reliably. Can someone tell me what I am doing incorrectly? I am trying to get a point and shoot video camera going here and this is problem is cramping my style.

1
  • update: tried adding camera.close() after camera.stop_recording() with no improvement in symptoms. video file still does not appear though script appears to be running. Commented Jul 18, 2017 at 1:36

1 Answer 1

0

Got in touch with the maintainer of the picamera library here and have resolved issues. Here is a list of changes that need to be made to get this to work:

  1. start_recording() needs a full path to explicitly tell it where to save, otherwise it will save to /. I made the following changes to do so:

    x = datetime.now().strftime('%Y-%m-%d-%H-%m-%s')
    path = ''.join(['/home/pi/', x, '.h264'])
    camera.start_recording(path)
    
  2. the line in /etc/rc.local needs to be written like this in order to run under the pi user and therefore properly save the file in /home/pi/: su -c "python3 /path/to/file.py" pi &

After making both of these changes the code in the initial question, which was apparently always working and just saving to a different place, will work properly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.