Motion

From Leo's Notes
Last edited on 14 June 2020, at 22:02.

Motion is a daemon that captures images and videos from a Video4Linux compatible webcam.

This page will go over the steps to set up motion on a Raspberry Pi running ArchLinux.

Setup[edit | edit source]

# pacman --noconfirm -S motion

The package doesn't seem to come with systemd service files. You may need to create it manually:

# cat <<EOF > /usr/lib/systemd/system/motion.service
[Unit]
Description=Motion web cam server

[Service]
ExecStart=/usr/bin/motion
Restart=always

[Install]
WantedBy=multi-user.target

EOF

# chmod 644 /usr/lib/systemd/system/motion.service
# systemctl daemon-reload
# systemctl enable motion
# systemctl start motion

Configuration[edit | edit source]

Edit the configuration file at /etc/motion/motion.conf.

The important fields are:

  • videodevice
  • width
  • height
  • target_dir

Motion can be configured to monitor multiple cams per instance (one per each thread). However, this is very flaky for me on a Raspberry Pi and causes corrupt/mangled images or causes webcams to disconnect.

If you plan on monitoring multiple cameras, I suggest running multiple instances of motion each recording one webcam discussed below.


Single Instance, Multiple Cameras[edit | edit source]

Specify each camera in its own individual thread file in motion.conf.


Multiple Instances, Multiple Cameras[edit | edit source]

Create an individual motion.conf file for each instance. My configs for the Raspberry Pis are:

# cat /etc/motion/motion1.conf
daemon off
process_id_file /var/run/motion/motion1.pid
setup_mode off
roundrobin_frames 1
roundrobin_skip 1
switchfilter off
threshold 0
threshold_tune off
noise_level 32
noise_tune on
despeckle EedDl
smart_mask_speed 0
lightswitch 0
minimum_motion_frames 1
pre_capture 0
post_capture 0
gap 60
max_mpeg_time 1800
output_all off
output_normal on
output_motion off
quality 75
ppm off
ffmpeg_cap_new on
ffmpeg_cap_motion off
ffmpeg_timelapse 0
ffmpeg_timelapse_mode hourly
ffmpeg_bps 500000
ffmpeg_variable_bitrate 0
ffmpeg_video_codec mpeg4
ffmpeg_deinterlace off
snapshot_interval 10
locate off
text_right %Y-%m-%d\n%T-%q
text_changes off
text_event %Y%m%d%H%M%S
text_double off
quiet on
thread /etc/motion/thread1.conf
# cat /etc/motion/thread1.conf
videodevice /dev/v4l/by-path/platform-3f980000.usb-usb-0:1.5:1.0-video-index0
v4l2_palette 8
norm 0
frequency 0
rotate 0
width 1280
height 720
framerate 2
minimum_frame_time 0
netcam_tolerant_check off
auto_brightness off
brightness 0
contrast 0
saturation 0
hue 0
snapshot_interval 3
locate off
text_right %Y-%m-%d\n%T-%q
text_changes off
text_event %Y%m%d%H%M%S
text_double off
target_dir /mnt/nfs/pi07-cam01
snapshot_filename snapshots/%Y%m/%d/%v-%H%M%S-snapshot
jpeg_filename motion/%Y%m/%d/%v-%Y%m%d%H%M%S-%q
movie_filename motion/%Y%m/%d/%v-%Y%m%d%H%M%S
timelapse_filename %Y%m%d-timelapse
webcam_port 8081
webcam_quality 80
webcam_motion off
webcam_maxrate 1
webcam_localhost off
webcam_limit 0
track_type 0
track_auto off
track_motorx 0
track_motory 0
track_maxx 0
track_maxy 0
track_iomojo_id 0
track_step_angle_x 10
track_step_angle_y 10
track_move_wait 10
track_speed 255
track_stepsize 40
# cat /etc/systemd/system/multi-user.target.wants/motion1.service
[Unit]
Description=Motion web cam server

[Service]
ExecStart=/usr/bin/motion -c /etc/motion/motion1.conf
Restart=always

[Install]
WantedBy=multi-user.target

Create these 3 files for each individual instance you desire, then reload systemd and enable/start the service.

Troubleshooting / Issues[edit | edit source]

On the Raspberry Pi, the webcam sometimes disconnects (a power issue?). The video4linux path changes because of this (from /dev/video0 to /dev/video3 for instance).

My work-around is to use the hardware path which doesn't change. You can see the paths correspond to the proper video device by listing:

# ls -al /dev/v4l/by-path/
total 0
drwxr-xr-x 2 root root 80 Mar  4 13:00 .
drwxr-xr-x 4 root root 80 Mar  4 13:00 ..
lrwxrwxrwx 1 root root 12 Mar  4 13:00 platform-3f980000.usb-usb-0:1.2:1.0-video-index0 -> ../../video0
lrwxrwxrwx 1 root root 12 Mar  4 13:00 platform-3f980000.usb-usb-0:1.3:1.0-video-index0 -> ../../video1