Controller for motion actions
Html server with image output, video stream and control buttons
I want to set up my Raspberry Pi 3 with Camera V2, an USB sound device with microphone and a powerbank to have a video recorder. It also opens a hotspot with a node web server so I can control it from my smartphone.
Most of it should be packed in docker so I can recreate everything with a few commands.
Make sure you have connected and tested the hardware, activated the camera with sudo raspi-config
, got images by raspistill and completed the following steps on your Raspberry Pi:
I use Raspbian, other systems might work too.
On the rasbian system activate the video driver:
$ sudo rpi-update # to make sure the v4L2 drive is available.
$ sudo modprobe bcm2835-v4l2 # to load it and create /dev/video0
This worked for me. I was having previous problems so in case anyone else has this issue; check to make sure you inserted the camera ribbon into the slot correctly, mine was in backwards.
You should add bcm2835-v4l2
to /etc/modules
so whenever the pi is restarted it will always load up the module and always have /dev/video0 available.
You should clone and build (uwegerdes/baseimage
)[https://github.com/UweGerdes/docker-baseimage] and (uwegerdes/nodejs
)[https://github.com/UweGerdes/docker-nodejs] before building the motion image.
$ docker build -t uwegerdes/motion .
Run the container with:
$ docker run -it --rm \
-v $(pwd)/modules/motion:/home/node/app/modules/motion \
-v $(pwd)/capture:/home/node/app/capture \
-v $(pwd)/key:/home/node/app/key \
-v $(pwd)/logs:/home/node/app/logs \
-v $(pwd)/fixture:/home/node/app/fixture \
-p 48080:8080 \
-p 48443:8443 \
-p 48081:8081 \
-p 8082:8082 \
-p 8083:8083 \
-e 'LIVERELOAD_PORT=48081' \
-v /dev/snd:/dev/snd \
-v /dev/video0:/dev/video0 \
--privileged \
--name motion \
uwegerdes/motion \
bash
You should start npm start
and open localhost:8080 for the server, localhost:8082 / localhost:8083 for motion / streaming (if started from server or console).
Restart it later with:
$ docker start -ai motion
In the running container you can start motion with:
motion -n
You find the results in ./capture
.
My default settings for motion work - change them as you like.
This process uses cpu and disk massively, so do it afterwards. The time depends on the (input and) output format. Best is no conversion for the video filetype.
$ ffmpeg -i "FILENAME.mp3" -r 30 -i "FILENAME.avi" "video.avi"
arecord --list-devices
can be used to find audio inputs, with an external USB camera the device name was Camera
. I got Device
for a USB soundcard.
SECS=5
TIME="$(date +%Y-%m-%d_%H:%M:%S)"
raspivid -t ${SECS}000 -w 1280 -h 720 -b 3500000 -o "video-${TIME}.h264" &
arecord -d ${SECS} -D default:CARD=Camera -t wav -c 1 -r 48000 -f S16_LE "audio-${TIME}.wav"
ffmpeg -i "video-${TIME}.h264" -i "audio-${TIME}.wav" -af loudnorm=I=-14:LRA=7:TP=-2 "vid-${TIME}.mp4"
raspistill
and raspivid
are deprecated and replaced by rpicam-*
. The new tools only work with Camera module, not USB.
raspistill -e png -dt -o "./img%010d.png" -w 1640 -h 1232 -cs 0 -n -t 10
SECS=5
TIME="$(date +%Y-%m-%d_%H:%M:%S)"
raspivid -t ${SECS}000 -w 1280 -h 720 -b 3500000 -o "video-${TIME}.h264" &
arecord -d ${SECS} -D default:CARD=Device -t wav -c 1 -r 48000 -f S16_LE "audio-${TIME}.wav"
ffmpeg -i "video-${TIME}.h264" -i "audio-${TIME}.wav" -af loudnorm=I=-14:LRA=7:TP=-2 "vid-${TIME}.mp4"
Hint: arecord
also works with -D plughw:1,0
Inspiration from (https://github.com/j3lte/node-motion)[https://github.com/j3lte/node-motion].