1

I have this code which record a video when there is a motion, and the name of the video is the date that is the code:

import RPi.GPIO as GPIO
import time
import picamera
import datetime  # new

def get_file_name():  # new
    return datetime.datetime.now().strftime("%Y-%m-%d_%H.%M.%S.h264")

sensor = 4

GPIO.setmode(GPIO.BCM)
GPIO.setup(sensor, GPIO.IN, GPIO.PUD_DOWN)

previous_state = False

current_state = False

cam = picamera.PiCamera()

while True:

    time.sleep(0.1)

    previous_state = current_state
    current_state = GPIO.input(sensor)
    if current_state != previous_state:
        new_state = "HIGH" if current_state else "LOW"
        print("GPIO pin %s is %s" % (sensor, new_state))

        if current_state:
            fileName = get_file_name()  # new
            cam.start_preview()
            cam.start_recording(fileName)  # new
        else:
            cam.stop_preview()
            cam.stop_recording()  # new    

I want to save the video in another directory, can anyone help me?

2
  • Hello, and welcome to the Raspberry Pi flavoured corner of the Stack Exchange network. I have edited your question so that the Python code is marked out. However I think the line new_state = "HIGH" if current_state else "LOW" is wrong - do you make an error copying it into the question? Do you have a fixed location that you wish to save the files to or do you want to have it in a variable that you can change - e.g. something like save_directory?
    – SlySven
    Apr 26, 2016 at 15:11
  • hi,the code is working but the problem that i wan to save the video in another directory which not the same as the location of the code
    – happy
    Apr 26, 2016 at 15:22

2 Answers 2

2

SlySven's answer is good, but I'd suggest this might be more easily accomplished with the new gpiozero library too. For example:

import os
import datetime as dt
from gpiozero import MotionSensor
from picamera import PiCamera
from signal import pause

destination = '/home/pi/video'
camera = PiCamera()
sensor = MotionSensor(4)

def record_video():
    filename = os.path.join(destination, dt.datetime.now().strftime('%Y-%m-%d_%H.%M.%S.h264'))
    camera.start_preview()
    camera.start_recording(filename)

def finish_video():
    camera.stop_recording()
    camera.stop_preview()

sensor.when_motion = record_video
sensor.when_no_motion = finish_video
pause()

Disclaimer: I may be involved in the development of these libraries :)

1

Assuming that you specify the save location as a variable save_directory (and that the location exists before you use this) I think (I am not strong in Python!) this might work for you:

import RPi.GPIO as GPIO
import time
import picamera
import datetime  # new

save_directory = "/home/pi/video/"

def get_file_name():  # new
    return datetime.datetime.now().strftime("%Y-%m-%d_%H.%M.%S.h264")

sensor = 4

GPIO.setmode(GPIO.BCM)
GPIO.setup(sensor, GPIO.IN, GPIO.PUD_DOWN)

previous_state = False

current_state = False

cam = picamera.PiCamera()

while True:

    time.sleep(0.1)

    previous_state = current_state
    current_state = GPIO.input(sensor)
    if current_state != previous_state:
        if current_state:
            new_state = "HIGH"
        else:
            new_state = "LOW"

        print("GPIO pin %s is %s" % (sensor, new_state))

        if current_state:
            fileName = "%s%s" % (save_directory, get_file_name())
            cam.start_preview()
            cam.start_recording(fileName)
        else:
            cam.stop_preview()
            cam.stop_recording()  
0

Your Answer

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

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